hostname result as variable and then comparing it
So. This is making me go crazy. My batch file knowledge is very basic.
So far I have this from other sources to set a variable to be the result from hostname
FOR /F "usebackq" %%i IN (`hostname`) DO SET PCNAME=%%i
I'm still confused by this whole line but it works so I've kept it. I would like to understand what each part of it is doing though.
With that variable PCNAME I now want to do the following
IF PCNAME = RDS then GOTO exit
else
GOTO main
But I can't get this IF ELSE part to work (the above I know isn't true code but it's just to explain what I'd like to do).
So to sum up.
Create a variable from the result of hostname.
Compare that variable to see if it matches RDS. If it does, then quit, if it doesn't go onto another part of the batch file.
I was thinking of looking to see if its easier to do in Powershell instead. My knowledge of that is mega basic as well but looks like Powershell is easier to use than batch files.
batch-file
add a comment |
So. This is making me go crazy. My batch file knowledge is very basic.
So far I have this from other sources to set a variable to be the result from hostname
FOR /F "usebackq" %%i IN (`hostname`) DO SET PCNAME=%%i
I'm still confused by this whole line but it works so I've kept it. I would like to understand what each part of it is doing though.
With that variable PCNAME I now want to do the following
IF PCNAME = RDS then GOTO exit
else
GOTO main
But I can't get this IF ELSE part to work (the above I know isn't true code but it's just to explain what I'd like to do).
So to sum up.
Create a variable from the result of hostname.
Compare that variable to see if it matches RDS. If it does, then quit, if it doesn't go onto another part of the batch file.
I was thinking of looking to see if its easier to do in Powershell instead. My knowledge of that is mega basic as well but looks like Powershell is easier to use than batch files.
batch-file
1
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
if /?, for /f
– LotPings
Nov 12 at 23:56
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply doset PCNAME=%computername%
or eve better, just use the%computername%
preset variable.
– Gerhard Barnard
Nov 13 at 7:25
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04
add a comment |
So. This is making me go crazy. My batch file knowledge is very basic.
So far I have this from other sources to set a variable to be the result from hostname
FOR /F "usebackq" %%i IN (`hostname`) DO SET PCNAME=%%i
I'm still confused by this whole line but it works so I've kept it. I would like to understand what each part of it is doing though.
With that variable PCNAME I now want to do the following
IF PCNAME = RDS then GOTO exit
else
GOTO main
But I can't get this IF ELSE part to work (the above I know isn't true code but it's just to explain what I'd like to do).
So to sum up.
Create a variable from the result of hostname.
Compare that variable to see if it matches RDS. If it does, then quit, if it doesn't go onto another part of the batch file.
I was thinking of looking to see if its easier to do in Powershell instead. My knowledge of that is mega basic as well but looks like Powershell is easier to use than batch files.
batch-file
So. This is making me go crazy. My batch file knowledge is very basic.
So far I have this from other sources to set a variable to be the result from hostname
FOR /F "usebackq" %%i IN (`hostname`) DO SET PCNAME=%%i
I'm still confused by this whole line but it works so I've kept it. I would like to understand what each part of it is doing though.
With that variable PCNAME I now want to do the following
IF PCNAME = RDS then GOTO exit
else
GOTO main
But I can't get this IF ELSE part to work (the above I know isn't true code but it's just to explain what I'd like to do).
So to sum up.
Create a variable from the result of hostname.
Compare that variable to see if it matches RDS. If it does, then quit, if it doesn't go onto another part of the batch file.
I was thinking of looking to see if its easier to do in Powershell instead. My knowledge of that is mega basic as well but looks like Powershell is easier to use than batch files.
batch-file
batch-file
edited Nov 13 at 0:38
LotPings
17.2k61531
17.2k61531
asked Nov 12 at 22:34
Jon Donnis
1
1
1
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
if /?, for /f
– LotPings
Nov 12 at 23:56
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply doset PCNAME=%computername%
or eve better, just use the%computername%
preset variable.
– Gerhard Barnard
Nov 13 at 7:25
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04
add a comment |
1
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
if /?, for /f
– LotPings
Nov 12 at 23:56
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply doset PCNAME=%computername%
or eve better, just use the%computername%
preset variable.
– Gerhard Barnard
Nov 13 at 7:25
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04
1
1
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
if /?, for /f
– LotPings
Nov 12 at 23:56
if /?, for /f
– LotPings
Nov 12 at 23:56
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply do
set PCNAME=%computername%
or eve better, just use the %computername%
preset variable.– Gerhard Barnard
Nov 13 at 7:25
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply do
set PCNAME=%computername%
or eve better, just use the %computername%
preset variable.– Gerhard Barnard
Nov 13 at 7:25
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04
add a comment |
1 Answer
1
active
oldest
votes
Thanks for the replies. In the morning, and as Gerhard Barnard mentioned I realised I could just use %computername%
which works
@echo off
IF %computername% EQU MyPC (
echo MyPC
) else (
echo Do nothing
)
As a best practice I would advise that you use this syntax when doing string comparisons.IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the/I
option for a case insensitive match.
– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use==
for that. What do you mean you cannot step through code?
– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fstackoverflow.com%2fquestions%2f53271080%2fhostname-result-as-variable-and-then-comparing-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for the replies. In the morning, and as Gerhard Barnard mentioned I realised I could just use %computername%
which works
@echo off
IF %computername% EQU MyPC (
echo MyPC
) else (
echo Do nothing
)
As a best practice I would advise that you use this syntax when doing string comparisons.IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the/I
option for a case insensitive match.
– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use==
for that. What do you mean you cannot step through code?
– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
add a comment |
Thanks for the replies. In the morning, and as Gerhard Barnard mentioned I realised I could just use %computername%
which works
@echo off
IF %computername% EQU MyPC (
echo MyPC
) else (
echo Do nothing
)
As a best practice I would advise that you use this syntax when doing string comparisons.IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the/I
option for a case insensitive match.
– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use==
for that. What do you mean you cannot step through code?
– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
add a comment |
Thanks for the replies. In the morning, and as Gerhard Barnard mentioned I realised I could just use %computername%
which works
@echo off
IF %computername% EQU MyPC (
echo MyPC
) else (
echo Do nothing
)
Thanks for the replies. In the morning, and as Gerhard Barnard mentioned I realised I could just use %computername%
which works
@echo off
IF %computername% EQU MyPC (
echo MyPC
) else (
echo Do nothing
)
edited Nov 13 at 17:03
Squashman
8,41231932
8,41231932
answered Nov 13 at 16:21
Jon Donnis
1
1
As a best practice I would advise that you use this syntax when doing string comparisons.IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the/I
option for a case insensitive match.
– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use==
for that. What do you mean you cannot step through code?
– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
add a comment |
As a best practice I would advise that you use this syntax when doing string comparisons.IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the/I
option for a case insensitive match.
– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use==
for that. What do you mean you cannot step through code?
– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
As a best practice I would advise that you use this syntax when doing string comparisons.
IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the /I
option for a case insensitive match.– Squashman
Nov 13 at 17:04
As a best practice I would advise that you use this syntax when doing string comparisons.
IF "%computername%"=="MyPC"
. Also remember that the string comparisons are case sensitive. Use the /I
option for a case insensitive match.– Squashman
Nov 13 at 17:04
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
@Squashman OK thanks. I'm having trouble understanding the help file for IF and ss64.com hasn't made it any easier. So I'm curious to know what the, easy to understand difference is between EQU and == It's difficult understanding where an issue lies in the BATCH file as well due to no ability to step through the code.
– Jon Donnis
Nov 14 at 16:42
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use ==
for that. What do you mean you cannot step through code?– Squashman
Nov 14 at 16:51
EQU, NEQ, GTR,GEQ,LSS, and LEQ
are normally used for comparing integers. While they can be used to compare strings, it is better to use ==
for that. What do you mean you cannot step through code?– Squashman
Nov 14 at 16:51
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
@Squashman Thanks, that makes more sense. Regarding the step through comment, I've searched for the option to step through code in a batch file line by line to debug it. But everyone says this isn't possible with BATCH files. The only way I've semi done it, which isn't great, is stick PAUSEs in various locations I believe I may have errors.
– Jon Donnis
Nov 15 at 9:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53271080%2fhostname-result-as-variable-and-then-comparing-it%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
1
If you read the help file for the IF command you will clearly see what the syntax is for using ELSE
– Squashman
Nov 12 at 23:18
if /?, for /f
– LotPings
Nov 12 at 23:56
This line is simple. it assigns a token (%%i) to the output of the command hostname and sets another variable with that same name.. pretty useless as you could simply do
set PCNAME=%computername%
or eve better, just use the%computername%
preset variable.– Gerhard Barnard
Nov 13 at 7:25
@Squashman Thanks Squashman for the warm welcome. I've always had issues making sense of the CMD help file examples, much as the same of DOS help way back when, hence I thought I'd ask here as everyone seems helpful.
– Jon Donnis
Nov 13 at 9:04