• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

skip computername

I have server1, server2, server3.

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

:end

Is this the right syntax?
0
alisafia
Asked:
alisafia
  • 7
  • 5
  • 2
1 Solution
 
paultomasiCommented:
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.
0
 
alisafiaAuthor Commented:
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.
0
 
paultomasiCommented:
If you want to test for the entire name 'SERVER1' then you need:

    if "%computername%"=="SERVER1" goto end
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
alisafiaAuthor Commented:
the pc name is HKG-TEN-001.
0
 
alisafiaAuthor Commented:
I don't want to enter all of PC name in the script.
0
 
paultomasiCommented:
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).
0
 
paultomasiCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
)
0
 
paultomasiCommented:
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.
0
 
alisafiaAuthor Commented:
I will try it on Monday I am in transit. I think I like idea doing if not.
0
 
Steve KnightIT ConsultancyCommented:
Completely agreed there Paul, was just giving. Example, mainly of using () syntax for him.
0
 
paultomasiCommented:
Yep, the proper syntax is as follows:

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

Open in new window

Or you could do this:

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

Open in new window

Personally, if I was doing this for myself I would do it like this (just a single command):

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

Open in new window

0
 
paultomasiCommented:
I know Steve... you're usually quite thorough...
0
 
alisafiaAuthor Commented:
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
)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now