0

I am making a script that can find multiple strings in an command output. For example, here is my output after running the command:

Mount Dir : D:\mount
Image File : F:\sources\boot.wim
Image Index : 1
Mounted Read/Write : No
Status : Needs Remount

I want the batch file to find the strings "D:\mount" and "Needs remount" in the output and they have to both match to give an output, but the problem is it keeps showing the wrong string:

Dir
Press any key to continue...
Needs
Press any key to continue...

I know the problems are in the delimiters, but even if I change it, the results are still the same. Here is the code that I used:

@echo off
for /f "tokens=2 delims=: " %%a in ('dism /get-mountedimageinfo ^| findstr /i /c:"Dir" /c:"status"') do (
@echo %%a
pause
)

Please help me out. Thanks in advance

1 Answers1

1

Your issue is this:

for /f "tokens=2 delims=: " %%a in (...

"delims=: " doesn't mean "delimit by colon plus space", but "delimit by colon and space" (delimters are one-char only; a string is translated into several one-char delimiters).
So tokens=2 is not what you need. You need the string after the (first) colon:

for /f "tokens=1,* delims=:" %%a in (...

where %%a is the part before the first colon and %%b is the part after the first colon (* means "do not tokenize the rest, but take it as one token"). Sadly the space after the colon is part of %%b then, but you can delete it (when needed) with substring substitution:

set "line=%%b"
set "line=!line:~1!"

(of course, you need delayed expansion for that.

Or more elegant with another for:

for /f "tokens=1,* delims=:" %%a in ('dism /get-mountedimageinfo ^| findstr /ibc:"Mount Dir" /ibc:"Status"') do (
  for /f "tokens=*" %%c in ("%%b") do (
   echo "%%c"
  )
)

Edit

According to your comment, you want to know if both the literal path D:\mount and the string Needs Remount occur in the output? Then the following approach is more straigthforward:

for /f %%a in ('dism /get-mountedimageinfo ^| findstr /ixc:"Mount Dir : D:\mount" /ixc:"Status : Needs Remount"^|find /c ":"') do set count=%%a
if %count%==2 echo both strings found

(search for both strings and count the number of lines; compare with expected "2"; No need to actually extract the strings)

Stephan
  • 53,940
  • 10
  • 58
  • 91