Computing.Net > Forums > Programming > If-then help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

If-then help

Reply to Message Icon

Name: wedge40
Date: June 24, 2009 at 05:28:13 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: June 24, 2009 at 06:44:33 Pacific
Reply:

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!


0

Response Number 2
Name: wedge40
Date: June 24, 2009 at 08:22:05 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: June 24, 2009 at 08:37:50 Pacific
Reply:

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


0

Response Number 4
Name: IVO
Date: June 24, 2009 at 08:41:25 Pacific
Reply:

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.


0

Response Number 5
Name: wedge40
Date: June 24, 2009 at 08:53:15 Pacific
Reply:


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


0

Related Posts

See More



Response Number 6
Name: IVO
Date: June 24, 2009 at 09:02:19 Pacific
Reply:

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.


0

Response Number 7
Name: wedge40
Date: June 24, 2009 at 09:16:03 Pacific
Reply:

See message 5


0

Response Number 8
Name: IVO
Date: June 24, 2009 at 12:52:49 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: If-then help

PLEASE!! Codewarrior/ C++ HELP!!! www.computing.net/answers/programming/please-codewarrior-c-help/6209.html

excel...if or vlookup? www.computing.net/answers/programming/excelif-or-vlookup/7872.html

ASP If Syntax - Help! www.computing.net/answers/programming/asp-if-syntax-help/10689.html