Creating a folder to download into

Advertisement

Shay_
Joined:
Posts:
4

Creating a folder to download into

As title says, is it possible to have the batch file create a folder and download files into it so I can have multiple backups?
Also is it possible to download into a compressed zip file?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: Creating a folder to download into

Create the folder in the batch file using mkdir command. And then execute WinSCP from the same batch file for the download.

Similarly, if you want to ZIP the downloaded files, execute the "zip" after WinSCP finishes with the download.

Reply with quote

Shay_
Joined:
Posts:
4

Sorry, I'm really new to all this. Could I get an example with my batch code?
If I do zip files, I'm hoping it'll create a zip each time with a timestamp so they're easy to track.
@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://stuff@stuff/ -hostkey="""" -rawsettings FSProtocol=2 ProxyPort=0" ^
    "cd /17539" ^
    "lcd C:\Backups" ^
    "get folder" ^
    "get folder" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%

Reply with quote

Shay_
Joined:
Posts:
4

An example if I were to use the zip command. You said execute zip once the download finishes right? So would it be something like this?
@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://stuff@stuff/ -hostkey="""" -rawsettings FSProtocol=2 ProxyPort=0" ^
    "cd /17539" ^
    "lcd C:\Backups" ^
    "get folder" ^
    "get folder" ^
    "zip -r "backup-`date +%Y%m%d%H%M`.zip" "backup/"" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%

I saw the command in another thread, but I'm not sure if it's the exact one I need to use. Right now I just want to download two folders and zip them into the directory with a timestamp.

Reply with quote

Advertisement

Shay_
Joined:
Posts:
4

@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://stuff@stuff/ -hostkey="""" -rawsettings FSProtocol=2 ProxyPort=0" ^
    "cd /17539" ^
    "lcd C:\Backups" ^
    "get folder" ^
    "get folder" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
    "zip -r "backup.zip" "!/"" ^
    "set TIMESTAMP_FORMAT=yyyy-mm-dd
winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit""
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%
Did I do this correctly?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

No. That's not the code from the article, it should be like:
set TIMESTAMP_FORMAT=yyyy-mm-dd
 
for /F "tokens=* USEBACKQ" %%F in (
    `winscp.com /command "echo %%TIMESTAMP#%TIMESTAMP_FORMAT%%%" "exit"`
) do set TIMESTAMP=%%F
 
zip -r "backup-%TIMESTAMP%.zip" C:\Backups
I do not know about the zip commandline syntax, that depends on what implementation of ZIP you have. And that's not a WinSCP question anyway.

Reply with quote

Advertisement

You can post new topics in this forum