Wildcards in Strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Wildcards in Strings

#1 Post by alleypuppy » 28 May 2011 16:43

Hey guys,

I need to know if you can put wildcards in strings. For example, I want to make an error message appear if the command has the right prefix, but an invalid extension.

Here is where I want the wildcard to be inserted:

Code: Select all

set var1=iexplore
set var2=notepad
set var3=chrome

set /p command=^>
if /I "%command%"=="start %var1%" start %var1%
if /I "%command%"=="start %var2%" start %var2%
if /I "%command%"=="start %var3%" start %var3%
if /I "%command%"=="start [WILDCARD]" echo Unknown Application!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Wildcards in Strings

#2 Post by orange_batch » 28 May 2011 16:58

Solution to both threads you made:

Code: Select all

set exes=iexplore,notepad,chrome

set /p command=^>

if /I "%command:~,5%"=="start" (
for %%x in (%exes%) do (
if /I "%command:~6%"=="%%x" (
start %%x
set started=1
))
if defined started (set start=) else echo Unknown Application!
)

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Wildcards in Strings

#3 Post by alleypuppy » 28 May 2011 19:49

Great! It works just the way I want it to! Thanks!

Just one more thing. I want to change "start" to something else. I tried changing it and it didn't work. Is this because of the ":~,5" or is it something else?

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Wildcards in Strings

#4 Post by orange_batch » 28 May 2011 19:57

Yes, that's character length. Say the command was echo, it should be ":~,4". That syntax means "from __ to 4" (__ defaults to 0, same as ":~0,4").

Negative values can be used to go from the end of the string too. http://ss64.com/nt/syntax-substring.html

The solution could be made better though, so you don't need substrings.

Code: Select all

set exes=iexplore,notepad,chrome

set /p command=^>

for /f "tokens=1*" %%x in ("%command%") do (
if /I "%%x"=="start" (
for %%z in (%exes%) do (
if /I "%%y"=="%%z" (
start %%z
set started=1
))
if defined started (set start=) else echo Unknown Application!
))

for /f is pretty much the most useful batch command. Essential to learn.

By the way, if you're just trying to change the command prompt character for the session... try prompt /?.

Code: Select all

prompt $G

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Wildcards in Strings

#5 Post by alleypuppy » 29 May 2011 14:16

orange_batch wrote:Solution to both threads you made:

Code: Select all

set exes=iexplore,notepad,chrome

set /p command=^>

if /I "%command:~,5%"=="start" (
for %%x in (%exes%) do (
if /I "%command:~6%"=="%%x" (
start %%x
set started=1
))
if defined started (set start=) else echo Unknown Application!
)

Could you re-post the code that you posted last night? I accidentally overwrote the file with the code and I can't get this one to work like I want it to.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Wildcards in Strings

#6 Post by orange_batch » 29 May 2011 15:34

Uhm, which? Both codes are still in the thread here.

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Wildcards in Strings

#7 Post by alleypuppy » 29 May 2011 17:42

The one that I quoted. It didn't have the "for" command in it before.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Wildcards in Strings

#8 Post by orange_batch » 30 May 2011 00:01

Before I edited my first response, I think this is what I changed:

Code: Select all

set var1=iexplore
set var2=notepad
set var3=chrome

set /p command=^>

if /I "%command:~,5%"=="start" (
if /I "%command:~6%"=="%var1%" (start %var1%
) else if /I "%command:~6%"=="%var2%" (start %var2%
) else if /I "%command:~6%"=="%var3%" (start %var3%
) else echo Unknown Application!
)

Not a good solution though.

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Wildcards in Strings

#9 Post by alleypuppy » 01 Jun 2011 17:05

Yes! Thank you very much! Kudos to you! :wink:

Post Reply