Computing.Net > Forums > Programming > How to detect Vista using VER

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to detect Vista using VER

Reply to Message Icon

Name: CR
Date: December 16, 2008 at 15:48:24 Pacific
OS: Windows XP Pro SP3
CPU/Ram: AMD XP 2500+ 1GB
Product: Custom / CUSTOM BUILD
Comment:

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 2000

VER | findstr /i "5.1." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running XP

VER | findstr /i "5.2." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Win 2003

VER | findstr /i "6.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Vista

This 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.



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 16, 2008 at 16:42:11 Pacific
Reply:

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 >nul

See how much simpler ^^ that is?

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: Holla
Date: December 16, 2008 at 21:51:41 Pacific
Reply:

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.


0

Response Number 3
Name: Judago
Date: December 16, 2008 at 22:39:50 Pacific
Reply:

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


0

Response Number 4
Name: CR
Date: December 17, 2008 at 02:55:53 Pacific
Reply:

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 Vista

Hmmm...


0

Response Number 5
Name: BatchFreak
Date: December 17, 2008 at 05:46:50 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: BatchFreak
Date: December 17, 2008 at 05:55:48 Pacific
Reply:

@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 >nul

This might work better ^^ it hasn't told mee Im running Vista :p

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 7
Name: CR
Date: December 17, 2008 at 07:24:35 Pacific
Reply:

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
:END

or 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


0

Response Number 8
Name: Mechanix2Go
Date: December 17, 2008 at 09:32:31 Pacific
Reply:

If the layout is consistent:

===========================================
@echo off & setLocal EnableDelayedExpansion

for /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


0

Response Number 9
Name: Holla
Date: December 17, 2008 at 10:33:13 Pacific
Reply:

The layout is not consistent! *&@^#%$!

c:\holla>ver

Microsoft Windows [Version 6.0.6001]

--
Holla.


0

Response Number 10
Name: CR
Date: December 17, 2008 at 11:03:44 Pacific
Reply:

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?


0

Response Number 11
Name: CR
Date: December 17, 2008 at 11:22:52 Pacific
Reply:

Thinking about it, Vista can show as either 6.0.6000 or 6.0.6001....


0

Response Number 12
Name: Holla
Date: December 17, 2008 at 11:39:34 Pacific
Reply:

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.


0

Response Number 13
Name: CR
Date: December 17, 2008 at 13:58:27 Pacific
Reply:

Ok I think I may have cracked it - how about this:

@echo off
if "%allusersprofile%"=="%systemdrive%\ProgramData" goto WinVista
if %os%==Windows_NT goto WinXP

echo 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
pause

It 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.


0

Response Number 14
Name: CR
Date: December 17, 2008 at 14:14:53 Pacific
Reply:

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


0

Response Number 15
Name: BatchFreak
Date: December 17, 2008 at 14:19:38 Pacific
Reply:

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.


0

Response Number 16
Name: CR
Date: December 17, 2008 at 14:22:08 Pacific
Reply:

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?


0

Response Number 17
Name: BatchFreak
Date: December 17, 2008 at 14:24:55 Pacific
Reply:

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.tmp

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 18
Name: CR
Date: December 17, 2008 at 15:00:21 Pacific
Reply:

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
...
...
...


0

Response Number 19
Name: BatchFreak
Date: December 17, 2008 at 15:58:44 Pacific
Reply:

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.tmp

IF NOT %version%==Windows XP GOTO Vista
XP PROGRAM
END

:VISTA
VISTA PROGRAM
END

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 20
Name: CR
Date: December 17, 2008 at 16:05:04 Pacific
Reply:

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


0

Response Number 21
Name: CR
Date: December 17, 2008 at 16:27:29 Pacific
Reply:

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
:VISTA

The 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 :)


0

Response Number 22
Name: Holla
Date: December 17, 2008 at 21:02:37 Pacific
Reply:

CR,
Here you go.
Does not work as expected.

c:\holla\bat\t>vert

c:\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=Windows

c:\holla\bat\t>IF NOT Windows == Windows XP GOTO Vista

c:\holla\bat\t>echo in XP
in XP

c:\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 :eof

I 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=windows

Post your complete batch file as it is if you want me to test under Vista.

--
Holla.


0

Response Number 23
Name: klint
Date: December 18, 2008 at 03:37:30 Pacific
Reply:

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.


0

Response Number 24
Name: CR
Date: December 18, 2008 at 04:10:13 Pacific
Reply:

:) 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_XP

Commands\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


0

Response Number 25
Name: klint
Date: December 18, 2008 at 07:07:02 Pacific
Reply:

> 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 2000

VER | findstr /l "5.1." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running XP

VER | findstr /l "5.2." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Win 2003

VER | findstr /l "6.0." > nul
IF %ERRORLEVEL% EQU 0 ECHO Running Vista
C:\>t
Running XP

C:\>


0

Response Number 26
Name: Mechanix2Go
Date: December 18, 2008 at 07:36:27 Pacific
Reply:

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


0

Response Number 27
Name: CR
Date: December 18, 2008 at 07:46:01 Pacific
Reply:

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_XP

Commands\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


0

Response Number 28
Name: Mechanix2Go
Date: December 18, 2008 at 08:02:26 Pacific
Reply:

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


0

Response Number 29
Name: CR
Date: December 18, 2008 at 08:13:16 Pacific
Reply:

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


0

Response Number 30
Name: Mechanix2Go
Date: December 18, 2008 at 08:23:58 Pacific
Reply:

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


0

Response Number 31
Name: CR
Date: December 18, 2008 at 08:45:06 Pacific
Reply:

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 XP

Well 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....



0

Response Number 32
Name: klint
Date: December 18, 2008 at 09:07:02 Pacific
Reply:

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.


0

Response Number 33
Name: Holla
Date: December 18, 2008 at 09:18:00 Pacific
Reply:

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.


0

Response Number 34
Name: Mechanix2Go
Date: December 18, 2008 at 10:40:34 Pacific
Reply:

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


0

Response Number 35
Name: klint
Date: December 18, 2008 at 10:56:46 Pacific
Reply:

@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.


0

Response Number 36
Name: Mechanix2Go
Date: December 18, 2008 at 11:12:12 Pacific
Reply:

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


0

Response Number 37
Name: CR
Date: December 18, 2008 at 11:59:34 Pacific
Reply:

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 :)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to detect Vista using VER

How to detect Logical partition www.computing.net/answers/programming/how-to-detect-logical-partition-/8050.html

Programming Code - Which to Use? www.computing.net/answers/programming/programming-code-which-to-use/8070.html

How to detect Window from my DOS pr www.computing.net/answers/programming/how-to-detect-window-from-my-dos-pr/12428.html