0

I have an install script that needs to behave differently if running on XP/2003 or 7/R2, and it's been working perfectly fine. UNTIL a couple weeks ago I found out that it doesn't run on XPe (works fine on 7e), turns out find.exe is not included in XPe.

My current script uses:

ver | find "5." > nul
if %ERRORLEVEL% == 0 goto WinXP
    goto Win7

I borrowed a colleagues XPe device to test a workable solution, I tried copying find.exe from XP Pro, but it still didn't work. I tried varying versions (full path to findxp.exe, with/without .exe) of this, but it still doesn't work. Here's the output from XPe.

ver   | findxp.exe "5."  1>nul
The system cannot find the file specified.

All I really care to determine is if it's XP/2003, otherwise I'm assuming it's Vista (ha ha) or 2008 or newer. Although I wouldn't object to a solution that told me if it was XPe, I guess that may or may not come in handy in the future, although it would probably make the script slighly more complicated as it would have to account for all versions of Windows.

Thanks, Brian

Brian
  • 137
  • 1
  • 4
  • 14

4 Answers4

2

The code segment below is a direct replacement of your original code that don't require find.exe:

for /F "delims=" %%a in ('ver') do set ver=%%a
if "%ver:5.=%" neq "%ver%" goto WinXP
goto Win7
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I'm actually in the office today, going to see if I can steal that XPe machine for a few hours to try this out on. – Brian May 09 '13 at 13:28
  • Thanks for this. This worked beautifully! Although my script is fairly complex and does quite a bit, I've haven't used any `for` or `::delims=" %%aa`, so I'll have to look into this and play around with it. But this is simpler than Endoro's solution (which I also liked and might end up using at some point as well). Thank you to all that helped out!! – Brian May 09 '13 at 18:07
  • @Brian: A small detail I think is not enough clear is that "Endoro's solution" was written by me. Review the second link in "© Aacini at dostips" line below the code in Endoro's answer... – Aacini May 10 '13 at 00:00
  • Amazing, I just found a windows 7 sp1 (ver 6.1.7601) without find.exe!! I'm going to use try this solution – Jako Dec 05 '13 at 15:58
0
@ECHO OFF
SET OSVersion=Unknown

VER | FINDSTR /L "5.0" > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

VER | FINDSTR /L "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

VER | FINDSTR /L "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

VER | FINDSTR /L "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

VER | FINDSTR /L "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=7

IF %OSVersion%==Unknown (
 ECHO Unable to determine your version of Windows.
) ELSE (
 ECHO You appear to be using Windows %OSVersion%
)

ECHO.
PAUSE
Ozan Deniz
  • 1,087
  • 7
  • 22
0

Check it out below. Also, you can access it here: http://pastebin.com/iUtgN4ZU

    @echo off

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit

Save the file as %WINDIR%\vers.bat

And after that run from the command prompt:

vers

This will display which version of Windows.

NMALKO
  • 168
  • 1
  • 7
  • This won't work on XP Embedded, as I'm using the same command, but find.exe is not included in XPe...as I originally stated. Because my script works fine for every OS other than XPe. – Brian May 06 '13 at 21:13
0

Try this:

@echo off
setlocal EnableDelayedExpansion

::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8) do (
   if "!Version!" equ "this" (
      set Version=Windows %%a
   ) else if "!ver: %%a=!" neq "%ver%" (
      set Version=this
   )
)

::Identify bit
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
   set Type=64 bit
) else (
   set Type=32 bit
)

::Display result
echo %Version% %Type%

© Aacini at dostips

Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • I like your 32/64 check, although I'm using a simpler version: `if exist "%programfiles(x86)%" goto x64` – Brian May 06 '13 at 19:21
  • I'm trying to figure out your IF statement on my Win7, can't figure out the `equ "this"` portion. No matter if I put in `equ "6.1."` or `equ "7"`, it doesn't seem to work. Also, is it possible to have this work for multiple values (XP, 2003, etc.) without multiple IF's? Thanks. – Brian May 06 '13 at 19:25
  • It works from Windows 95 to Win 8 `95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8`. See also [here](http://www.dostips.com/forum/viewtopic.php?p=24774#p24774). – Endoro May 06 '13 at 19:40
  • Yeah, I'm not questioning whether it works, I can see that it works. But I'm trying to figure out the IF logic with `if "!Version!" equ "this"`. Where is "this" coming from? If I change "this" to "6.1." (with or without quotes), it returns "this 64 bit". I'm not a batch programming guru, but my existing script is fairly complex. So I'm trying to figure out your IF statement and get it to go to my XP section if the version is 5. (which would include XP and 2003), otherwise goto/assume Vista/2008/7/R2/etc. – Brian May 06 '13 at 20:31
  • My understand (which must be wrong) is if I change this to ` if "!Version!" equ "5.1" ( goto WinXP ) else if "!ver: %%a=!" neq "%ver%" ( goto Win7 )`. Or something like that. – Brian May 06 '13 at 20:35
  • You can replace all occurences of `this` with `that` or `Bill` and this will still working. – Endoro May 06 '13 at 20:38
  • Interesting, yeah, just tried that. Going to try using %%a in an IF then. – Brian May 06 '13 at 20:50
  • `%%a` contains successive all values from the for loop, values between the parentheses. – Endoro May 06 '13 at 20:57
  • LOL, yeah. Looks like I'll need a seperate IF statement that jumps to XP or 7 section, as I don't need a seperate section for each and every OS. Although I still don't understand the "this" in the `if "!Version!" equ "this"`, but I see that both "this's" have to match up for this to work. – Brian May 06 '13 at 21:11
  • You do not need additional `if`s, you can also use `goto` statements. – Endoro May 06 '13 at 21:21
  • @Brian: Although the FOR command set have _pairs_ of values, like `5.1.=XP` or `5.2.=2003`, the values are processed individually, that is, you could replace equal-signs by spaces and the set would be processed in the same way. When %%a match a version, like `5.1.` the method assign "this" string as indicator that in the _next_ iteration the value of %%a have the name of the OS version, like `XP`. You may replace "this" string with any other string that NOT appear in the FOR set. – Aacini May 07 '13 at 16:48
  • Yeah, was thinking of doing something like that with this. Although I like your other variation of my original. – Brian May 09 '13 at 13:28
  • LOL, yes, was planning to as soon as I had a working solution. Which is now. Woo hoo! – Brian May 09 '13 at 18:00