4

I am trying this command:

for /f "tokens=3 usebackq" %%i in (`"%~dp0imagex.exe" /info "%~dp0DVD\sources\install.wim" ^| findstr /c:"Image Count:"`) do set ImageCount=%%i 
echo %ImageCount%

I get error if the path %~dp0 contains spaces like "D:\my work". Although I used usebackq and back quote instead of single quote.

the error message is:'D:\my' is not recognized as internal or external command, operable program or batch file.

What is the wrong in my command?

3
  • Possible duplicate of "batch - How to handle spaces in path names in for loop?". Commented Sep 4, 2012 at 13:09
  • I can't see anything wrong with that code that would generate the error you are reporting. USEBACKQ should not be needed, but it can't hurt. Are you sure the above is the exact command in your script? Commented Sep 4, 2012 at 14:42
  • Try running it with echo on instead of off to see if you can spot any errors. Commented Sep 4, 2012 at 20:50

1 Answer 1

2

If this doesn't work:

for /f "tokens=3 usebackq" %%i in (`"%~dp0imagex.exe" /info "%~dp0DVD\sources\install.wim" ^| findstr /c:"Image Count:"`) do set ImageCount=%%i 
echo %ImageCount%

Try this:

pushd %~dp0
for /f "tokens=3 usebackq" %%i in (`imagex.exe" /info "DVD\sources\install.wim" ^| findstr /c:"Image Count:"`) do set ImageCount=%%i 
echo %ImageCount%
popd
Sign up to request clarification or add additional context in comments.

3 Comments

The missing ~ was inadvertantly dropped in my previous edit of the question - I have restored it to the question. %~dp0 already has a trailing \ - you do not need to add another one. Adding an additional one will not hurt unless the script is running from the root directory.
@dbenham Hmm. I never noticed that.
@tcc Added a different suggestion.

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.