I am trying to create a batch file that FTPs into a URL(say ftp.tester123.com.au) with directory "C:\Documents\Client\" using username mark123 and password testabc into local directory "C:\Desktop\GoHere\".
The script is to copy all the files within this ftp directory to my local machine. I have read the following msdn tutorial
https://support.microsoft.com/en-gb/kb/96269
But it didn't help me with my current issue. Will winscp allow me to perform this task and create a batch file for it? I would like to automate execution of this directory - at say midnight every night. Is this possible?
Alternatively I am very familiar with .NET and winSCP has a wrapper to allow you to write C# instead of standard scripting. If I go with a .NET approach, can I create a simple .exe or batch file that can simply be executed by double clicking?
Thanks in advance!
What you are trying to do is a very trivial task. What you are missing is a conceptual understanding of the task. So it's difficult to give you an answer that will help you, as it's difficult to understand what piece of knowledge you are missing to accomplish it.
The easiest approach to start with, is to make use of WinSCP ability to generate a transfer code:
Login to your FTP server with WinSCP;
Navigate to the source remote directory and to the destination local directory;
Select the remote files to download (all files?);
Use the Files > Download command in the main menu (or the Download command in the files' context menu);
On the Download dialog, click the drop down arrow on the Transfer Settings button and select the Generate Code command.
You will get the Generate transfer code dialog.
There, you can choose if you want WinSCP to generate a WinSCP script or even complete Windows batch file:
or .NET assembly (C#) code:
(These are official screenshots from WinSCP documentation. So they show SFTP upload, not FTP download)
Now that you have your batch file or C# executable ready, you can schedule it to be run using Windows Scheduler.
If you have WinSCP installed, then the following code will work for you. I fetched all files from a particular folder and downloaded it to a local folder. Wrote a logfile along the way. You can generate this code using WinSCP too.
#echo off
set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
set d_name=/export/home/sysm/opt/oss/server/var/fileint/pm/pmexport_%mydate%
#echo %d_name%
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\HP\Desktop\Datacom\Reports\WinSCP.log" /ini=nul ^
/command ^
"open sftp://YOUR_USERNAME:YOUR_PASSWORD#FTP_SERVER_IP/ -hostkey=""ssh-rsa 1024 PUuYRVADKXB9j1Si+o89v2fsrsr7w2ZrV3NIqdz6kus0GtY=""" ^
"cd %d_name%" ^
"lcd C:\Users\HP\Desktop\Datacom\Reports\Test_ftp" ^
"get *" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
Modfify it according to your need and then put the code into a .bat file. Later you can execute it manually or use Windows Task Scheduler.
Related
I'm trying to create a SFX file and run a vbs afterwards.
Here's my config file:
;!#Install#!UTF-8!
InstallPath="c:\\windows\\temp\\"
ExecuteFile="cscript.exe"
ExecuteParameters="c:\\windows\\temp\\script.vbs"
;!#InstallEnd#!
The .7z archive is not corrupt, I cn=an open and extract files.
here's how I created the .exe:
copy /b 7zS.sfx + config.txt + SylinkReplacer.7z SylinkReplacer.exe
When I run it a cmd windows flashes I have the feeling it's trying tu run the vbs but I can see it has not been extracted in C:\windows\temp.
I ran process monitor and apparently it extract the files in my users' %appdata% within a temporary folder named 7zNNNNNN which gets deleted afterwards.
Any suggestion? Thanks
I found a way to make it work (I actually found it on stackoverflow but now I can't find it anymore).
;!#Install#!UTF-8!
ExecuteFile="ReplaceSylink.vbs"
;!#InstallEnd#!
This is not the best solution since I wanted it to run it with cscript (console) while on many servers the default engine is wscript (GUI).
I'll use something to force cscript like this on this one http://www.robvanderwoude.com/vbstech_engine_force.php
Dears, could you please share your ideas how to download files from an FTP server to a local directory filtered by changed date.
So, I have a folder on the FTP server with files, but I need to move just those of them which have been changed yesterday (f.e.).
Thanks in advance for your reply.
open anymail.com
login
pass
!:--- FTP commands below here ---
lcd D:\Test
cd /home/my_folder
binary
mget *
disconnect
bye
You can hardly do this with the Windows built-in ftp.exe.
You would have to list the remote directory, redirect the listing to a file. Then parse the listing file to find the files you want. And then generate an ad-hoc download script. Quite a lot of work.
Use some more capable 3rd party Windows FTP command-line client.
For example with WinSCP scripting you can use a batch file (e.g. download_yesterdays.bat) like:
winscp.com /ini=nul /log=ftp.log /command ^
"open ftp://user:password#ftp.example.com/" ^
"cd /remote/path" ^
"lcd c:\local\path" ^
"get *>=yesterday<today" ^
"exit"
The >=yesterday selects files created since yesterday (inclusive). The <today excludes files created today. This syntax is supported since WinSCP 5.15.
In earlier versions of WinSCP, you can use %TIMESTAMP% syntax instead: >=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd% (the TIMESTAMP-1D syntax is supported since WinSCP 5.9).
References:
WinSCP guide to Downloading the most recent file from FTP server
File masks with time-constraints
%TIMESTAMP% syntax
(I'm the author of WinSCP)
I have program in Unix that generates files 8 files at 8:30 pm everyday.
I need to download the files into my Windows machine after the files are generated using WinSCP.
I can drag and drop those but its time consuming, I need to automate this process.
I cannot use .Net assembly to download those.
I have tried to use the get command, but its throwing error: Not an SCCS file.
Let me know how can I solve this.
--Thanks
To automate a task using WinSCP, use its scripting interface from a batch file (e.g. download.bat).
I assume you want to use SFTP, as you are connecting to a *nix server.
The simplest download batch file is like:
winscp.com /log=c:\path\to\log\winscp.log /command ^
"open sftp://username:password#example.com/ -hostkey=""xxx""" ^
"get /path/to/file.ext c:\path\to\download\to\" ^
"exit"
Replace the username, password and example.com with the actual connection details. Get the value of -hostkey switch from your GUI session. Use real paths.
Though it's easier to have WinSCP generate the batch file for you.
For details see a guide to automating file transfers from SFTP server using WinSCP.
Once you have the batch file working, schedule the the batch file execution using Windows scheduler.
See a guide to scheduling file transfers from SFTP server.
I have the following batch file:
open <ip>
username
password
bin
lcd C:\FTP_OUT\
prompt
mput *.PDF
ascii
mput *.XML
bye
How do I change it to upload only files older than 1 minute to the FTP server?
The Windows ftp.exe does not support any kind of a file selection based on a file timestamp.
All you can do is to generate a temporary upload script with an explicit list of files to upload. You can use a PowerShell script to generate the temporary script (implementing in a pure batch file would be very difficult).
A way easier solution is to use an FTP client that supports selecting files based on file timestamp.
For example with WinSCP FTP/SFTP client you can use the following batch file:
#echo off
winscp.com /log=upload.log /command ^
"open ftp://username:password#example.com/" ^
"lcd C:\FTP_OUT" ^
"put *.PDF<1N" ^
"put -transfer=ascii *.XML<1N" ^
"exit"
The <1N in the files mask is a time-constraint that selects files created or modified more than one minute ago only.
See also the guide for converting Windows ftp.exe scripts to WinSCP scripts.
(I'm the author of WinSCP)
Say I've got a windows batch file with a file extension .batscript, and say I've got a web server set up to serve it with a content-type of application/x-batscript. How can I set up Firefox 4 to "always open" this file so that it runs as a standard batch file would? (I have access to the PCs but want avoid creating/installing external programs/extensions/plugins)
I can't use a straight .bat because Firefox doesn't allow those to be opened directly; they have to be saved and run separately (for obvious security reasons).
The standard way to run a batch file is cmd /c %1 where %1 is the path to the bat file, but this doesn't work when %1 is a path to a file with a custom file extension (such as .batscript) because cmd looks up what application %1 should be opened with, so adding a direct file association via the registry doesn't work (it recursively opens cmd prompts).
Instead, I found a work around where I temporarily copy my .batscript file to a .bat file and run it, using registry entries like this, which allows me to double click on a .batscript file to run it. But Firefox doesn't respect these platform file associations!
Ok, so Firefox wants me to define my file type associations separately for each content-type: fair enough, file extensions & content-types aren't always the same. But I can only get Firefox to run discrete applications, and a batch script needs to be run with cmd.exe /c %1 not just cmd %1.
Does anybody know of a way to force Firefox to either use the system file association or allow launching arbitrary programs with command line parameters?
(For those wondering, the use case is similar to "an internal web app which allows users to run a local program with a varying amount of command line parameters by clicking on a link" - alternative solutions to these small "breaking out of the sandbox" problems are also welcome!)
I think you need to edit the MimeTypes.rdf file. For more information see also this page.