checking for variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In our logon script I would like to check for the presense of two servers as an expample. i.e. if %computername%=server1 or server2... How should this line be formatted? Thank you for your help..

L
 
If you just want to hardcode it like that, you can do just that.

IF %COMPUTERNAME%==SERVER1 (GOTO :EOF)
IF %COMPUTERNAME%==SERVER2 (GOTO :EOF)
echo The computer is not one of those two.

Ray at work

lwg said:
In our logon script I would like to check for the presense of two servers
as an expample. i.e. if %computername%=server1 or server2... How should this
line be formatted? Thank you for your help...
 
lwg said:
In our logon script I would like to check for the presense of two servers
as an expample. i.e. if %computername%=server1 or server2... How should this
line be formatted? Thank you for your help...

Unfortunately batch files don't have an OR statement, so it's not simple.

You could do something like this:

SetLocal
Set RESULT=1
If /I "%COMPUTERNAME%" EQU "server1" Set RESULT=0
If /I "%COMPUTERNAME%" EQU "server2" Set RESULT=0
If %RESULT% EQU 0 (
REM This is server1 or server2
) Else (
REM This is not server1 or server2
)


Or you could use FINDSTR.EXE with the ^ and $ regular expressions:

(the first line is long, it might get line-wrapped)

Echo %COMPUTERNAME%|FindStr.exe /r /i "^server1$ ^server2$">NUL
If %ERRORLEVEL% EQU 0 (
REM This is server1 or server2
) Else (
REM This is not server1 or server2
)
 
Ray is there a way to hard code it on one line? Just curious...

thx L

----- Ray at <%=sLocation%> wrote: -----

If you just want to hardcode it like that, you can do just that.

IF %COMPUTERNAME%==SERVER1 (GOTO :EOF)
IF %COMPUTERNAME%==SERVER2 (GOTO :EOF)
echo The computer is not one of those two.

Ray at work

lwg said:
In our logon script I would like to check for the presense of two servers
as an expample. i.e. if %computername%=server1 or server2... How should this
line be formatted? Thank you for your help...
 
Two methods I can think of:

IF %COMPUTERNAME%==SERVER1 (GOTO :NEXTSTEP) ELSE IF %COMPUTERNAME%==SERVER2
(GOTO :EOF)
-or-
FOR /D %%A IN (SERVER1 SERVER2) DO IF %COMPUTERNAME%==%%A GOTO :EOF

Wayne Chesley
Bowdoin College




lwg said:
In our logon script I would like to check for the presense of two servers
as an expample. i.e. if %computername%=server1 or server2... How should this
line be formatted? Thank you for your help...
 
lwg said:
Ray is there a way to hard code it on one line? Just curious...

thx L

----- Ray at <%=sLocation%> wrote: -----

If you just want to hardcode it like that, you can do just that.

IF %COMPUTERNAME%==SERVER1 (GOTO :EOF)
IF %COMPUTERNAME%==SERVER2 (GOTO :EOF)
echo The computer is not one of those two.

Ray at work


as an expample. i.e. if %computername%=server1 or server2... How should this
line be formatted? Thank you for your help...

if "%computername%" NEQ "server1" if "%computername%" NEQ "server2" goto :NEITHER
echo/if you got here, then 'computername' is either 'server1' or 'server2'
:
:
:
:NEITHER
 
In our logon script I would like to check for the presense of two servers as an expample.
i.e. if %computername%=server1 or server2... How should this line be formatted?
Thank you for your help...

echo " server1 server2 " | find /i "%computername%" > nul && echo
found || echo not found

Garry
 
Garry Deane said:
echo " server1 server2 " | find /i "%computername%" > nul && echo
found || echo not found

Nice one, but try it in the case of the computername being "server" or
"erver1". I think you need delimiters also in the search string as in:

echo " server1 server2 " | find /i " %computername% " > nul && echo found ||
echo not found


/Al
 
Nice one, but try it in the case of the computername being "server" or
"erver1". I think you need delimiters also in the search string as in:

echo " server1 server2 " | find /i " %computername% " > nul && echo found ||
echo not found

Yes, good point.

Garry
 
Back
Top