Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a string for a substring in a batch file (Windows)?

Tags:

Let's say I have some text in a variable called $1. Now I want to check if that $1 contains a certain string. If it contains a certain string I want to print a message. The printing is not the problem, the problem is the check. Any ideas how to do that?

like image 506
EOB Avatar asked Jan 06 '12 10:01

EOB


People also ask

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I search for a word in a batch file?

Using the findstr command lets you search for text within any plaintext file. Using this command within a batch file lets you search for text and create events off the results found.

How do I search for a string in a command prompt?

You can run findstr from the command line or as a batch file. Open a new command line prompt by clicking on the Windows-key, typing cmd.exe, and selecting the result. Alternatively, use the Run command to open findstr.


1 Answers

The easiest way in my opinion is this :

set YourString=This is a test  If NOT "%YourString%"=="%YourString:test=%" (     echo Yes ) else (     echo No ) 

Basiclly the string after ':' is the string you are looking for and you are using not infront of the if because %string:*% will remove the * from the string making them not equal.

like image 171
Wald Avatar answered Sep 19 '22 20:09

Wald