Automatic FTP download via batch - batch-file

I need to download a file from my company's FTP server daily, I tried to do this via batch
open wget --user=USERNAME --password=PASSWORD FTP://000.00.0.0
cd/company/file
get *.* forfiles /D+0
put C:\loads\path
pause
I tried some alternatives seen in the WinSCP documentation, but I was not successful.

To download files from an FTP server using WinSCP from a batch file, you can do:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log=winscp.log /ini=nul /command ^
"open ftp://username:password#example.com/ ^
"cd /company/file" ^
"get *.*" ^
"put C:\loads\path\*" ^
"exit"
pause
See https://winscp.net/eng/docs/guide_automation.
Also WinSCP GUI can generate a FTP download batch file template for you.

Related

WinSCP - Download file from SFTP server to network location, then delete

I need to write a WinSCP script or batch file that will download the contents of a remote SFTP folder, delete contents and then upload to a mapped network folder. I know how to write the login part of the script. But I am confused as to what the specific command line should look like. From what I've read the command script to download and delete the file is something like this:
get -delete -transfer=binary *
The part that I'm not getting is the upload to the network folder command. I understand put is the WinSCP upload command operator. But how do I use it in conjunction with the network folder location files should be uploaded to? Also, can this be scripted out in a batch file or do the batch file and script need to be written separately? Thanks!
An "upload" to a mapped network folder is not a job for WinSCP. A mapped network folder behaves as a local drive. A plain Windows copy command will do. So first do your WinSCP download and then copy (it's not really called an upload) the files to the network drive.
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* c:\local\drive\path\" ^
"exit"
copy c:\local\drive\path\* n:\mapped\network\drive\path\
Though, if you do not need the files locally, you can have WinSCP download the files from the SFTP folder directly to the mapped network drive:
winscp /ini=nul /command ^
"open sftp://user:password#example.com/" ^
"get -delete /sftp/path/* n:\mapped\network\drive\path\" ^
"exit"

How to execute WinSCP commands from a batch file

I wanna know if there is a way to run WinSCP commands like open sftp..., from a batch file, without having to enter commands after WinSCP opens up.
Here's what I wanna happen:
I create a batch file (say demo.bat) that opens WinSCP command-line.
I insert WinSCP commands (open, get, put, etc.) in demo.bat.
The batch file demo.bat opens up WinSCP command-line and executes the commands (get, put, etc.) automatically.
Use /command or /script command-line switches.
There's a guide to automating file transfers to SFTP server with WinSCP.
A simple batch file using /command switch is like:
WinSCP.com ^
/log="C:\writable\path\to\log\WinSCP.log" /ini=nul ^
/command ^
"open sftp://username:password#example.com/" ^
"put C:\local\path\file.txt /remote/path/" ^
"exit"
And WinSCP GUI can actually even generate the batch file for you:

Downloading new files from FTP server to Windows in a batch file

I am writing a batch file that downloads files from an FTP server.
Now, I want these files to be checked against the existing files, so that they are not being downloaded again. There are multiple files being downloaded and I do not know the name of the files that may be present in the FTP server.
Thanks
Just use any Window command-line FTP client that supports synchronization.
For example with WinSCP FTP client, you can use a batch file like:
#echo off
winscp.com /ini=nul /log=synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize local C:\local\path /remote/path" ^
"exit"
References:
Guide to automating synchronization from FTP server;
Scripting command synchronize.
You can have WinSCP GUI generate a batch file template for you.
(I'm the author of WinSCP)

Using batch file and WinSCP to download files from the FTP server to file server (shared folder)

I am using the following code to transfer files from my FTP server to my local machine which works fine.
"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
"open ftp://rnandipati:J13#Files8.cyberlynk.net/kgptel/" ^
"lcd ""C:\\rnandipati\KGP\File History""" ^
"get *.xls>1D" ^
"rm *.xls<1D" ^
"exit"
Now, I access my server using this path
\\fs01\\Reporting\KGP\File History
When I put this path in place of my local directory path, it shows an error that the system could not find the file specified and error changing directory.
Thanks.
A UNC path cannot be a working directory in Windows.
But you can use it as a target path in the get command:
get *.xls>1D "\\fs01\Reporting\KGP\File History\"
A full command for a batch file will be:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
"open ftp://rnandipati:J13#Files8.cyberlynk.net/kgptel/" ^
"get *.xls>1D ""\\fs01\Reporting\KGP\File History\""" ^
"rm *.xls<1D" ^
"exit"
(not that I understand a logic of the get *.xls>1D and rm *.xls<1D)
For a similar question, see Get file from FTP server and copy it to UNC directory.
If you need to authenticate to the file server, see:
Include credentials of shared folder in WinSCP script file
How to give credentials in a batch script that copies files to a network location?

Automatically upload new files added in multiple directories to FTP

I am trying to do some automation in uploading new files to FTP that were added in local directories to the same directories in server.
Is there anyway I could make that possible? Please help me out. I will be grateful.
Thank you!
Just use any command-line Windows FTP client that supports synchronization.
For example with WinSCP scripting, you can use its synchronize command:
winscp.com /log=ftp.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize remote c:\local\path /remote/path" ^
"exit"
And depending on your requirements, either loop the batch file or schedule it in Windows scheduler.
For real-time synchronization, you can also use the keepuptodate command.
winscp.com /log=ftp.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"keepuptodate c:\local\path /remote/path" ^
"exit"
(I'm the author of WinSCP)

Resources