View Full Version : batch file - woes setting variable.


wiklendt
03-30-2010, 10:19 PM
Hi Guys,

I'm making a batch file to automate file backups, which i thought was working fine but i've noticed now it doesn't do one part correctly...

by comparing the string "C:" to the "built-in" %~d0 (http://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file) argument, i test whether the batch file is running from the local C: drive, or an external drive of another letter. this is done using an IF statement.

if the batch file is run from a different-to-C: drive, all good - the batch file uses the %~d0 to get the correct drive letter and i can prefix the paths.

however, if it is running from the C drive, it prompts the user for the drive letter of the backup drive and stores it is SUPPOSED to store the data in the variable %D%, but it doesn't seem to. i added a check by echo'ing the %D% value to the screen.

now, i should also mention: %~d0 returns the drive letter WITH a colon, while my prompt only asks for the drive letter itself. therefore, so that i can use either %~d0 or the user variable as a prefix to the paths further in the script, i also concatenate a colon to my user variable.

the code snippet for this is:

::determine if program is run from local or other drive
IF %~d0==C: (
ECHO.
ECHO ======================================
ECHO START
ECHO.
ECHO Please attach the backup drive.
ECHO.
:: set the drive letter
SET /P L=Type the drive letter, then press ENTER:
:: attach colon to the letter (the caret character is used before special symbols)
SET D=%L%^:
ECHO Your drive letter is: %D%
PAUSE
CLS
) ELSE (
::set drive letter of batch file automatically
SET D=%~d0
)

i'm not sure what it's actually doing, but i can tell you that i have to set the drive letter three times before it will accept and keep that variable stored.

the %D% result i get from doing setting the drive letter three times is (e.g., for drive letter "i"):

Your drive letter is:
Your drive letter is: :
Your drive letter is: i:
interestingly, if i make a clean batch file (or just comment out the IF bits) with ONLY the 'set drive letter' code (without the if or %~d0 business), the script works - so it seems to be an IF issue? but why would it work on the third+ go?

this is my first ever batch script, which i cobbled together from reading up on a few websites and threads i found in google, but this little glitch has really got me puzzled...

i've attached my full batch script to this post, if it helps.

wiklendt
04-01-2010, 03:55 PM
turns out i needed to enable delayed expansion, and adjust the code accordingly. it now works as expected. see the adjusted code in red below (follow the red url for more detail):


@ECHO OFF & setLocal EnableDELAYedeXpansion
:: written on Microsoft Windows [version 6.0.6002]
:: with help from http://www.computing.net/answers/programming/user-prompt-to-set-drive-letter/21210.html

::determine if program is run from local or other drive
IF %~d0==C: (
ECHO.
ECHO ==============================================
ECHO START
ECHO.
ECHO Please attach the backup drive.
ECHO.
:: set drive letter manually
SET /P L="Type the drive letter, then press ENTER: "
:: the caret character is used before special symbols
SET D=!L!:
ECHO Your drive letter is: !D!
PAUSE
CLS
) ELSE (
::set drive letter of batch file automatically
SET D=%~d0
)