Checking for several different strings in a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Checking for several different strings in a variable

#1 Post by ALbino » 31 Oct 2014 18:13

Hey all,

I was trying to think of the cleanest way to do this, but I'm not sure what the best way is, or even how to go about it.

Essentially I just want to see if various phrases are in my filename, and if it is then to set a variable one way, and if they're absent, then to set it another. In a sort of pseudo-code:

Code: Select all

if %InputFilename% contains "*example*" OR "*another*version*" OR "*this*and*that*" (
 set Condition=1
 ) else (
    set Condition=2
)


There may be upwards of 20+ of these conditional phrases. Is there a way to do this using some type of OR in the if statement? Or should it just be a giant chain of ELSE IF questions?

Also, is there a way to search for "*word1*word3*" with wildcards before, after, and in the middle, so that "word1word2word3.txt" would be found?

Any help would be appreciated. Thanks!

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Checking for several different strings in a variable

#2 Post by ShadowThief » 31 Oct 2014 18:57

You can store the phrases to search for in a separate file and then check the filename for each substring one at a time.

Code: Select all

@echo off
setlocal enabledelayedexpansion

:: Get the filename from user input
set filename=%~1

if "%filename%"=="" (
   echo Please provide a string to search.
   exit /b
)

:: This isn't a real boolean, but it plays one on TV
set substring_found=false

:: Check filename against list of strings in DAT file
for /f %%A in (namecheck.dat) do (
   echo %filename%|findstr "%%A" >nul
   if !errorlevel! equ 0 (
      set substring_found=true
   )
)

if "!substring_found!"=="true" (
   echo Substring found!
) else (
   echo Substring not found!
)


Code: Select all

namecheck.dat (sample input)
flag_1
apples
children
flag4
screwdriver
strawberry
handbrake
flag__8
check phrase


Note that there are some characters like ^ that need to be escaped if you've going to use them.

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#3 Post by ALbino » 31 Oct 2014 19:31

Ahh, yes, that's a great idea. Thanks so much for the help!

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Checking for several different strings in a variable

#4 Post by Aacini » 31 Oct 2014 20:30

It is simpler and faster if you use findstr's /F option, that is, using the same data of ShadowThief's answer:

Code: Select all

:: Check filename against list of strings in DAT file
echo %filename%|findstr /G:namecheck.dat >nul
if %errorlevel% equ 0 (
   set substring_found=true
)

However, this method is slow if you want to check a large number of filenames because FINDSTR must be executed several times, so it would be faster to assemble a long command that check all strings in just one line; for example:

Code: Select all

set Found=true
if "!filename:example=!" equ "%filename%" if "!filename:another version=!" equ "%filename%" if "!filename:this and that=!" equ "%filename%" set Found=false


Antonio

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#5 Post by ALbino » 31 Oct 2014 20:37

Thanks for the suggestion. Would both of these methods work with multiple strings? You've written:

Code: Select all

if "!filename:this and that=!"


Will that find only "here is this and that.txt" or will it also find "here is this and here is that.txt"?

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Checking for several different strings in a variable

#6 Post by Aacini » 31 Oct 2014 20:46

This method will only find the exact "this and that" string. You may use FINDSTR method in order to use regular expressions...

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#7 Post by ALbino » 31 Oct 2014 20:56

Good to know, thank you.

Can anybody recommend a good comprehensive walk through of dealing with strings? This is what I've been using, but in their examples they rarely show the eventual result:

http://ss64.com/nt/findstr.html

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Checking for several different strings in a variable

#8 Post by ShadowThief » 31 Oct 2014 21:38

What sort of "dealing with strings" information are you looking for? Because we have http://www.dostips.com/DtTipsStringOperations.php and http://www.dostips.com/DtTipsStringManipulation.php which should cover a good chunk of basic string coding.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Checking for several different strings in a variable

#9 Post by ghostmachine4 » 31 Oct 2014 21:41

vbscript comes with regular expression.

Code: Select all

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set regex = New RegExp
With regex
   .Pattern = "example|this.*that"
   .IgnoreCase = True
   .Global = False
End With
inputFile = WScript.Arguments(0)
Set objFile = objFSO.OpenTextFile(inputFile)
Do Until objFile.AtEndOfStream
   strNextLine = objFile.ReadLine
   Set Matches = regex.Execute(strNextLine)
   If regex.Test(strNextLine) Then
      WScript.Echo strNextLine
   End If
Loop

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#10 Post by ALbino » 31 Oct 2014 21:57

ShadowThief wrote:What sort of "dealing with strings" information are you looking for? Because we have http://www.dostips.com/DtTipsStringOperations.php and http://www.dostips.com/DtTipsStringManipulation.php which should cover a good chunk of basic string coding.


Thank you, I appreciate the link!

Your example works with single phrases, but I'm unsure how to use multiple ones. For example, if I do this:

Code: Select all

set filename=word1word2word3.txt


And I put into namecheck.dat:

Code: Select all

word1


Then that returns "Substring found", which it should. If I then put into namecheck.dat:

Code: Select all

word1 word2


It also returns "Substring found", which seems like it's working, but really it's just finding "word1" still, because if I then replace it in namecheck.dat with:

Code: Select all

word1 word9 word2


It still returns "Substring found". Ideally in this scenario of word1word2word3.txt would return found only if all three were in namecheck.dat and in the same order, i.e. "*word1*word2*word3*". If this is confusing let me know and I'll try and explain it better :)

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#11 Post by ALbino » 31 Oct 2014 21:58

ghostmachine4 wrote:vbscript comes with regular expression.

Code: Select all

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set regex = New RegExp
With regex
   .Pattern = "example|this.*that"
   .IgnoreCase = True
   .Global = False
End With
inputFile = WScript.Arguments(0)
Set objFile = objFSO.OpenTextFile(inputFile)
Do Until objFile.AtEndOfStream
   strNextLine = objFile.ReadLine
   Set Matches = regex.Execute(strNextLine)
   If regex.Test(strNextLine) Then
      WScript.Echo strNextLine
   End If
Loop


I'm not sure how to use this... can I call it from a batch script? Either way, thanks for the effort :)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Checking for several different strings in a variable

#12 Post by ghostmachine4 » 31 Oct 2014 22:25


I'm not sure how to use this... can I call it from a batch script? Either way, thanks for the effort :)


save as a file and on command line

Code: Select all

cscript //nolog myscript.vbs myFileToSearch.txt

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Checking for several different strings in a variable

#13 Post by Aacini » 31 Oct 2014 23:10

ALbino wrote:Your example works with single phrases, but I'm unsure how to use multiple ones. For example, if I do this:

Code: Select all

set filename=word1word2word3.txt


And I put into namecheck.dat:

Code: Select all

word1


Then that returns "Substring found", which it should. If I then put into namecheck.dat:

Code: Select all

word1 word2


It also returns "Substring found", which seems like it's working, but really it's just finding "word1" still, because if I then replace it in namecheck.dat with:

Code: Select all

word1 word9 word2


It still returns "Substring found". Ideally in this scenario of word1word2word3.txt would return found only if all three were in namecheck.dat and in the same order, i.e. "*word1*word2*word3*". If this is confusing let me know and I'll try and explain it better :)


I copied the following information from FINDSTR help (and translated it to English):

FINDSTR /? wrote:Use spaces to separate multiple search strings. For example, 'FINDSTR "one two" x.y' search for "one" or "two" in x.y file.


If you want to search for "word1", followed by zero or more characters, followed by "word2", followed by zero or more characters, followed by "word3", then you must use a regular expression:

FINDSTR /? wrote:Regular expression quick reference:
. Wild-card: any character
* Repeat: zero or more occurrences of previous character


This way, to match the search described in previous paragraph, you must include the following regular expression into namecheck.dat:

Code: Select all

word1.*word2.*word3

and insert /R switch in the FINDSTR command.

I suggest you to read about "regular expressions"...

Antonio

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Checking for several different strings in a variable

#14 Post by ALbino » 31 Oct 2014 23:30

I'll read up on it, thank you Antonio!

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Checking for several different strings in a variable

#15 Post by Yury » 01 Nov 2014 09:11

ALbino wrote:Essentially I just want to see if various phrases are in my filename, and if it is then to set a variable one way, and if they're absent, then to set it another. In a sort of pseudo-code:

Code: Select all

if %InputFilename% contains "*example*" OR "*another*version*" OR "*this*and*that*" (
 set Condition=1
 ) else (
    set Condition=2
)


There may be upwards of 20+ of these conditional phrases. Is there a way to do this using some type of OR in the if statement? Or should it just be a giant chain of ELSE IF questions?



Code: Select all

@echo off

set "InputFilename=The another new version of the document.txt"

:: Add, change or remove regular expressions.
:: Don't use «^».
:: Change «*» to «.|».
for %%i in (
 ".|example.|"
 ".|another.|version.|"
 ".|this.|and.|that.|"
 ) do (
 rem Don't use «set RegExp=%%i» and «'call echo %%RegExp:|=*%%'». _
 rem Don't use «set RegExp="%%~i"» and «'call echo %%RegExp:|=*%%'». _
 rem Use only «set RegExp=%%~i» and «'call echo "%%RegExp:|=*%%"'».
 set RegExp=%%~i
 for /f "delims=" %%j in ('call echo "%%RegExp:|=*%%"') do (
  call set Args=%%Args%% /c:%%j
  )
 )

for /f "delims=" %%i in ("%InputFilename%") do (
 rem Don't use «echo %%i| findstr ...». _
 rem It is deadly for the execution of the code _
 rem if some poison characters are present.
 echo "%%i"| findstr /ir%Args%>nul&& (
 set Condition=1
 )|| (
 set Condition=2
 )
 )

set Condition

pause>nul
exit /b

Post Reply