Solved

OS & Architecture specific Batch Scripting

Posted on 2011-09-29
10
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:ipremise
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:jan-janssen
Comment Utility
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
Comment Utility
Thanks jan-janssen. If I hadn't been so quick to mark the solution, I would have given you partial credit.
0
 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I made a request to reopen the thread....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now