Using %COMPUTERNAME% question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chondro
Posts: 6
Joined: 19 Mar 2015 09:43

Using %COMPUTERNAME% question

#1 Post by chondro » 26 Mar 2015 13:38

Hi,
I am in the middle of writing a batchfile to do a bunch of things, but I do not want any of my executions to work on a particular server, if, by chance someone were to execute this on that server.
So what I have done within the batch is exactly below. In the goto:server I just have a message stating that this batch can't be used here and it makes you go back to the menu. It works, but the problem is its working no matter what server I am on, loops me back to the menu no matter what.

Code: Select all

:ERROR
ECHO This can't be run here
pause
GOTO Menu

if "%COMPUTERNAME%"=="MYSERVERNAME"
goto:ERROR


Above is the exact syntax, seems trivial and not a difficult thing to check, but its kicking my butt, and I am new to batch scripting. I have also tried using :arrow: call instead of if...
Any suggestions?
Thanks in advance!

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Using %COMPUTERNAME% question

#2 Post by Squashman » 26 Mar 2015 15:27

The GOTO needs to be on the same line as the IF.

chondro
Posts: 6
Joined: 19 Mar 2015 09:43

Re: Using %COMPUTERNAME% question

#3 Post by chondro » 26 Mar 2015 16:19

Thanks for he reply, I have tried that and it doesn't make a difference..

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Using %COMPUTERNAME% question

#4 Post by Squashman » 26 Mar 2015 16:34

Without seeing all of the actual script it would be hard to troubleshoot.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using %COMPUTERNAME% question

#5 Post by foxidrive » 26 Mar 2015 18:15

chondro wrote:Thanks for he reply, I have tried that and it doesn't make a difference..


It's the correct solution to the syntax error that you showed. :)

What you have shown above is code that will never be reached.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Using %COMPUTERNAME% question

#6 Post by trebor68 » 27 Mar 2015 02:04

I have built here no menu, but it is possible in the block "menu" to build multiple choices.
The GOTO instructions have to be adapted your code, depending on whether you want to jump to menu or program termination.

Code: Select all

@echo off
:menu
echo.
echo   Code block 1 is running.
echo.
goto :begin

:error
echo  This can't be run here.
echo.
pause
rem goto :menu
goto :eof

:begin
echo  This computer is: "%computername%"
if not "%computername%"=="MYSERVERNAME" goto :error

echo.
echo  This is the correct computer.
echo   Code block 2 is running.
echo.

rem goto :eof
rem goto :menu

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Using %COMPUTERNAME% question

#7 Post by Compo » 27 Mar 2015 04:16

chondro wrote:

Code: Select all

if "%COMPUTERNAME%"=="MYSERVERNAME"
goto:ERROR

Try a case insensitive verification:

Code: Select all

If /I "%COMPUTERNAME%"=="MYSERVERNAME" GoTo :ERROR

chondro
Posts: 6
Joined: 19 Mar 2015 09:43

Re: Using %COMPUTERNAME% question

#8 Post by chondro » 27 Mar 2015 08:01

Thanks for the help guys.. I was able to get it to work by fixing the syntax, went out of the dos window, went back in, started my program.. :shock: weird.. appreciate it..

Post Reply