Link to home
Start Free TrialLog in
Avatar of alisafia
alisafia

asked on

skip computername

I have server1, server2, server3.

if /i %computername:~5,7% == server goto end

:end

Is this the right syntax?
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

if your servers are named: 'SERVER1', 'SERVER2' and 'SERVER3' then to get just the 'SERVER' part of the name you need the following:

if "%computername:~0,6%"=="SERVER" goto end

  ::
  ::
  ::

:end

Open in new window

If you think case-sensitivity may be an issue then you could throw in the '/i' switch just as you've done in the question.


You may also use the 'EQU' operator like this:

if "%computername:~0,6%" equ "SERVER" goto end

  ::
  ::
  ::

:end

Open in new window

Either way, it's always best to use double-quotes around both Lvalue and Rvalue.


BTW, if all you want to do is end the program if the IF-condition is true the you can simply do this:

if "%computername:~0,6%"=="SERVER" goto :eof

  ::
  ::
  ::

Open in new window

Thee's no need to actually have a label named ':eof' as it's a system 'end'of file' marker.
Avatar of alisafia
alisafia

ASKER

if /i "%computername:~5,7%" == "TEN" goto dontrun

net localgroup Administrators groupa /add
net localgroup Administrators groupb /add

:dontrun

This is what I have and the batch script is adding the groups on PC where I don't want to.
If you want to test for the entire name 'SERVER1' then you need:

    if "%computername%"=="SERVER1" goto end
the pc name is HKG-TEN-001.
I don't want to enter all of PC name in the script.
If the computer name is 'HKG-TEN-001' and you want to test for 'TEN' then you need this:

  if "%computername:~4,3%" == "TEN" goto dontrun


The '4' is the starting position of 'TEN' (because the 'T' is the 4th character-position in the name. Remember we're counting from zero onwards so, the 1st character 'H' is at character-position '0' and so on).

The '3' is the length of 'TEN' ie, the phrase 'TEN' is comprised of 3 characters and is therefore 3 characters long (or wide).
NOTE: if the 'HKG' part of 'HKG-TEN-001' varies in length from one name to another then you could regard the 'HKG', 'TEN' and '001' as three distinct parts.

So, if you had the following names:

   HKG-TEN-001
   HKG2-NINE-005
   HG-FOURTEEN-0007

Then the following code will find 'TEN', 'NINE' and 'FOURTEEN' providing the format is consistent (each part is seperated as they are with a hyphen '-'). For example, to test for 'TEN' you vould do this:

for /f "tokens=2 delims=-" %%a in ("%computername%") do if "%%a"=="TEN" goto :eof

To better illustrate this, set any variable to any of the following names and test it with the same line of code:

   HKG-TEN-001
   HKG2-NINE-005
   HG-FOURTEEN-0007

Copy & paste this code into a batch file and run it to see the results:

@echo off

set variable=HKG-TEN-001
for /f "tokens=2 delims=-" %%a in ("%variable%") do echo %%a

set variable=HKG2-NINE-005
for /f "tokens=2 delims=-" %%a in ("%variable%") do echo %%a

set varioable=HG-FOURTEEN-0007
for /f "tokens=2 delims=-" %%a in ("%variable%") do echo %%a

Open in new window

You'll notice it displays:

    TEN
    NINE
    FOURTEEN

Notice lines 4, 7 and 10 are the same though.
Avatar of Steve Knight
BTW if it is of interest you can do without the goto method using ( ) to include the commands, e.g.

if not /i "%computername:~4,3%" == "TEN" (
  net localgroup Administrators groupa /add
  net localgroup Administrators groupb /add
) ELSE (
  echo Do something else
)
echo Done for all

Another way if you want to see if there is the word TEN anywhere in a name for instance is:

if /i %computername%==%computername:TEN=% (
  echo There is not a TEN somewhere in the name
) ELSE (
  echo There is a TEN in the name
)
dragon-it (Steve)

Regarding your last example, it's better to do it like this:

if /i "%computername%"=="%computername:-TEN-=%" (
  ::
  :: TEN is NOT present
  ::
) else (
  ::
  :: TEN IS present
  ::
)

Open in new window

Notice the '-TEN-' instead of just the 'TEN'. This is incase you come up against something like 'BETTEND-TEN-001'. See how 'TEN' occurs twice here (BETTEND-TEN-001)?

And what about here: BETTEND-TWO-001? This would cause havok in your code.
I will try it on Monday I am in transit. I think I like idea doing if not.
Completely agreed there Paul, was just giving. Example, mainly of using () syntax for him.
ASKER CERTIFIED SOLUTION
Avatar of Paul Tomasi
Paul Tomasi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know Steve... you're usually quite thorough...
I have used this and it works great.

if /i not "%computername:~4,3%"=="TEN" (
  net localgroup Administrators groupa /add
  net localgroup Administrators groupb /add
)