If-then help

June 24, 2009 at 05:28:13
Specs: Windows XP
I'm trying figure out how to do an If-Then script. This has to be done using basic commands found in XP. I can't add programs.
It's been years since I've done any command line programing.
I need to run a command (netsh), look for a specific string in the output and then make a decision if the string is found.
netsh interface ip show config results in

Configuration for Interface "Local Area Connection 2"
.....
.....
....
....

Configuration for Interface " Local Area Connection"


If this result contains the "Local Area Connection 2" then I run one set of command else I run a different set of commands.

I've managed to get the FOR command to work. Well sort of.

*******************************Command file**************
set test2=netsh interface ip show config

For /f "tokens=1 Delims= "%%J IN ('%test2% ^|find "Local Area Connection 2"') Do echo reult is %%J.
***************************end command file.
This results in the whole line Configuration for Interface "Local Area Connection 2" being displayed.
It would nice to get Just "Local Area Connection 2" to be equal to %%J. Is this done with the Delims and tokens? Idealy I would have 4 tokens witht the last being the part between the quotes.

Now I need to creat the If-Then statement Making the decision based on whether "Local Area Connection 2" was found.

Sorry for the long message but I figure the more I put the easier it is for people to understand what I'm looking to do.

Wedge


See More: If-then help

Report •


#1
June 24, 2009 at 06:44:33
According to what you posted with "tokens=1 delims= " you should get just Configuration an no more from your For loop.

Anyway if you just need to select a sequence based on the result of netsh

netsh interface ip show config | find "Local Area Connection 2" > nul
if ErrorLevel 1 (
  Here the commands for NOT FOUND
) else (
  HERE YES FOUND
)

You can insert how many commands you want, but NOT goto statements jumping to a label inside the IF. More you can't set variables and then refer to them INSIDE the IF. To do so code at the beginning of your batch

  @echo off & setlocal EnableDelayedExpansion

and then mark variables using !, e.g.

set myVar=Hello
echo.!myVar!


Report •

#2
June 24, 2009 at 08:22:05
Here is the cmd file I wrote. IP address are fake to protect info


netsh interface ip show config |find "Local Area Connection 2">null
IF ErrorLevel 1 (netsh interface ip set address name="Local Area Connection" static x.x.x.x 225.255.255.0
rsh x.x.x.x -l admin admin SQL Update command set command=on
netsh interface ip set address name=Local Area Connection" static y.y.y.y 225.255.255.0)
Else(netsh interface ip set address name="Local Area Connection 2" static x.x.x.x 255.255.255.0
rsh x.x.x.x -l admin admin SQL Update command set command=on
netsh interface ip set address name="Local Area Connection 2" static y.y.y.y 255.255.255.0)

This exactly as written in the cmd file. Including the Carriage Returns. I get an error Else is not a recognized as an internal or external
command, operable program or batch file.

Wedge


Report •

#3
June 24, 2009 at 08:37:50
Try this
===========================
netsh interface ip show config |find "Local Area Connection 2">null
IF ErrorLevel 1 (

netsh interface ip set address name="Local Area Connection" static x.x.x.x 225.255.255.0
rsh x.x.x.x -l admin admin SQL Update command set command=on
netsh interface ip set address name=Local Area Connection" static y.y.y.y 225.255.255.0

) Else (

netsh interface ip set address name="Local Area Connection 2" static x.x.x.x 255.255.255.0
rsh x.x.x.x -l admin admin SQL Update command set command=on
netsh interface ip set address name="Local Area Connection 2" static y.y.y.y 255.255.255.0
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 24, 2009 at 08:41:25
netsh interface ip show config | find "Local Area Connection 2" > nul
IF ErrorLevel 1 (
  netsh interface ip set address name="Local Area Connection" static x.x.x.x 225.255.255.0
  rsh x.x.x.x -l admin admin SQL Update command set command=on
  netsh interface ip set address name=Local Area Connection" static y.y.y.y 225.255.255.0
) ELSE (
  netsh interface ip set address name="Local Area Connection 2" static x.x.x.x 255.255.255.0
  rsh x.x.x.x -l admin admin SQL Update command set command=on
  netsh interface ip set address name="Local Area Connection 2" static y.y.y.y 255.255.255.0
)

You do not follow the syntax I posted that MUST be strictly replicated as software doesn't allow tolerance.

By the way this is NT batch as there is no DOS in Windows XP.


Report •

#5
June 24, 2009 at 08:53:15

Reformated and it seems to work.. Wont know till I have it all hooked up into the lab.
The script puts a null file on the desktop, can I prevent this or delete when done? Will @echo off fix this?
Thanks. I have a scripting book come from Amazon, maybe I'll be able to learn exactly what the heck I did someday.
Wedge

Report •

#6
June 24, 2009 at 09:02:19
What I have posted (and M2 too) works correctly.

CRs are nothing to do with your mistakes.

You have to code an IF (...) ELSE (...) as

IF condition (
STATEMENTS
) ELSE (
STATEMENTS
)

not

IF condition (STATEMENTs)
ELSE (STATEMENTS )

Review again my post #4.


Report •

#7
June 24, 2009 at 09:16:03
See message 5

Report •

#8
June 24, 2009 at 12:52:49
You have to redirect the output of FIND to nul NOT null.

If you inspected accurately my post #4 you did got it. Nul is the standard "black hole" for Windows output while null (with TWO ll) is just a (unwanted) file.

@echo off (to be coded as the first statement of the batch) suppresses the command echo on the screen and is used to shape a friendly output. It must be removed however while debugging.

CRs, spaces and even chars must be carefully evaluated when you are coding software: to be close is not allowed.


Report •


Ask Question