alisafia
asked on
skip computername
I have server1, server2, server3.
if /i %computername:~5,7% == server goto end
:end
Is this the right syntax?
if /i %computername:~5,7% == server goto end
:end
Is this the right syntax?
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.
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
if "%computername%"=="SERVER1
ASKER
the pc name is HKG-TEN-001.
ASKER
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).
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:
TEN
NINE
FOURTEEN
Notice lines 4, 7 and 10 are the same though.
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
You'll notice it displays:TEN
NINE
FOURTEEN
Notice lines 4, 7 and 10 are the same though.
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%==%computern ame:TEN=% (
echo There is not a TEN somewhere in the name
) ELSE (
echo There is a TEN in the name
)
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%==%computern
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:
And what about here: BETTEND-TWO-001? This would cause havok in your code.
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
::
)
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I know Steve... you're usually quite thorough...
ASKER
I have used this and it works great.
if /i not "%computername:~4,3%"=="TE N" (
net localgroup Administrators groupa /add
net localgroup Administrators groupb /add
)
if /i not "%computername:~4,3%"=="TE
net localgroup Administrators groupa /add
net localgroup Administrators groupb /add
)
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:
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:
Open in new window
Thee's no need to actually have a label named ':eof' as it's a system 'end'of file' marker.