?
Solved

Windows Batch script  conditional statement

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

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 70

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses

765 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