I want to do an automatic file transfer from Windows server to my FTP.
Problem is that file is generated with timestamp in its name (the name is not fixed). So I need to upload always only the last version (newest) of file. Is there any way how to do that?
Running under Windows Server 2003. Thank you.
To select the newest file in a Windows batch file, see
How do I write a Windows batch script to copy the newest file from a directory?
Based on that you can create an upload batch file like:
@echo off
FOR /F %%I IN ('DIR C:\source\path\*.* /B /O:D') DO SET NEWEST_FILE=%%I
echo Uploading %NEWEST_FILE%
(
echo open ftp.example.com
echo username
echo password
echo put C:\source\path\%NEWEST_FILE% /target/path/%NEWEST_FILE%
echo bye
) > ftp.txt
ftp.exe -s:ftp.txt
For an easier and more reliable approach, use some more powerful 3rd party FTP client.
For example with WinSCP FTP client, you can use the -latest
switch of its put
command.
An example batch file (.bat
):
winscp.com /ini=nul /command ^
"open ftp://username:password@ftp.example.com/" ^
"put -latest C:\source\path\* /target/path/" ^
"exit"
You can even have WinSCP generate the script/batch file for you (you just need to add the -latest
switch manually).
See WinSCP article on Uploading the most recent file.
(I'm the author of WinSCP)