Solved

Windows Batch script  conditional statement

Posted on 2014-04-14
3
530 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 68

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now