Home > other >  Batch Script - Using Variable with SCP command
Batch Script - Using Variable with SCP command

Time:10-14

I have a simple batch script which transfers a file filename to a remote location destination using SCP.

Using scp filename destination works perfectly fine, however adding variable var doesn't work.

Code snippet:

set /p "var=filename"
echo %var%
echo scp "%var%" destination
scp "%var%" destination

Results:

filename
scp "" destination
: not a regular file

The variable is not used or recognized by the second echo and scp.

Is there something I'm missing from using the variable properly in echo and scp?

Note: Code snippet is in an if statement

Solution: Need to use delayexpansion, and use !var! instead of%var%

See here: windows batch SET inside IF not working

CodePudding user response:

There is no problem with the snippet you posted.
But these lines will fail in the way you describe if these lines are within a parenthesized codeblock.
If this is the case then you must change the code to use delayed expansion.
See setlocal/? and set/? for details and documentation.

  • Related