Using multiple variable in command-lin

Advertisement

mrfat0la
Joined:
Posts:
3

Using multiple variable in command-lin

I have a SFTP site with multiple folders that I need to download all of the *.pdf files into multiple folders on our network. All of local folders and remote folders listed in a text file which I read using the "for /F (tokens=*)" format in a batch file.

for /F "tokens=1, 2 delims=;" %%A in (STD-Folders.txt) do "C:\Program Files (x86)\WinSCP\WinSCP.com" /log="C:\WinSCP\SFTP\Logs\Log.txt" /ini=nul /command "open sftp://my-sftp-site:v!so9tpjszd9O9@sftp.us2.pool.com/ -hostkey=""ssh-rsa 2048 zeKC3/C87n3L12jSehP7r9B1LKHs3Vqo/PguPtVnUxQ=""" "cd /"%%B "lcd E:\Faxes\"%%A "get -delete *.pdf" "lcd E:\Faxes\Z-IDXFiles" "get -delete *.idx" "exit"

This works well but this method opens a session and exits on every iteration. Having 70 folders to download takes a lot of time. I would like to establish one session and then use "cd /remotefolder1" "lcd/ localfolder1" "get -delete *.pdf"
"cd /remotefolder2" "lcd/ localfolder2" "get -delete *.pdf"
"cd /remotefolder3" "lcd/ localfolder3" "get -delete *.pdf"
etc...where the remotefolder1 and localfolder1 are variables from a text file. I do not want to have 70 "cd/" "lcd/ " and "get -delete" statements. Just one that will loop and replace the variable and execute ... replace and execute until the end of the file is reached. Something similar to:

"C:\Program Files (x86)\WinSCP\WinSCP.com" /log="C:\WinSCP\SFTP\Logs\Log.txt" /ini=nul /command "open sftp://my-sftp-site:v!so9tpjszd9O9@sftp.us2.pool.com/ -hostkey=""ssh-rsa 2048 zeKC3/C87n3L12jSehP7r9B1LKHs3Vqo/PguPtVnUxQ="

Start Loop:
"cd /"%%remotefolder "lcd E:\Faxes\"%%localfolder "get -delete *.pdf"
"cd /"%%remotefolder "lcd E:\Faxes\"%%localfolder "get -delete *.pdf"
"cd /"%%remotefolder "lcd E:\Faxes\"%%localfolder "get -delete *.pdf"
End Loop:

Until the end-of-file then "exit".

Reply with quote

Advertisement

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

Re: Using multiple variable in command-lin

So make you for loop append a command to a (temporary) WinSCP script file, instead of running WinSCP.
And run WinSCP with the script file only after the for loop.

Reply with quote

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

Re: Using multiple variable in command-lin

Roughly something like this (untested):

echo open sftp://my-sftp-site:v!so9tpjszd9O9@sftp.us2.pool.com/ -hostkey="ssh-rsa 2048 zeKC3/C87n3L12jSehP7r9B1LKHs3Vqo/PguPtVnUxQ=" > script.txt
 
for /F "tokens=1, 2 delims=;" %%A in (STD-Folders.txt) do (
    echo lcd E:\Faxes\%%A >> script.txt
    echo lcd get -delete *.pdf >> script.txt
)
 
echo exit >> script.txt
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" /log="C:\WinSCP\SFTP\Logs\Log.txt" /ini=nul /script=script.txt

Reply with quote

Advertisement

You can post new topics in this forum