In a batch file, I have a string abcdefg
. I want to check if bcd
is in the string.
Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring.
Is there an easy solution for this?
In a batch file, I have a string abcdefg
. I want to check if bcd
is in the string.
Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring.
Is there an easy solution for this?
You can pipe the source string to
findstr
and check the value ofERRORLEVEL
to see if the pattern string was found. A value of zero indicates success and the pattern was found. Here is an example:When this is run in CMD.EXE, we get:
If you are detecting for presence, here's the easiest solution:
This works great for dropping the output of windows commands into a boolean variable. Just replace the echo with the command you want to run. You can also string Findstr's together to further qualify a statement using pipes. E.G. for Service Control (SC.exe)
That one evaluates the output of SC Query for windows update services which comes out as a multiline text, finds the line containing "state" then finds if the word "running" occurs on that line, and sets the errorlevel accordingly.
Better answer was here:
I usually do something like this:
Example:
Output:
I don't know if this is the best way.
For compatibility and ease of use it's often better to use FIND to do this.
You must also consider if you would like to match case sensitively or case insensitively.
The method with 78 points (I believe I was referring to paxdiablo's post) will only match Case Sensitively, so you must put a separate check for every case variation for every possible iteration you may want to match.
( What a pain! At only 3 letters that means 9 different tests in order to accomplish the check! )
In addition, many times it is preferable to match command output, a variable in a loop, or the value of a pointer variable in your batch/CMD which is not as straight forward.
For these reasons this is a preferable alternative methodology:
Use: Find [/I] [/V] "Characters to Match"
[/I] (case Insensitive) [/V] (Must NOT contain the characters)
As Single Line:
Multi-line:
As mentioned this is great for things which are not in variables which allow string substitution as well:
Output From a command:
As you can see this is the superior way to handle the check for multiple reasons.
Yes, you can use substitutions and check against the original string:
The
%str1:bcd=%
bit will replace abcd
instr1
with an empty string, making it different from the original.If the original didn't contain a
bcd
string in it, the modified version will be identical.Testing with the following script will show it in action:
And the results of various runs:
A couple of notes:
if
statement is the meat of this solution, everything else is support stuff.x
before the two sides of the equality is to ensure that the stringbcd
works okay. It also protects against certain "improper" starting characters.