[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Windows Batch script  conditional statement

Posted on 2014-04-14
3
Medium Priority
?
542 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

872 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