Solved

OS & Architecture specific Batch Scripting

Posted on 2011-09-29
10
304 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 82

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
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…
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.
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.
Suggested Courses

628 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