Trying to set username variable in batch script

Advertisement

njleach
Guest

Trying to set username variable in batch script

Hi,
So my server requires the username in the format: www.domain.com/email@address.com

This seems to cause problems when I attempt to put it all on the one line:
open sftp://www.domain.com/email@address.com:Password@www.domain.com
So I found another forum post where it is mentioned to use a variable for the username, by adding this line to the script:
set /P USERNAME="www.domain.com/email@address.com"
and then using %USERNAME% for the open script:
open sftp://%USERNAME%:Password@www.domain.com
When I try to run this it returns 'unknown command 'set'.'

My batch file is just simple:
WinSCP.exe /console /script=script.txt
and contents of script.txt:
option batch on
option confirm off
set /P USERNAME="www.domain.com/email@address.com"
open sftp://%USERNAME%:Password@www.domain.com
option transfer binary
put test2.txt /
exit
Any help appreciated!

Reply with quote

Advertisement

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

Re: Trying to set username variable in batch script

The set command should be in a wrapping batch file, not in WinSCP script.
But it won't help you anyway. It makes no difference if you specify the username directly or via environment variable.

Solution is to URL-encode the slash and at sign:
open sftp://www.domain.com%2Femail%40address.com:Password@www.domain.com
See https://winscp.net/eng/docs/session_url

Edit 2021: Now you can easily use -username switch that does not need any encoding:
https://winscp.net/eng/docs/scriptcommand_open#username

Reply with quote

njleach
Guest

Thank you so much prikryl – URL-encoding the slash and @ symbol worked like a treat. Greatly appreciated! :D

Nick

Reply with quote

Advertisement

You can post new topics in this forum