Solved

Windows Batch script  conditional statement

Posted on 2014-04-14
3
536 Views
Last Modified: 2014-04-14
As you can see from the below code, I'm trying to work out which OS a machine is using and then based on this do different things.  However, for some reason none of the if statements are executing their conditions.

When printing the contents of %osname% I noticed that there is a space after the string, which is why I've added that into the condition, but it didn't work either.

@ECHO off

REM set variables
SET /P comp= Computer:
SET /P user= username:

SET cstring=/node:"%comp%"
SET osname=

ECHO Computer Selected = %comp%
ECHO Username Selected = %user%

Pause

FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% os get Name /value') do SET osname=%%A
FOR /F "tokens=1 delims='|'" %%A in ("%osname%") do SET osname=%%A

ECHO ----------------
ECHO Operating System: %osname%
ECHO ----------------

pause

if %osname% == "Microsoft Windows XP Professional " (
  GOTO ver_xp
)

if %osname% == "Microsoft Windows 7 Professional " (
  GOTO ver_7
)

if %osname% == "Microsoft® Windows VistaT Business " (
  GOTO ver_vista
)

:ver_xp
echo blah blah A
PAUSE

:ver_7
echo blah blah B
PAUSE

:ver_vista
echo blah blah C
PAUSE

EXIT

Open in new window

0
Comment
Question by:peterseychelles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39999242
You need to include the delimiters to the var, too:
if "%osname%" == "Microsoft Windows XP Professional " goto ver_xp

Open in new window

There is another issue, but I reckon that is because of your simplification: The individual parts should have a  goto :EOF or exit /b to stop execution, else you run also the other, following parts (i.e. for XP, the W7 and Vista stuff).
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39999247
On another note, it is good style to NOT use GOTO. If it isn't too much of action you have to perform for each OS type, do it directly within the IF parenthesis.
0
 

Author Closing Comment

by:peterseychelles
ID: 39999351
You sir are a flipping hero, it would have never occurred to me to encapsulate the variable like that.

If I was writing in any other language I wouldn't touch goto statements at all, in this instance the stuff being executed in each section is quite substantial so it will do for my needs.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most frustrating experiences a help desk technician will ever encounter is when a customer comes to them with a solution of their own invention and expects the tech to implement it. This often happens when people with a little bit of tech…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question