0

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

Leon
  • 3
  • 3
  • `Set /P` prompts for input. When `filename` appears on your screen, you're supposed to type it, and press the `[ENTER]` key. From what you've posted, it is clear that you are just pressing the `[ENTER]` key. – Compo Oct 14 '22 at 13:01

1 Answers1

0

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.

OJBakker
  • 602
  • 1
  • 5
  • 6
  • Yes the code is within an if statement, this was helpful with looking into set, setlocal and enabledelayedexpansion – Leon Oct 14 '22 at 15:26