Make a script independent of the System drive letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

Make a script independent of the System drive letter

#1 Post by budhax » 10 May 2008 16:37

Hello,
In some case, the "System drive letter" is different of C:, and a script can use an absolute path like C:\folder\bla.
So, this script cannot works if MS Windows is installed in a difference drive letter of C:

Here is a script using an absolute path in the C: drive (where C: is the System drive letter).

Code: Select all

@ECHO OFF
SETLOCAL
IF NOT EXIST "C:\PROGRAM Files\WinRAR" (ECHO *** WinRAR is missing&Pause&EXIT)
SET PATH="C:\PROGRAM Files\WinRAR\";%PATH%
:: Then do something using WinRAR
ENDLOCAL


Is this method enough and safe to make the script independent of the "System drive letter"?
METHOD:
- add a variable: SET SD=%SYSTEMDRIVE%
- replace all absolute drive C: by %SD%

Any comment?
Thanks and regards.

Code: Select all

@ECHO OFF
SETLOCAL
SET SD=%SYSTEMDRIVE%
IF NOT EXIST "%SD%\PROGRAM Files\WinRAR" (ECHO *** WinRAR is missing&Pause&EXIT)
SET PATH="%SD%\PROGRAM Files\WinRAR\";%PATH%
:: Then do something using WinRAR
ENDLOCAL

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 12 May 2008 15:36

Hi Budhax,

Is this method enough and safe to make the script independent of the "System drive letter"?


yes and no.

yes it is enough to independent of the drive letter.

but it's useless because "Program Files" is not static, on my XP System it's called "Programme".
If you will independent better use %ProgramFiles%

Code: Select all

@ECHO OFF
SETLOCAL
SET SD=%SYSTEMDRIVE%
IF NOT EXIST "%ProgramFiles%\WinRAR" (ECHO *** WinRAR is missing&Pause&EXIT)
SET PATH="%ProgramFiles%\WinRAR\";%PATH%
:: Then do something using WinRAR
ENDLOCAL


jeb

Post Reply