Page 1 of 1
Wildcards in Strings
Posted: 28 May 2011 16:43
by alleypuppy
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!
Re: Wildcards in Strings
Posted: 28 May 2011 16:58
by orange_batch
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!
)
Re: Wildcards in Strings
Posted: 28 May 2011 19:49
by alleypuppy
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?
Re: Wildcards in Strings
Posted: 28 May 2011 19:57
by orange_batch
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.htmlThe 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 /?.
Re: Wildcards in Strings
Posted: 29 May 2011 14:16
by alleypuppy
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.
Re: Wildcards in Strings
Posted: 29 May 2011 15:34
by orange_batch
Uhm, which? Both codes are still in the thread here.
Re: Wildcards in Strings
Posted: 29 May 2011 17:42
by alleypuppy
The one that I quoted. It didn't have the "for" command in it before.
Re: Wildcards in Strings
Posted: 30 May 2011 00:01
by orange_batch
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.
Re: Wildcards in Strings
Posted: 01 Jun 2011 17:05
by alleypuppy
Yes! Thank you very much! Kudos to you!