Solved

OS & Architecture specific Batch Scripting

Posted on 2011-09-29
10
301 Views
Last Modified: 2012-05-12
I have two different but similar batch scripts that attempt to find the PC's OS or architecture (x86 or x64) and run commands based on the what is determined. The scripts need to work on XP, Vista, and 7 machines.

Here is the first:
 
systeminfo | find "OS Name" > %USERPROFILE%\desktop\osname.txt

FOR /F "tokens=5" %%A IN (%USERPROFILE%\desktop\osname.txt) DO SET vers = %%A
IF "%vers%" == "7" (
IF "%vers%" == "Vista" (
GOTO :vista
) 
) 
GOTO :vista
ELSE (
GOTO :xp
)

:vista 
echo vista success!
pause
exit

:xp 
echo xp success!
pause
exit

Open in new window


Here is the second:
systeminfo | find /I "System Type" > "%USERPROFILE%\desktop\architecture.txt"

FOR /F "tokens=3" %%A IN (%USERPROFILE%\desktop\architecture.txt) DO SET type = %%A
IF "%type%" == "x64-based" (
GOTO :vista
) ELSE (
GOTO :xp
)

:vista
echo 64success!
pause
exit

:xp
echo 32success!
pause

Open in new window


The trouble seems to be with my IF,ELSE statements. Getting these working would help me a ton!

Thanks.
0
Comment
Question by:ipremise
[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
10 Comments
 
LVL 66

Accepted Solution

by:
johnb6767 earned 100 total points
ID: 36817303
I do something similar.....

set type=64
systeminfo | find /i "x86-based" && set type=32

That way ....

if %type%==32 ACTION
0
 
LVL 1

Author Comment

by:ipremise
ID: 36817555
Thanks! That definitely is a simpler code than what I had and works great; though I needed to put %type% in quotes as well as the trailing 32.

Now I just need to figure out the OS name script. I'm guessing it could work the same way though; e.g. -

set vers=7
systeminfo | find /i "Windows XP" && set vers=xp

if "%vers%"=="xp" COMMAND

But that leaves out Vista...
0
 
LVL 66

Expert Comment

by:johnb6767
ID: 36818123
ver | find /i "Version 6.1.7600" && set vers=7
ver | find /i "Version 6.0.0000" && set vers=vista     <~~~~ I don't have a Vista machine to verify the result of the ver command.... You get the idea though....
ver | find /i "Version 5.1.2600" && set vers=xp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ipremise
ID: 36818139
The OS name script does work in a similar manner. Here's what I got to work:
 
set vers=7
systeminfo | find /i "Windows XP" && set vers=xp

IF "%vers%"=="xp" (
GOTO :xp
) ELSE IF "%vers%"=="7" ( 
GOTO :vista
) ELSE (
GOTO :vista
)

:vista 
echo vista success!
pause
exit

:xp 
echo xp success!
pause
exit

Open in new window


Thanks johnb6767!
0
 
LVL 9

Expert Comment

by:jan-janssen
ID: 36818143
Here is a full solution for your issue:

REM Check Windows Version
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_XP
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_Win7
goto warn_and_exit

:ver_Win7
:Run Windows 7 specific commands here
REM echo OS Version: Windows 7 (debug line)
goto end

:ver_Vista
:Run Windows Vista specific commands here
REM echo OS Version: Windows Vista (debug line)
goto end

:ver_XP
:Run Windows XP specific commands here
REM echo OS Version: Windows XP (debug line)
goto end

:warn_and_exit
echo Machine OS cannot be determined.

:end  

Open in new window


 As seen on: http://www.grimadmin.com/article.php/batchfile-easy-way-to-detect-os-version
0
 
LVL 1

Author Comment

by:ipremise
ID: 36818256
Thanks jan-janssen. If I hadn't been so quick to mark the solution, I would have given you partial credit.
0
 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 25 total points
ID: 36818262
better to just use:

 ver | find /i "Version 6.1" && set vers=7
ver | find /i  "Version 6.0" && set vers=Vista
ver | find /i "Version 5.1" && set vers=xp

as Windows 7 sp1 is minor version 7601
and vista sp2 is minor version 6002

here is a good list but not comprhensive as it does not include server versions

0
 
LVL 66

Expert Comment

by:johnb6767
ID: 36818396
Feel free to use the Request Attention link above and request the thread to be reopened,....

I assumed you had the majority, was only trying to help fill in the missing bits....
0
 
LVL 66

Expert Comment

by:johnb6767
ID: 36819030
I made a request to reopen the thread....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…

749 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