1

I am attempting to check whether a sub-string occurs within a string. If it does then I dont want to perform the 'if' conditions.

My Problem: My code that checks whether a sub-string occurs within a string is not working. It always thinks that a sub-string does NOT occur within a string when it actually does.

How can I check whether a sub-string occurs within a string in batch?

SET filePath="c:/users/abc/dir1/subdir"
SET excludeDir1="c:/users/abc/dir1"
SET excludeDir2="c:/users/abc/dir2"

REM // If the string excludeDir1 does not occur in filePath AND If the string excludeDir2 does not occur in filePath: continue
if /i NOT x%filePath:%excludeDir1%=%==x%filePath% if /i NOT x%filePath:%excludeDir2%=%==x%filePath% (
    REM // Do stuff
)
5
  • That should be a good reason to ditch batch files in favor of PowerShell scripts... Commented May 23, 2013 at 0:16
  • While it's likely that you are trying to do is probably better served by another scripting language, it would be good to know what behavior you're seeing and what you would expect to happen. Commented May 23, 2013 at 0:42
  • @Mark everytime I run the script (but change the filePath value) it always enters the if block when it shouldn't. What I am expecting to happen is if the strings excludeDir1 AND excludeDir2 do NOT occur within filePath then we should perform the if block code. Commented May 23, 2013 at 0:45
  • I'm not sold on the idea that I should completely abandon Batch. I could perform string comparison by: determine the length of excludeDir1(len), trim filePath so that is has the same length as len, convert it to lowercase, then compare the 2 strings. Commented May 23, 2013 at 0:48
  • 2
    @JakeM: You must fix just a small detail in your Batch code! This is the type of topics that people that don't know Batch capabilities enough, quickly suggest to change it for anything else... :( Commented May 23, 2013 at 1:37

2 Answers 2

3

You almost have it. Just remember that the parsing of the lines in a Batch file is performed from left to right, so there is no chance to nest two %variable% expansions. The way to solve it is combining one %normal% expansion and one !delayed! expansion:

REM Next command is required in order to use Delayed !variable! Expansion
SETLOCAL EnableDelayedExpansion

SET filePath="c:/users/abc/dir1/subdir"
SET excludeDir1="c:/users/abc/dir1"
SET excludeDir2="c:/users/abc/dir2"

REM // If the string excludeDir1 does not occur in filePath AND If the string excludeDir2 does not occur in filePath: continue
if /i NOT "!filePath:%excludeDir1%=!" == "%filePath%" if /i NOT "!filePath:%excludeDir2%=!" == "%filePath%" (
    REM // Do stuff
)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks very much :D Note I had to remove the quotations from filePath, excludeDir1 and excludeDir2 to get it to work.
If your paths are likely to contain an & then wrap the envar=text in double quotes like this SET "filePath=c:/users/abc/dir1/subdir" which protects the text but doesn't include the quotes.
1

Use powershell or install Cygwin and use a real POSIX/UNIX/LINUX shell like bash. You will have much better success testing strings and file paths with BASENAME and FILENAME and utilities like 'grep' and 'find' than what is available to you with CMD.EXE. You will also find plenty of stackoverflow examples from 10+ years ago on how to do all of that stuff in a proper shell.

2 Comments

+1 for PS - PowerShell ROCKS! stackoverflow.com/questions/2988880/…
Yes, powershell was introduced Dec 2008, a long overdue must-have feature. What it lacks in standard *NIX utilities it makes up for with API calls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.