• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 585
  • Last Modified:

Find out OS version in batch file...

Hello everyone, I need to create a batch file that will copy (from a location on C) and replace a single file. However the problem I have is that based on the OS the user is running the file could be in a different location on the hard drive (XP vs Win 7).

Can some tell me how I would check to see what OS they are running in a batch file and then based on that OS go to the correct folder location?

File location:
XP: C:\Documents and Settings\Username\Application Data\Meeting Center\Invitation Cache

Win 7: C:\Users\Username\App Data\Roaming\Meeting Center\ Invitation Cache

Thanks in advance for your help!!
1 Solution
winver will provide you the OS version installed.
Just check for the existance of C:\users or C:\Documents and Settings
REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_2000
ver | findstr /i "5\.1\." > nul
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto ver_2003
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

:Run Windows 7 specific commands here
REM echo OS Version: Windows 7 (debug line)
goto end

:Run Windows Vista specific commands here
REM echo OS Version: Windows Vista (debug line)
goto end

:Run Windows Server 2003 specific commands here
REM echo OS Version: Windows Server 2003 (debug line)
goto end

:Run Windows XP specific commands here
REM echo OS Version: Windows XP (debug line)
goto end

:Run Windows 2000 specific commands here
REM echo OS Version: Windows 2000 (debug line)
goto end

echo Machine OS cannot be determined.


There's no need at all to check the OS for this; just use the environment variable "APPDATA", which will point to the correct location on every OS since W2k (enter "set appdata" in a command prompt to check).
copy "C:\SomeFolder\whatever.txt" "%APPDATA%\Meeting Center\ Invitation Cache"

Open in new window

Never use a hardcoded "C:\documents and settings\%username%"; always use the environment variable like %USERPROFILE% or, in this case, %APPDATA%.
CCG3Sr Systems AdministratorAuthor Commented:
Thank you all. I think oBdA is right on target. I just need to set this up and test it on a few machines. I will let you know how it goes. Thanks again everyone!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now