Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I have been searching for a way to detect Windows Vista using a batch script for a while now and below is what I found so I thought I would share for those that need this :)
VER | findstr /i "5.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running 2000VER | findstr /i "5.1." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running XPVER | findstr /i "5.2." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Win 2003VER | findstr /i "6.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running VistaThis searches for the build number in the ver string. If anyone has a better way of doing this then I would love to hear your thoughts.

This might be easier for the n00bs to understand :p
@echo off
VER>Version.tmp
ECHO You are running:
TYPE Version.tmp
DEL Version.tmp
PAUSE >nulSee how much simpler ^^ that is?
I only Batch if possible, 2000 more lines of code, oh well.

Well, Batchfreak,
that way the following script is even
simpler, faster and produces similiar result
as yours :-) :@echo You are running:
@ver
@pause
I think what CR wanted is to determine the os
within a batch file programmatically and do
different operations based on OS version.--
Holla.

I'm not sure if this will work for everyone, but it seems to work for me in xp:
for /f "tokens=3 delims=[ " %%a in ('call cmd /c "cmd<nul"^|find /i "windows"') do ECHO Running %%a

Yup Holla thats what I was wanting to do. However, I have found a problem... I am running Windows XP Pro here yet my script gives the following output:
Running XP
Running VistaHmmm...

he can searxh for the word XP, or VISTA instead of the number.
I only Batch if possible, 2000 more lines of code, oh well.

@echo off
VER>Version.tmp
FOR /F "Tokens=1 Delims=[" %%a IN (version.tmp) DO SET VERsion=%%a
SET version=%version:~10%
ECHO %version%
DEL Version.tmp
PAUSE >nulThis might work better ^^ it hasn't told mee Im running Vista :p
I only Batch if possible, 2000 more lines of code, oh well.

But how to use your code with mine so that depending on the OS it detects, it lets you goto another section of the batch...
For example, say I have an application that is launched when running XP but skipped when running Vista or vice-versa...
This is what I am using when needing to skip a Vista only application:
REM If XP then skip
VER | findstr /i "5.1." > nul
IF %ERRORLEVEL% EQU 0 GOTO END
CALL System_Maint\TweakUAC.exe
GOTO END
:ENDor this when needing to skip an XP only application:
REM If Vista then skip
VER | findstr /i "6.0." > nul
IF %ERRORLEVEL% EQU 0 GOTO END
CALL System_Maint\StartUpLite.exe
GOTO END
:END

If the layout is consistent:
===========================================
@echo off & setLocal EnableDelayedExpansionfor /f "tokens=5 delims= " %%a in ('ver') do (
set nn=%%a
set nn=!nn:~0,3!
)goto :!nn!
:5.0
echo 5.0
goto :eof:5.2
echo 5.2
=====================================
If at first you don't succeed, you're about average.M2

The layout is not consistent! *&@^#%$!
c:\holla>verMicrosoft Windows [Version 6.0.6001]
--
Holla.

Hmmm, didn't you say you were running XP too Holla?
I have just opened CMD and done a ver with the following results:
Microsoft Windows XP [Version 5.1.2600]
As for being consistent, well it kinda isn't... Think a list of applications one after another opening with a simple CALL command - some are for Vista only while others are for XP :)
Fun little problem isn't it... However, I guess I could use your code Mechanix2Go just before each application depending on what I needed but this would mean a lot of repeated code.
Couldn't I set VAR's at the start of my batch for say VERXP and VERVISTA then add a check to each section just before the application is called?

CR,
1. I am running Vista.
2. I still could not figure out why your
initial script is giving XP and Vista both as
output.
3. Is there anything else in Vista
environment that tells it is vista? if then
instead of checking version, we can check the
environment variable.
Here is my vista env:
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Holla\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=ACROPETAL-HBE
ComSpec=C:\Windows\system32\cmd.exe
DFSTRACINGON=FALSE
EMC_AUTOPLAY=C:\Program Files\Common Files\Roxio Shared\
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=C:\holla
LOCALAPPDATA=C:\Users\Holla\AppData\Local
LOGONSERVER=\\ACROPETAL-HBE
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;C:\Program Files\Microsoft Network Monitor 3\;C:\Program Files\Common Files\Roxio Shared\11.0\DLLShared\;c:\holla\bat;c:\holla\util;C:\Windows\system32\WindowsPowerShell\v1.0\;C:\holla\bat;C:\holla\util
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PSC1
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0f0d
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PROMPT=$t$H$H$H$M$+$p$g
PUBLIC=C:\Users\Public
RCAUTOPLAY=C:\Program Files\Roxio Creator 2009 Ultimate\Roxio Central 4\
RoxioCentral=C:\Program Files\Common Files\Roxio Shared\9.0\Roxio Central33\
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\Holla\AppData\Local\Temp
TMP=C:\Users\Holla\AppData\Local\Temp
TRACE_FORMAT_SEARCH_PATH=\\NTREL202.ntdev.corp.microsoft.com\4F18C3A5-CA09-4DBD-B6FC-219FDD4C6BE0\TraceFormat
USERDOMAIN=ACROPETAL-HBE
USERNAME=Holla
USERPROFILE=C:\Users\Holla
windir=C:\Windows--
Holla.

Ok I think I may have cracked it - how about this:
@echo off
if "%allusersprofile%"=="%systemdrive%\ProgramData" goto WinVista
if %os%==Windows_NT goto WinXPecho If you see this then assume Win 9x base
goto EndCheck:WinVista
echo Windows Vista Operating System Detected
goto EndCheck:WinXP
echo Windows XP Operating System Detected:EndCheck
pauseIt simply checks for %systemdrive%\ProgramData and if it finds it then we know the user is using Vista as this path does not exist within XP :)
It will only detect between Windows Vista and XP but I think that will be ok for most users.

Ooh and I finally answered one of my own questions lol - your huge brains must be rubbing off on me :P

Its all it takes :) Soon you'll be able to do this telepathically :p
Wouldn't it be easier to set a variable?
I only Batch if possible, 2000 more lines of code, oh well.

Oh god here we go lol - this is how it starts huh :P
I thought use a var earlier and even said it but then got my thinking head on and did it another way... as is mostly always the case :)
Care to start me off?

I gave you the code :p
This is the code to set the %version% as your operating system:
VER>Version.tmp
FOR /F "Tokens=1 Delims=[" %%a IN (version.tmp) DO SET VERsion=%%a
SET version=%version:~10%
DEL Version.tmpI only Batch if possible, 2000 more lines of code, oh well.

So if I set the OS as a VAR at the start of my batch then whenever I need to do a check just do an if ver=%osvar% then goto skip.... ? I can see how it would work but am struggling to put it into practice with in my batch.
Example batch layout
find current OS and set as OSVAR
...
...
...
if ver=%osvar% then goto skip
echo Launching some program
call some program here
goto done:skip
echo Skipped, incorrect OS
goto end:done
echo Finished:end
pause
...
...
...

Like this... :p
VER>Version.tmp
FOR /F "Tokens=1 Delims=[" %%a IN (version.tmp) DO SET VERsion=%%a
SET version=%version:~10%
DEL Version.tmpIF NOT %version%==Windows XP GOTO Vista
XP PROGRAM
END:VISTA
VISTA PROGRAM
ENDI only Batch if possible, 2000 more lines of code, oh well.

Ahh nice one - I see now thanks :)
and I have just gone through my entire batch using the following to either run/skip depending on Vista/XP too...
REM Schedule Defragmentation of Pagefile during next reboot
if "%allusersprofile%"=="%systemdrive%\ProgramData" goto VISTA_DETECTED
if %os%==Windows_NT goto VISTEA_NOT_DETECTED:VISTA_NOT_DETECTED
CALL System_Maint\Pagedfrg.exe -o -t 3 > NUL
:VISTEA_DETECTED

Now updated to include your code, IE:
REM Schedule Defragmentation of Pagefile during next reboot
IF NOT %version%==Windows XP GOTO Vista
CALL System_Maint\Pagedfrg.exe -o -t 3 > NUL
:VISTAThe VER of OS was set at the start of my batch with the above code being called as and when I needed to 'skip' a section depending on the client OS :)
Can't test it tho as I don't have vista... Any offers :)

CR,
Here you go.
Does not work as expected.c:\holla\bat\t>vertc:\holla\bat\t>FOR /F "Tokens=1 Delims=[" %a IN ('ver') DO SET VERsion=%a
c:\holla\bat\t>SET VERsion=Microsoft Windows
c:\holla\bat\t>SET version=Windows
c:\holla\bat\t>echo version=Windows
version=Windowsc:\holla\bat\t>IF NOT Windows == Windows XP GOTO Vista
c:\holla\bat\t>echo in XP
in XPc:\holla\bat\t>goto :eof
My batch file vert.bat looks like this:FOR /F "Tokens=1 Delims=[" %%a IN ('ver') DO SET VERsion=%%a
SET version=%version:~10%
echo version=%version%
IF NOT %version%==Windows XP GOTO Vista
echo in XP
goto :eof
:VISTA
Echo in VISTA PROGRAM
goto :eofI am not sure what is the intent of this line:
"IF NOT %version%==Windows XP GOTO Vista "
Did you mean WINDOWS_XP?
it will try to execute the command XP with parameters goto vista as arguements if version=windowsPost your complete batch file as it is if you want me to test under Vista.
--
Holla.

Guys,
To answer one of CR's earlier questions, the reason you are getting both Windows XP and Vista reported is that findstr is using regular expressions. When you search for "6.0" the dot matches any character. So on XP, VER returns 5.1.2600 and "6.0" matches the substring "600" in "5.1.2600".
Try using findstr /l instead of findstr /i.

:) That fixed it when running from It's own batch but what does /l do that /i doesn't? - sorry If I sound a bit thick but I am kind of new to this...
Also, when adding to my previous batch it just exits... Maybe you could advise why it does this.
Basically I need it to detect Windows XP and skip calling this app as it is only for Windows Vista. Here is my code:
VER | findstr /l "5.1." > nul
IF %ERRORLEVEL% EQU 0 GOTO RUNNING_XPCommands\CHOICE /N " Run Tweak UAC? (Vista) [Y/N] "
if errorlevel 2 goto NO
COLOR 6F
ECHO Executing, please wait...
CALL System_Maint\TweakUAC.exe
COLOR 2F
FOR /F "TOKENS=*" %%i in ('time /t') do set TIMET=%%i
ECHO Finished on %Date% at %TIMET%. && %R1%
goto end:RUNNING_XP
COLOR 4F
ECHO Tweak UAC does not support XP, skipping... && %R3%
goto end:NO
COLOR 4F
ECHO Skipped... && %R1%:end
COLOR 8F
pause

> what does /l do that /i doesn't?
/l means treat the strings as literal, and this option turns off regular expressions.
/i means ignore case, which in this case has absolutely no effect since we have no letters in our search string.>when adding to my previous batch it just exits
It doesn't when I try it:
C:\>type t.bat
@ECHO OFF
VER | findstr /l "5.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running 2000VER | findstr /l "5.1." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running XPVER | findstr /l "5.2." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Win 2003VER | findstr /l "6.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Vista
C:\>t
Running XPC:\>

Hi klint,
I'm glad it's not just me.
Why use findstr when what you need is find. And we won't even discuss /i when FINDing numbers. LOL
=====================================
If at first you don't succeed, you're about average.M2

Hi Klint, thanks for your answer but I was not refering to the original batch. Maybe it was my poor description but I wanted to know why the following exits when it detects Windows XP.
Top 'n' tail of it is I need to skip TweakUAC.exe if a user is using Windows XP as it is a Vista only application. However, it need to run through if the user has Windows Vista installed...
Anyhow, here is my code:
VER | findstr /l "5.1." > nul
IF %ERRORLEVEL% EQU 0 GOTO RUNNING_XPCommands\CHOICE /N " Run Tweak UAC? (Vista) [Y/N] "
if errorlevel 2 goto NO
COLOR 6F
ECHO Executing, please wait...
CALL System_Maint\TweakUAC.exe
COLOR 2F
FOR /F "TOKENS=*" %%i in ('time /t') do set TIMET=%%i
ECHO Finished on %Date% at %TIMET%. && %R1%
goto end:RUNNING_XP
COLOR 4F
ECHO Tweak UAC does not support XP, skipping... && %R3%
goto end:NO
COLOR 4F
ECHO Skipped... && %R1%:end
COLOR 8F
pause

If it detects XP and goes to :RUNNING_XP why wouldn't it exit?
=====================================
If at first you don't succeed, you're about average.M2

The code I have posted is not my entire batch script but a section. Once the above code has finished it continues on rather than just halting at a pause command (added only to try and catch the script and stop it ending once finished so I can check for on-screen errors etc...). I have only included this section as it is this section that is giving me a problem. However, for me it does not even get to the included pause command (or continues on to run the remainder of my full batch script when in it's full form) as the CMD windows simply closes before it should....
All I need is for it to skip loading this section of my batch if WinXP is detected and then continue without actually exiting the batch all together.
I hope that I have been clearer this time :)
***EDIT***
Phew! That was a long winded way of saying that the above code doesn't reach the pause command at the end wasn't it :P

If you really want to hammer it out it might help to post the actual code.
You can leave out the COLOR and other clutter.
And give some ex[;aination about stuff like %r3%.While we're at it, you don't need to create an :END , it's got :EOF built in.
If you've got the 'closing window' syndrome, run it from a prompt and save yourself some time and frustration.
=====================================
If at first you don't succeed, you're about average.M2

That is my code... Ok, the %R1% and %R3% parts are nothing more than pause commands. SET R1=Start /w Commands/Rest3.vbs is called earlier on in my batch when setting up a few vars. The contents of these files are simply Wscript.sleep 875 and Wscript.sleep 2625.
I know I can use all kinds of ways to add a pause such as ping localhost -n 3 but the .vbs way works for me :)
Right, moving swiftly on to the script in question, as explained the code I have now posted twice should (regardless of the route it takes - XP or Vista) reach the end pause command but it doesn't.
Now, if I set the XP detection part to simply echo what version of Windows I am running on like so:
VER | findstr /l "5.1." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running XPWell then my batch moves on as it should to the choice command (Commands\CHOICE /N " Run Tweak UAC? (Vista) [Y/N] ") and the remainder of the script works correctly so I am thinking that for whatever reason the XP detection part does not like the GOTO command as it then simply exits/quits/closes the CMD window and drops out of my batch all together as opposed to moving on using the GOTO RUNNING_XP, echoing that TweakUAC is not a WinXP app, waiting 3 seconds, going to the :END, then finally pausing until I press any key to continue on with whatever I choose to add afterwards....

If it doesn't like the GOTO command, it would print an error message to the screen and terminate. But if you are running the script by double-clicking it in Explorer, then the window will close before you get a chance to see the error message. Which is why you should always run your scripts from the command prompt, until you are satisfied they have been fully debugged.

CR,
It would help us if u post the entire script.
Many times it saves a lot of time if we just cut and paste the script into batch file and execute and see... Just like klint did above...
May be there are multiple labels of the same
name? Just wondering...(That regular expression thing never struck
me klint!)--
Holla.

I don't know how this bit looks on anyone else's browser but I see a square box after EXHO.
===================================
:RUNNING_XP
COLOR 4F
ECHO Tweak UAC does not support XP, skipping... && %R3%
goto end
=====================================
If at first you don't succeed, you're about average.M2

@M2: using the default code page 850, it's like a small triangular arrow pointing to the text on the right.
You probably use a different code page (are you based in Thailand?) so you see a boxy sort of character instead. Try typing CHCP 850.

Hi klint,
I pasted in that scrap and the goofy char is 70 hex. That's not what's tripping up the code. I got rid of the && %R1% and it works fine.CR,
You ought to lose those VB pauses until you get the rest of it working.
As a couple of us have said, by clicking instead of running it at the prompt, you're hiding errors and shovelling against the tide.
=====================================
If at first you don't succeed, you're about average.M2

Hi people, I removed the && %Rx% parts and replaced with ping localhost -n 3 > nul and it now works...
To be fair though I don't see how that could have caused the problem but it did so maybe it will remain as one of lifes little unanswered questions lol - main thing is it now works as expected so thank you all for your help :)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |