I am trying to write a batch script to iterate folders and sub-folders from last five days and push file to FTP from network drive. So far I am able to get 5 days previous date and current date and able to push file to FTP.
But not able to find out the solution to iterate over last 5 days folders and sub-folders inside that.
@echo off
REM get current date
set TIMESTAMP=%date:~10,4%-%date:~7,2%-%date:~4,2%
REM get 5 days previous date from current date
set day=-5
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "pre_date=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%pre_date:~0,4%"
set "MM=%pre_date:~4,2%"
set "DD=%pre_date:~6,2%"
set "pre_date=%yyyy%-%mm%-%dd%"
REM synchronize folder from network drive to FTP
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\testuser\Desktop\batch\WinSCP.log" /ini=nul ^
/command ^
"open sftp://team:O%%l%%24og3@187.121.11.25/ -hostkey=""ssh-ed25519 256 f0pa/Fxg81I0Wjo=""" ^
"synchronize remote Z:\test\folder1 /mnt/logs/test" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
PAUSE
exit /b %WINSCP_RESULT%
FOLDER STRUCTURE-
date folder(folder name only date) --> multiple sub-folders --> then some sub-folders are having files AND some sub-folders again having sub-folders. Only first folder having name as date not sub-folders.
Make use of WinSCP %TIMESTAMP%
syntax to generate a file mask to filter the synchronization to the folders you want:
%TIMESTAMP#yyyy-mm-dd%/;%TIMESTAMP-1D#yyyy-mm-dd%/;%TIMESTAMP-2D#yyyy-mm-dd%/;%TIMESTAMP-3D#yyyy-mm-dd%/;%TIMESTAMP-4D#yyyy-mm-dd%/;%TIMESTAMP-5D#yyyy-mm-dd%/
The whole synchronize
command will be like:
"synchronize remote Z:\test\folder1 /mnt/logs/test -filemask=%%TIMESTAMP#yyyy-mm-dd%%/;%%TIMESTAMP-1D#yyyy-mm-dd%%/;%%TIMESTAMP-2D#yyyy-mm-dd%%/;%%TIMESTAMP-3D#yyyy-mm-dd%%/;%%TIMESTAMP-4D#yyyy-mm-dd%%/;%%TIMESTAMP-5D#yyyy-mm-dd%%/" ^
(note the doubled %
, due to batch file syntax)
And now you do not need any of your "date calculation" code.
If you want the number of days to be variable, you can generate the mask in a loop:
set DAYS=5
for /l %%D in (0, 1, %DAYS%) do call set MASK=%%MASK%%;%%%%TIMESTAMP-%%DD#yyyy-mm-dd%%%%/
set MASK=%MASK:~1%
"synchronize remote Z:\test\folder1 /mnt/logs/test -filemask=%MASK%" ^
If you really need to loop over the days, because you have specific directory structure requirements, you can do:
set DAYS=5
for /l %%D in (0, 1, %DAYS%) do (
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\testuser\Desktop\batch\WinSCP.log" /ini=nul ^
/command ^
"open sftp://team:O%%l%%24og3@187.121.11.25/ -hostkey=""ssh-ed25519 256 f0pa/Fxg81I0Wjo=""" ^
"synchronize remote Z:\%%TIMESTAMP-%%DD#yyyy-mm-dd%%\folder1 /mnt/logs/%%TIMESTAMP-%%DD#yyyy-mm-dd%%/test ^
"exit"
)
If you do not want to connect for every date, you can generate the synchronize
commands in the loop only. And execute them at once using one connection (one WinSCP.com
call) at the end.