Reputation: 686
I am using .bat script to access sftp and download file. The ftp folder consist of a lot of file which can be uniquely identify by the file name in format YYYYMMDD. eg: 20180307.csv, 20180306.csv etc. Currently I am using mget to get all the file, then at later filter it locally. How can I modify the script to get the file on today only?
This is my .bat script.
D:
CD\myTargetFolder
psftp [email protected] -pw password -b mySftpScript.ftp
exit
This is my ftp script.
cd /myFTPSourceFolder
mget *.csv
bye
Thank you.
Upvotes: 2
Views: 2650
Reputation: 38719
If you don't mind dynamically creating the ftp script then perhaps your .bat script could look like this:
@Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined csv Set "csv=%%A%%B%%C.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp [email protected] -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
I've commented out the last line, remove the ::
from it if you wish to delete the dynamically created ftp script.
Edit
To get yesterday's date csv then something like this, leveraging PowerShell, would be my recommendation:
@Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('yyyMMdd')"`
) Do Set "csv=%%A.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp [email protected] -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
Upvotes: 1