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
Solved

Windows Batch script  conditional statement

Posted on 2014-04-14
3
535 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Learn how ViaSat reduced average response times for IT incidents from 10 minutes to 30 seconds.
Are client relationship the only driver of a successful MSP? While important, client relationships are only one component. Learn how else MSPs can broaden their horizon and differentiate themselves.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

839 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