Solved

Need to run a batch file with IF statement

Posted on 2008-10-25
14
500 Views
Last Modified: 2012-05-05
Hi all,

hope you'all having a great weekend.

so here's this weekend question :)

I have a batch script and there some parts of it that needs to run based  on windows version. so I'll like to say "if windows version=windows server 2003 goto W2K3"

I just couldn't find a way or command to determine the running windows version.

can you please help me out with this issue?

Thanks
0
Comment
Question by:Medquest
[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
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 10

Expert Comment

by:Alan_White
ID: 22803489
Have you played around with VER?
 
This will give you the string of the installed OS, you could use that in your IF statement.
0
 

Author Comment

by:Medquest
ID: 22803598
I am checking it out right now and as of yet I am not sure who am I going to use it with IF, but I will see if I can use it with errorlevel.

will keep you posted.

Thanks
0
 
LVL 10

Expert Comment

by:Alan_White
ID: 22803707
Yeah, you might want to use FIND and pipe in the results from VER then use errorlevel to trigger your action.
0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 22803724
Server 2003 is version 5.2 - see the series of commands below.

C:\Documents and Settings>ver | find /i "Version 5.2"
Microsoft Windows [Version 5.2.3790]

C:\Documents and Settings>echo %errorlevel%
0

C:\Documents and Settings>ver | find /i "Version 5.1"

C:\Documents and Settings>echo %errorlevel%
1

C:\Documents and Settings>
0
 

Author Comment

by:Medquest
ID: 22803926
This is a great idea :)

I am heading out right now, because I am going to take one of the tests to my MCITP test 70-238 and I will try this idea as soon I am get home.

Thanks for all the responses.
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22804550
Two options for you:

for /f "tokens=1 delims=[" %%i in ('ver') do set winver=%%i

On a Windows 2000 workstation this sets winver to: Microsoft Windows 2000
On Windows XP: Microsoft Windows XP
On Vista (amazingly): Microsoft Windows

for /f "tokens=2-3 delims=[." %%i in ('ver') do set winver=%%i.%%j

On a Windows 2000 workstation: Version 5.00
On Windows XP: Version 5.1
On Vista: Version 6.0

I don't have a server handy right now so you will have to see what you get on the kinds of machines you're interested in .

0
 

Author Comment

by:Medquest
ID: 22804940
I like the second option.

so would my batch script start with soemthing like this:
for /f "tokens=2-3 delims=[." %%i in ('ver') do set winver=%%i.%%j
if winver=Version 5.00 goto blah1
if winver=Version 5.1 goto blah2
if winver=Version 6.0 goto blah3
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22805176
Well, you need to dereference the environment variable with percent signs, and quotes are advisable in case you unexpectedly get a blank string back, so

IF "%winver%" EQU "Version 5.00" GOTO blah1

etc


0
 
LVL 96

Expert Comment

by:Lee W, MVP
ID: 22805268
why not explain your complete purpose so we can provide a complete answer?
0
 

Author Comment

by:Medquest
ID: 22805272
here's the one I tried:

====================================
ver
IF "%ver%" EQU "Version 5.0" goto Win2K
IF "%ver%" EQU "Version 5.1" goto WinXP
IF "%ver%" EQU "Version 6.0" goto WinVista

Pause
:Win2K
Echo this computer is running windows 2000
Pause
Exit
:WinXP
Echo this computer is running windows XP
Pause
Exit
:WinVista
Echo this computer is running windows vista
Pause
Exit
====================================
and I am getting the result :(


C:\>ver
Microsoft Windows XP [Version 5.1.2600]
C:\>IF "" EQU "Version 5.0" goto Win2K
C:\>IF "" EQU "Version 5.1" goto WinXP
C:\>IF "" EQU "Version 6.0" goto WinVista
C:\>Pause
Press any key to continue . . .
 
0
 
LVL 5

Accepted Solution

by:
PaulKeating earned 500 total points
ID: 22805290
ver
IF "%ver%" EQU "Version 5.0" goto Win2K

That won't work. It doesn't put the value you want in the environment variable VER. It just runs the command VER and sends the output to the screen. That won't magically assign a value to an environment variable. That needs an explicit SET statement.

Please follow the recipe exactly until you understand what it does. Instead of the first line, put

for /f "tokens=2-3 delims=[." %%i in ('ver') do set ver=%%i.%%j
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22805298
And by the way, for Win2K workstation you need 5.00 not 5.0 ... these are strings you are dealing with, not numbers. So "5.00" is different from "5.0", just as "ABCC" is different from "ABC".
0
 

Author Closing Comment

by:Medquest
ID: 31509949
That worked.

you're my hero.

Thanks a lot
0
 

Author Comment

by:Medquest
ID: 23555593
PaulKeating:
I need a favor from you.
how can I make this line works with systeminfo insteac of ver to get the service pack level on a windows XP.
what changes need to be made to get this result?
 
I appreciate.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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