Establishing the OS (Win 7 / Vista or XP) in the batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mr_wizard
Posts: 14
Joined: 11 Jul 2012 12:46

Establishing the OS (Win 7 / Vista or XP) in the batch

#1 Post by mr_wizard » 23 Jul 2012 13:21

I have a batch I'm working up that offers a number of choices via a dos window menu for the end user to choose. These options perform a wide variety of operations in regard to PC maintenance / tune up. A lot of these operations are OS dependent, where I have to modify the directory on the command line in order to tell the batch where to go. I was thinking the most simple way of doing this is just checking for the existence of the "User" folder under the root directory, which would tell us what the OS is, and I can simply put this statement down before each variable so it knows which way to do it.

For example:

:1
@echo off

if blabla | RENAME "C:\Documents and Settings\%USERNAME%\Application Data\Thomson Financial\Thomson ONE\Workspaces" Workspaces.%DATE%
if blabla | RENAME "C:\Users\%USERNAME%\Appdata\Roaming\Thomson Financial\Thomson ONE\Workspaces" Workspaces.%DATE%
if not @echo Undetected Operating System
echo.
echo Finished.
echo.

pause

exit


Clearly I'm not sure what to put in blabla. It would have to see the user folder or not, and perform said operations. Also, I'm not 100% sure the "if not @echo Undetected Operating System" statement will work as an error as well. Can anyone shed some light? Much appreciated!

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#2 Post by Squashman » 23 Jul 2012 13:23

Why not use the VER command?

mr_wizard
Posts: 14
Joined: 11 Jul 2012 12:46

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#3 Post by mr_wizard » 23 Jul 2012 13:35

Learn something new every day. Wow.

So would it look something like this:

:1
@echo off

if VER=Microsoft Windows XP | RENAME "C:\Documents and Settings\%USERNAME%\Application Data\Thomson Financial\Thomson ONE\Workspaces" Workspaces.%DATE%
if VER=Microsoft Windows Vista | RENAME "C:\Users\%USERNAME%\Appdata\Roaming\Thomson Financial\Thomson ONE\Workspaces" Workspaces.%DATE%
if VER=Microsoft Windows 7 | RENAME "C:\Users\%USERNAME%\Appdata\Roaming\Thomson Financial\Thomson ONE\Workspaces" Workspaces.%DATE%
if not @echo Undetected Operating System
echo.
echo Finished.
echo.

pause

exit

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#4 Post by Squashman » 23 Jul 2012 13:44

It is a command. Not a variable. You will need to put it into a FOR LOOP to assign it to a variable.
I don't recall what the VER command outputs on Vista and Windows 7. You will need to run it on each computer.
you don't need the PIPE in there.

mr_wizard
Posts: 14
Joined: 11 Jul 2012 12:46

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#5 Post by mr_wizard » 23 Jul 2012 14:20

Noted.

30 minutes now messing with the FOR command and can't seem to get the syntax right. This is where I'm at:

For /D "Users==" %i IN C: DO @echo Successfully found the Users folder.


"Users==" was unexpected at this time.

Anyone mind providing the correct context? Not sure how this would work with the VER command, not grasping the help instructions for it :( Maybe my brains just ready to go home.

mr_wizard
Posts: 14
Joined: 11 Jul 2012 12:46

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#6 Post by mr_wizard » 23 Jul 2012 14:35

Ok that command was a little too much, I found this instead:

systeminfo | find "Windows XP"
if %ERRORLEVEL% == 0 goto WinXP

systeminfo | find "Windows 7”
if %ERRORLEVEL% == 0 goto Win7



Only issues is, if I tell it to go to any one of those points, I have 11 different options in my menu that will need to go to their specific commands for that OS... so while I suppose this would work, I would just have a ton of unique goto's

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#7 Post by Squashman » 23 Jul 2012 15:15

Code: Select all

for /F "tokens=2 Delims=[]" %%G in ('ver') do echo %%G

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Establishing the OS (Win 7 / Vista or XP) in the batch

#8 Post by foxidrive » 23 Jul 2012 16:57

This should work:

Code: Select all

set "windows="
VER | find  " 4.1." > nul && set windows=W98
VER | find  " 4.0." > nul && set windows=NT
VER | find  " 5.0." > nul && set windows=W2K
VER | find  " 5.1." > nul && set windows=XP
VER | find  " 5.2." > nul && set windows=2003
VER | find  " 6.0." > nul && set windows=Vista or server 2008
VER | find  " 6.1." > nul && set windows=Win7 or server 2008 R2
VER | find  " 6.2." > nul && set windows=Windows 8
if not defined windows echo unknown operating system


Version info came from here - I'm not sure that both the Win 2008 server values are correct.
http://en.wikipedia.org/wiki/Ver_%28command%29

Post Reply