How to download to new folder with today date

Advertisement

KJ88
Joined:
Posts:
1

How to download to new folder with today date

Hi,

I'm new to WinSCP and to scripting so please forgive my lack of knowledge.

I have created a .bat file to automatically download several files from an FTP server. I have added this .bat file to Windows Task Scheduler so it will run several times each day.

Now I would like to know how to add a function to my .bat file that creates a new folder with the current date and time, then downloads the files into the newly created folder.

My .bat file currently looks like this:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^ 
  /log="C:\Users\xxxxxx\WinSCP.log" /ini=nul ^
  /command ^
    "open ftp://anonymous:anonymous%%40example.com@ftp.com.au/" ^
    "cd /xxx/xxx/xxx/xxx" ^
    "lcd ""C:\Users\Data""" ^
    "get xxxx1.nc.gz" ^
    "get xxxx2.nc.gz" ^
    "get xxxx3.nc.gz" ^
    "exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
exit /b %WINSCP_RESULT%
Is there a way to have this same script create a new folder with today date and time in C:\Users\Data and then download the files there?

Many thanks

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,605
Location:
Prague, Czechia

Re: How to download to new folder

WinSCP script cannot create local folders. You have to create the folder in the batch file, before you start WinSCP. But you can use WinSCP %TIMESTAMP% syntax to generate the folder name.
See Formatting Timestamp in Batch File.
So something like this:
set TIMESTAMP_FORMAT=yyyy-mm-dd
 
for /F "tokens=* USEBACKQ" %%F in (
    `winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"`
) do set TIMESTAMP=%%F
 
set TARGET_PATH=C:\Users\Data\%TIMESTAMP%
mkdir %TARGET_PATH%
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^ 
  ...
  /command ^
    ...
    "lcd ""%TARGET_PATH%""" ^
    ...

Reply with quote

Advertisement

You can post new topics in this forum