Preserve file modification timestamp when downloading with Windows FTP mget command - batch-file

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!

Related

Batch File FTP over TLS [duplicate]

I currently have batch scripts on different servers that transfer a csv file to an FTP server at a different location. My script looks similar to this:
echo user ftp_user> ftpcmd.dat
echo password>> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.MyFTPSite.com
del ftpcmd.dat
If I wanted to require a secure transmission, is how would my script be updated?
Thanks.
First, make sure you understand, if you need to use Secure FTP (=FTPS, as per your text) or SFTP (as per tag you have used).
Neither is supported by Windows command-line ftp.exe. As you have suggested, you can use WinSCP. It supports both FTPS and SFTP.
Using WinSCP, your batch file would look like (for SFTP):
echo open sftp://ftp_user:password#ftp.MyFTPSite.com -hostkey="..." >> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv >> ftpcmd.dat
echo exit >> ftpcmd.dat
winscp.com /script=ftpcmd.dat
del ftpcmd.dat
And the batch file:
winscp.com /log=ftpcmd.log /script=ftpcmd.dat /parameter %1 %date%
Though using all capabilities of WinSCP (particularly providing commands directly on command-line and the %TIMESTAMP% syntax), the batch file simplifies to:
winscp.com /log=ftpcmd.log /command ^
"open sftp://ftp_user:password#ftp.MyFTPSite.com -hostkey=""...""" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"
For the purpose of -hostkey switch, see verifying the host key in script.
Easier than assembling the script/batch file manually is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you:
All you need to tweak is the source file name (use the %TIMESTAMP% syntax as shown previously) and the path to the log file.
For FTPS, replace the sftp:// in the open command with ftpes:// (explicit TLS/SSL) or ftps:// (implicit TLS/SSL). And remove the -hostkey switch.
winscp.com /log=ftpcmd.log /command ^
"open ftps://ftp_user:password#ftp.MyFTPSite.com -explicit" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"
You may need to add the -certificate switch, if your server's certificate is not issued by a trusted authority.
Again, as with the SFTP, easier is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you.
See a complete conversion guide from ftp.exe to WinSCP.
You should also read the Guide to automating file transfers to FTP server or SFTP server.
Note to using %TIMESTAMP#yyyymmdd% instead of %date%: A format of %date% variable value is locale-specific. So make sure you test the script on the same locale you are actually going to use the script on. For example on my Czech locale the %date% resolves to čt 06. 11. 2014, what might be problematic when used as a part of a file name.
For this reason WinSCP supports (locale-neutral) timestamp formatting natively. For example %TIMESTAMP#yyyymmdd% resolves to 20170515 on any locale.
(I'm the author of WinSCP)
The built in FTP command doesn't have a facility for security. Use cUrl instead. It's scriptable, far more robust and has FTP security.
ftps -a -z -e:on -pfxfile:"S-PID.p12" -pfxpwfile:"S-PID.p12.pwd" -user:<S-PID number> -s:script <RemoteServerName> 2121
S-PID.p12 => certificate file name ;
S-PID.p12.pwd => certificate password file name ;
RemoteServerName => abcd123 ;
2121 => port number ;
ftps => command is part of ftps client software ;

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)

Batch file - Download the latest FTP folder

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)

Can I use SOCKS5 in FTP batch file on Windows?

I want to use a SOCKS5 connection with a generic proxy to upload files to FTP.
I'm working on Windows server. My .txt for my .bat actually looks like:
open proxyhost
proxyuser
proxypassword
user ftpuser#ftphost.de
ftppassword
bin
mput \\upload\path\to\*.pdf
quit
I'm opening the .txt file with:
ftp -i -s:"\\path\to\mytxt.txt" >"\\path\to\ftp.log"
find "226" "\\path\to\ftp.log" && goto finished || goto error
:finished
exit 0
:error
exit 1
Now the question: Is there a way to use SOCKS5 in this .txt?
I found nothing on Google.
Maybe you can help me out. Thanks a lot.
Best regards
The Windows built-in ftp.exe FTP client does not support any kind of proxies (the FTP proxy you are using in your script is a transparent proxy, the FTP client does not even know it exists).
You have to use another command-line FTP client.
For example with WinSCP scripting:
winscp.com /log="\\path\to\ftp.log" /command ^
"open ftp://ftpuser:ftppassword#ftphost.de/ -rawsettings ProxyMethod=2 ProxyHost=proxyhost ProxyUsername=proxyuser ProxyPassword=proxypassword" ^
"put \\upload\path\to\*.pdf" ^
"exit"
if errorlevel 1 goto error
exit 0
:error
exit 1
For details see:
the guide for converting Windows FTP script to WinSCP.
documentation for the "raw session settings" for the proxy.
(I'm the author of WinSCP)

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