How to get a batch file to set paths depending on OS?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ftlog
Posts: 2
Joined: 02 Aug 2013 07:24

How to get a batch file to set paths depending on OS?

#1 Post by ftlog » 06 Aug 2013 02:30

Hi, apologies if this sounds ridiculously basic, but I'm new to this and cannot seem to find what I need on the internet, so some help would be brilliant please!

I would like to alter a batch file so that it recognises which OS it is in and then acts accordingly. So for instance, if it's in windows 7, I would need the path set to include "Program Files (x86)", but if it were in XP, the path of the same file that needs to be set would include just "Program Files". Does anyone have any suggestions for how I could do this please?

Thank you in advance. I would really appreciate some assistance.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to get a batch file to set paths depending on OS?

#2 Post by penpen » 06 Aug 2013 03:15

You may do something like this:

Code: Select all

@echo off
setlocal

set "winver[4.1]=Windows 98"
set "winver[4.90]=Windows ME"
set "winver[5.00]=Windows 2000"
set "winver[5.1]=Windows XP"
set "winver[6.0]=Windows Vista"
set "winver[6.1]=Windows 7"
set "winver[6.2]=Windows 8"
set "winver[6.3]=Windows 8.1"

for /F "tokens=2 delims=[]" %%a in ('ver.exe') do (
   for /F "tokens=2-4 delims=. " %%b in ("%%a") do (
      set "version=%%b", "subversion=%%c", "build=%%d"
   )
)

if defined winver[%version%.%subversion%] (
   call echo You are using: %%winver[%version%.%subversion%]%%
) else (
   echo unknown version, or ver.exe is missing: %version%.%subversion%
)
endlocal
goto :eof
Notes:
I am sure that the list above is incomplete.
Additionally i don't know since when the pathes have been changed,
but you may google this.

Maybe this suffices, too:

Code: Select all

for /F "tokens=2 delims=[]" %%a in ('ver.exe') do (
   for /F "tokens=2-4 delims=. " %%b in ("%%a") do (
      set /A "version=%%b", "subversion=%%c", "build=%%d"
   )
)
set "myPath=Program Files (x86)"
if %version% == 5 if %subversion% LEQ 1 set "myPath=Program Files"
if %version% LSS 5 set "myPath=Program Files"
But i don't know exact where the change was, and it depends on the path you are using.

penpen

Edit: Shortened the above example and added the below.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to get a batch file to set paths depending on OS?

#3 Post by penpen » 06 Aug 2013 06:01

I currently got another idea what might work:

Code: Select all

if defined PROGRAMFILES(X86) ( set "myDir=%PROGRAMFILES(X86)%"
) else set "myDir=%PROGRAMFILES%"

penpen

ftlog
Posts: 2
Joined: 02 Aug 2013 07:24

Re: How to get a batch file to set paths depending on OS?

#4 Post by ftlog » 06 Aug 2013 06:22

penpen wrote:penpen

Oooh, this looks hopeful! I'll give it a go, so hopefully I can figure out how to use it properly. Thank you for your help! :D

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: How to get a batch file to set paths depending on OS?

#5 Post by brinda » 06 Aug 2013 06:37

penpen,

thanks. was searching for this as well.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: How to get a batch file to set paths depending on OS?

#6 Post by carlsomo » 07 Aug 2013 21:39

This is what I do but you must be beyond a setlocal to avoid screwing with the path:

Code: Select all

setlocal
if defined ProgramFiles(x86) set "ProgramFiles=%ProgramFiles(x86)%"
start "" "%ProgramFiles%\The Program path\The Program.exe"
endlocal&exit/b


if you are in a 32 bit system %ProgramFiles% remains as is.

Post Reply