Batch file - Download the latest FTP folder - batch-file

I'm trying to download the latest folder from an FTP server. This folder contains several folders in which contains several CSV files.
The issue I have is that the folders are created each day and each time I run the script I only want it to download the latest folder at that location.
I have not the foggiest idea of how to specify this, or even download an entire folder structure from an FTP using batch file.
Please let me know if any additional information is required and I will provide it immediately, thanks again for your help.
Kind Regards,
Lewis

There's no easy way to select the most recent folder with the built-in Windows FTP client, the ftp.exe. You would have more luck with a PowerShell script and the FtpWebRequest.
But even if you manage to select the most recent directory, neither the ftp.exe nor the FtpWebRequest support recursive downloads anyway.
You better use some more powerful 3rd party FTP client.
For example with WinSCP FTP client you can download the latest file or folder, using the -latest switch of the get command (WinSCP 5.9 and newer):
winscp.com /command ^
"open ftp://username:password#ftp.example.com/" ^
"cd /remote/path" ^
"lcd c:\local\path" ^
"get -latest *" ^
"exit"
See also the guide to downloading the most recent file with WinSCP.
(I'm the author of WinSCP)

Related

How can I make a directory(folder) in FTP server using current date?

Thank you for helping me in advance !
I am currently making FTP batch file to create new folders in FTP server. the problem is that I could figure out how to make folders in local but I can't not find the way to make folders naming current date in FTP server. Could you please let me know command lines to fix this problem?
It's easy with (my) WinSCP FTP client and its %TIMESTAMP% syntax:
winscp.com /ini=nul /command ^
"open ftp://username:password#example.com/" ^
"cd /remote/parent/path" ^
"mkdir %%TIMESTAMP#yyyymmdd%%" ^
"exit"

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"

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)

Preserve file modification timestamp when downloading with Windows FTP mget command

My FTP mget command is overwriting file modification timestamp, which actually I don't want.
Please help me on this.
Copy.bat
open 10.198.37.61
user
password
mget *.*
disconnect
quit
Main.bat
ftp -i -s:Copy.bat
The Windows built-in FTP client (ftp.exe) cannot preserve timestamp of remote file when downloading (and vice versa).
You have to use another FTP client. Most other FTP clients do preserve the timestamp.
For example with WinSCP scripting use the following batch file (Main.bat):
winscp.com /ini=nul /log=script.log /command ^
"open ftp://user:password#10.198.37.61/" ^
"get *.*" ^
"exit"
See also Converting Windows FTP script to WinSCP script.
(I'm the author of WinSCP)
CoreFTP LE (or PRO) can preserve timestamp - FTP or SFTP
To configure CoreFTP LE:
Site Manager -> Advanced -> General -> Convert Files to GMT (mark it)
Site Manager -> Advanced -> Transfers -> Use MFMT for date modify (mark it)
Done!

Automated FTP to upload new files to web server?

I'm looking for a FTP client that I can use to upload new files from a local development machine to a remote web-server. I only want to upload the newly edited files though.
Is there a command line utility that can do this, that I can add into an automated process? Is there a GUI client available that can do this? Would be nice to have it cross-platform too. Any ideas?
The Mercurial FTP Extension should do this for you, although I haven't tried it myself.
There is a 'backup' program called SyncBack that does this.
You can find out more about it here: http://www.2brightsparks.com
You have two options:
Schedule a frequent synchronization of a local folder against a remote folder (or moving all files from local folder to a remote folder, if that's more appropriate)
Use a tool that can watch for changes in a local directory and reflect them on a remote directory
You can implement both these options with WinSCP FTP client.
Scheduling
To synchronize changes in a local directory to a remote directory, use the WinSCP synchronize script command from a batch file like:
winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"synchronize remote C:\local\path /remote/path" ^
"exit"
And schedule the batch file to be run frequently using Windows scheduler.
If you do not want to keep a local copy of the images, just move them to the FTP server, instead of synchronizing them. For that, replace the
"synchronize remote C:\local\path /remote/path" ^
with the put -delete command like:
"put -delete C:\local\path\* /remote/path/" ^
For details see also a guide to automating file transfers (or synchronization) to FTP server.
Watching for changes
Use "Keep remote directory up to date" function of WinSCP.
It can be used both in command-line/console mode using the keepuptodate command, like:
winscp.com /ini=nul /log=c:\writable\path\to\synchronize.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"keepuptodate C:\local\path /remote/path" ^
"exit"
Or in a graphical/GUI mode. You can launch the graphical mode in WinSCP GUI (after logging in) or from a command-line using the /keepuptodate switch like:
winscp.exe ftp://username:password#ftp.example.com/ /keepuptodate C:\local\path /remote/path
(I'm the author of WinSCP)

Resources