Solved

OS & Architecture specific Batch Scripting

Posted on 2011-09-29
10
300 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer default details view 10 82
No module found pypyodbc, 3 32
Copy 20 files at a time from drive G to drive H 10 49
Restore get-aduser functionality 8 10
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

856 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