Test whether a string contains another string
Moderator: DosItHelp
Test whether a string contains another string
Hi,
I want to test in a batch file whether a string contains a different string.
For some reason, it is not possible to get the search string properly in the formula.
I have two variable that contain the strings:
set StringA=aba d25 c9.8 v23
set SearchString=9.7
Then i do the test:
If NOT "%StringA%"=="%StringA:%SearchString%=%" (
echo Yes
) else (
echo No
)
The test returns 'Yes' but it must be 'nN', as the following test does:
If NOT "%StringA%"=="%StringA:9.7=%" (
echo Yes
) else (
echo No
)
What am I doing wrong? Can anyone help?
I want to test in a batch file whether a string contains a different string.
For some reason, it is not possible to get the search string properly in the formula.
I have two variable that contain the strings:
set StringA=aba d25 c9.8 v23
set SearchString=9.7
Then i do the test:
If NOT "%StringA%"=="%StringA:%SearchString%=%" (
echo Yes
) else (
echo No
)
The test returns 'Yes' but it must be 'nN', as the following test does:
If NOT "%StringA%"=="%StringA:9.7=%" (
echo Yes
) else (
echo No
)
What am I doing wrong? Can anyone help?
Re: Test whether a string contains another string
The recommended way is to enable delayed expansion.
Code: Select all
@Echo Off
SetLocal DisableDelayedExpansion
Set "StringA=Aba D25 C9.8 V23"
Set "SearchString=9.7"
SetLocal EnableDelayedExpansion
If /I Not "%StringA%"=="!StringA:%SearchString%=!" (Echo Yes) Else Echo No
EndLocal
Re: Test whether a string contains another string
String replacement sometimes could be tricky.Hendrik_- wrote:What am I doing wrong?
In your case the expansion of (the second) "StringA" variable ends at the percentage sign character right after the double colon;
in this case there is no string operation because the part after the colon is missing, so it expands the environment variable "StringA:", which probably is not defined.
The next environment variable in this file is "=" which also shouldn't be defined.
So the if line after the expansion is (if both variables are undefined):
Code: Select all
If NOT "aba d25 c9.8 v23"=="SearchString" (
Sidenote: Please use code tags ([code][/code]) around your code.
penpen
Re: Test whether a string contains another string
penpen wrote:String replacement sometimes could be tricky.Hendrik_- wrote:What am I doing wrong?
In your case the expansion of (the second) "StringA" variable ends at the percentage sign character right after the double colon;
in this case there is no string operation because the part after the colon is missing, so it expands the environment variable "StringA:", which probably is not defined.
The next environment variable in this file is "=" which also shouldn't be defined.
So the if line after the expansion is (if both variables are undefined):The strings "aba d25 c9.8 v23" and "SearchString" are not equal, so it echoes "Yes".Code: Select all
If NOT "aba d25 c9.8 v23"=="SearchString" (
Sidenote: Please use code tags ([code][/code]) around your code.
penpen
Thanks for your response.
Sorry, I'm not familiar with the code tags.
Can you give an example of how you apply it?
Re: Test whether a string contains another string
Of course; there are two ways to use code tags:Hendrik_- wrote:Sorry, I'm not familiar with the code tags.
Can you give an example of how you apply it?
1) When you create or edit a post, you should see some buttons above the editing textarea.
Just click the button with the label "Code" (without doublequotes) and a pair of code tags should be inserted to the editing textarea at the current cursor position.
You could also mark some text in the textarea before clicking the button, and this text is inserted between code tags.
2) You just could type "[code]Insert your code.[/code]" (without doublequotes); result:
Code: Select all
Insert your code.
penpen
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Test whether a string contains another string
@penpen and @ShadowThief, thanks for the information.
@Compo, thank you for your suggestion.
I have now discovered that the procedure in my case does not work because it takes place in a "for" loop in a batch file.
I solved the problem by executing the string comparison in a subroutine.
And then the test works.
Anyway, thanks for the help
I've found that the old 'Dos' commando language has many options ...
@Compo, thank you for your suggestion.
I have now discovered that the procedure in my case does not work because it takes place in a "for" loop in a batch file.
I solved the problem by executing the string comparison in a subroutine.
And then the test works.
Anyway, thanks for the help
I've found that the old 'Dos' commando language has many options ...
Re: Test whether a string contains another string
If the search string changes in a FOR loop, then you may use an additional FOR command to change the delayed expansion of the search string by an equivalent replaceable parameter, and then use the delayed expansion for the base string:
This method is simpler and run faster than a CALL to a subroutine.
This type of management is fully explained at this post, although the topic is not exactly the same...
Antonio
Code: Select all
for /F %%x in ("!SearchString!") do if NOT "%StringA%"=="!StringA:%%~x=!" ( echo Yes...
This method is simpler and run faster than a CALL to a subroutine.
This type of management is fully explained at this post, although the topic is not exactly the same...
Antonio