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

1
  • 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. Commented Oct 14, 2022 at 13:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes the code is within an if statement, this was helpful with looking into set, setlocal and enabledelayedexpansion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.