1

My batch file is throwing error when run, but when I run the commands one by one manually in command line then I get no error. Here's the batch file 'test.bat'.

echo "test"
cd "c:\packages"
pause
for /R c:\packages %F in (*.msi) do set /A servername=%~nxF
pause
echo %servername%
pause

I get this error - "~nxF was unexpcted at this time".

I got the sample code from https://stackoverflow.com/a/1100466/1105556 I'm just trying the get the file name in c:\packages & store the value in 'servername' variable. There is only one file (.msi) in the folder I cant figure out whats wrong. Can someone please solve the riddle for me?

1 Answer 1

4

You need to double the %% in batch files:

for /R c:\packages %%F in (*.msi) do set /A servername=%%~nxF
Sign up to request clarification or add additional context in comments.

6 Comments

Also, set /A evaluates the RHS as an arithmetic expression, which probably isn't wanted.
Thank you Ken & @arx. Took both suggestions & it worked. Just out of curiosity what is RHS. I googled it but couldn't find any thing relevant
@Sudhakar: RHS = Right Hand Side. /A takes everything to the right of it and tries to use it as an arithmetic expression (eg., 1 + 1 * 3
Thank you Ken. Appreciate your time.
It works ! But can you tell me why we have to add double % on batch file?
|

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.