Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OS & Architecture specific Batch Scripting

Posted on 2011-09-29
10
Medium Priority
?
307 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
10 Comments
 
LVL 66

Accepted Solution

by:
johnb6767 earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 84

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 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

Independent Software Vendors: 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!

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…
Unable to change the program that handles the scan event from a network attached Canon/Brother printer/scanner. This means you'll always have to choose which program handles this action, e.g. ControlCenter4 (in the case of a Brother).
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
Suggested Courses

886 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