FINDSTR + literal strings/double quotes and DelayedExpansion
Posted: 15 Mar 2014 09:29
Greetings DosTips addicts 
After hours of trial-and-error i've (once again) come to this place to hopefully get an explanation or solution for my FINDSTR batch troubles.
I have a script that has both CommandExtensions and DelayedExpansion enabled.
The purpose of the script is to search through a file for a matching string and show an errorlevel/exit code based on the result.
I've been trying it with and without a FOR /F loop and eventually managed to get it working, BUT i doubt i did it correctly and even don't know why it works, so hopefully one of you batch-guru's can assist/educate me.
The string to look for is in a .json file:
the script:
doesn't work:
works:
All i did was add a caret, and i could even make the script work by putting the caret somewhere else.
I really don't get it
- ©opyitright

After hours of trial-and-error i've (once again) come to this place to hopefully get an explanation or solution for my FINDSTR batch troubles.
I have a script that has both CommandExtensions and DelayedExpansion enabled.
The purpose of the script is to search through a file for a matching string and show an errorlevel/exit code based on the result.
I've been trying it with and without a FOR /F loop and eventually managed to get it working, BUT i doubt i did it correctly and even don't know why it works, so hopefully one of you batch-guru's can assist/educate me.
The string to look for is in a .json file:
Code: Select all
"selectedProfile": "
the script:
Code: Select all
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SystemPath=!SystemRoot!\System32\
SET PROFILES=example.json
REM continues below
doesn't work:
Code: Select all
"!SystemPath!FINDSTR" /IC:"\"selectedProfile\": \"" "!PROFILES!">NUL
ECHO '!ERRORLEVEL!'
works:
Code: Select all
"!SystemPath!FINDSTR" /IC:^"\"selectedProfile\": \"" "!PROFILES!">NUL
ECHO '!ERRORLEVEL!'
All i did was add a caret, and i could even make the script work by putting the caret somewhere else.
I really don't get it

- ©opyitright