Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Im saying this at the beginning so it doesn't happen: Don't say anything bad about batch its needed for a reason.

I need wildcards in an if statement something like

<br />
if "food is good"=="food * good" echo Yes I agree it is<br />

                                              (Wildcard)


Is this possible? How can I do this?
Posted
Comments
RedDk 31-Jul-13 14:15pm    
http://www.dostips.com/ and wayd through the search return ...
Sergey Alexandrovich Kryukov 31-Jul-13 15:02pm    
I'm saying this at the beginning so it doesn't happen (later again): don't be so arrogant, let people tell you whatever they find appropriate. After all, you are asking for help, so get ready to listen to member's opinion. I think most members will understand that batch can be useful.
—SA
pasztorpisti 31-Jul-13 16:30pm    
Your toolset is very limited if you are using just builtin commands. This kind of problem should be solved by running a small external tool program or script from your bat file by passing the strings as parameters and then the external stuff could return the result as an exitcode. I would use for example python script (maybe a perl one-liner) in which this task is ridiculously easy.
G4mm4R4y 1-Aug-13 2:41am    
I do know a little python but not enough to get by without help, I know barely anything besides variables math and user input
pasztorpisti 1-Aug-13 4:35am    
Then read the tutorial. The python homepage is excellent, you can find everything you need there.

1 solution

AFAIK the batch IF Statement only works on string equality / non-equality, there are no wildcards.
However, it would not be difficult to write a quick'n'dirty application which takes two strings as parameters, and returns ERRORLEVEL 0 for equality and +1 and -1 for greater and less than. Since it'd be your app, wildcards would be completely under your control...
 
Share this answer
 
Comments
PIEBALDconsult 31-Jul-13 14:37pm    
Or RegEx.
OriginalGriff 31-Jul-13 14:45pm    
I've not heard of a regex in a batch file: does the cmd process support them?
PIEBALDconsult 31-Jul-13 14:46pm    
I mean write a utility like you suggest, but use RegEx to perform the test.
OriginalGriff 31-Jul-13 14:59pm    
That's a pity! I thought I was going to learn something new. :laugh:
PIEBALDconsult 31-Jul-13 15:49pm    
Sorry to get your hopes up. On the other hand, I wonder if an in-line Perl script would do it. (I don't have Perl installed on this system.)
On the plus side, I reviewed my simple little RegEx utility and added the return of the number of matches found.


C:\Projects\Utilities>regex "food .* good" "food is good"
0 = food is good

C:\Projects\Utilities>if errorlevel 1 echo Yay!
Yay!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900