Trying to use wildcard in IF statement
up vote
0
down vote
favorite
I'm trying to execute some commands based on the machine hostname but am having
trouble getting it to work. I want the command to run only if the hostname starts with "Machine". Do IF statements support wildcards?
@echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1" %%i in (`wmic computersystem get name`) do (
set name=%%i
goto :done
)
:done
if [%name%]==[MachineXXXX] start "" "%ProgramFiles%Internet Exploreriexplore.exe" "download url"
endlocal
windows batch
add a comment |
up vote
0
down vote
favorite
I'm trying to execute some commands based on the machine hostname but am having
trouble getting it to work. I want the command to run only if the hostname starts with "Machine". Do IF statements support wildcards?
@echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1" %%i in (`wmic computersystem get name`) do (
set name=%%i
goto :done
)
:done
if [%name%]==[MachineXXXX] start "" "%ProgramFiles%Internet Exploreriexplore.exe" "download url"
endlocal
windows batch
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to execute some commands based on the machine hostname but am having
trouble getting it to work. I want the command to run only if the hostname starts with "Machine". Do IF statements support wildcards?
@echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1" %%i in (`wmic computersystem get name`) do (
set name=%%i
goto :done
)
:done
if [%name%]==[MachineXXXX] start "" "%ProgramFiles%Internet Exploreriexplore.exe" "download url"
endlocal
windows batch
I'm trying to execute some commands based on the machine hostname but am having
trouble getting it to work. I want the command to run only if the hostname starts with "Machine". Do IF statements support wildcards?
@echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1" %%i in (`wmic computersystem get name`) do (
set name=%%i
goto :done
)
:done
if [%name%]==[MachineXXXX] start "" "%ProgramFiles%Internet Exploreriexplore.exe" "download url"
endlocal
windows batch
windows batch
asked Nov 26 at 17:48
grahamriley
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can't use wild-cards in the sense you want, but you can achieve the result you require by using a variable substring:
if [%name:0,7%]==[Machine] start ...
An alternative approach would be to use the matching functions in findstr
:
echo %name% | findstr /b /c:"Machine" && start ...
This uses an external program, so is less efficient in this case, but it may be a useful technique for more complex matches (eg using findstr /r
).
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can't use wild-cards in the sense you want, but you can achieve the result you require by using a variable substring:
if [%name:0,7%]==[Machine] start ...
An alternative approach would be to use the matching functions in findstr
:
echo %name% | findstr /b /c:"Machine" && start ...
This uses an external program, so is less efficient in this case, but it may be a useful technique for more complex matches (eg using findstr /r
).
add a comment |
up vote
1
down vote
You can't use wild-cards in the sense you want, but you can achieve the result you require by using a variable substring:
if [%name:0,7%]==[Machine] start ...
An alternative approach would be to use the matching functions in findstr
:
echo %name% | findstr /b /c:"Machine" && start ...
This uses an external program, so is less efficient in this case, but it may be a useful technique for more complex matches (eg using findstr /r
).
add a comment |
up vote
1
down vote
up vote
1
down vote
You can't use wild-cards in the sense you want, but you can achieve the result you require by using a variable substring:
if [%name:0,7%]==[Machine] start ...
An alternative approach would be to use the matching functions in findstr
:
echo %name% | findstr /b /c:"Machine" && start ...
This uses an external program, so is less efficient in this case, but it may be a useful technique for more complex matches (eg using findstr /r
).
You can't use wild-cards in the sense you want, but you can achieve the result you require by using a variable substring:
if [%name:0,7%]==[Machine] start ...
An alternative approach would be to use the matching functions in findstr
:
echo %name% | findstr /b /c:"Machine" && start ...
This uses an external program, so is less efficient in this case, but it may be a useful technique for more complex matches (eg using findstr /r
).
answered Nov 26 at 18:23
AFH
13.6k31938
13.6k31938
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1378523%2ftrying-to-use-wildcard-in-if-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown