user prompt to set drive letter

April 1, 2010 at 02:41:51
Specs: Windows Vista
hi,

i'm trying to semi-automate a batch file of mine for file backups. i'm using an 'if' statement to determine (by comparing to %~d0) if the batch is being run from the backup drive (so use %~d0) or the C: drive (so prompt user for drive letter).

my script seems to work fine from the backup drive, but is 'funny' if run from the C: drive.

'funny' means that i have to input the drive letter three times before the letter is actually 'set'.

i have added an echo of the resulting drive letter variable, and in each of the first three trys, this is what i get:

1) your drive letter is:
2) your drive letter is: :
3) your drive letter is: i:
+) your drive letter is: i:

the other quirk is that, if i comment out the if statement, and have JUST the user prompt, the variable is set fine.

here is the script:

::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
)

this is my first ever batch script, but this quirk has got me stumped. has anyone experienced this before? can anyone see where i've gone wrong?

thanks in advance,
agnieszka.


See More: user prompt to set drive letter

Report •

#1
April 1, 2010 at 05:59:21
Whenever you've got a series of commands inside brackets (i.e. in your case it's the IF-block) then those commands are loaded into the interpreter all at once, and all %variable% expansions are performed before the commands are executed. So in your SET D=%L%^: the %L% gets expanded to the value of L at the start of the IF block, and does not see the changed value after the preceding SET/P command.

To fix this, you need to use delayed expansion. At the start of your batch file, put

SETLOCAL EnableDelayedExpansion

and use !L! instead of %L% (and of course !D! instead of %D%). Type SET /? for more details.


Report •

#2
April 1, 2010 at 06:25:04
I don't think you need special handling of colon. But use delayed expansion to make IF blocks work.

================================

@echo off & setLocal EnableDELAYedeXpansion
    IF %~d0==C: (
      SET /P L="Type the drive letter, then press ENTER: "
      SET L=!L!:
      ECHO Your drive letter is: !L!
    ) ELSE (
      SET D=%~d0
    )


=====================================
Helping others achieve escape felicity

M2


Report •

#3
April 1, 2010 at 07:24:35
The code from the opening post works ... I dont see an issue:

D:\Temp>test

D:\Temp>IF D: == C: (
ECHO.
ECHO ============================
ECHO START
ECHO.
ECHO Please attach the backup drive.
ECHO.
SET /P L="Type the drive letter, then press ENTER: "
SET D=:
ECHO Your drive letter is: D:
PAUSE
CLS
) ELSE (SET D=D: )
D:\Temp>

But you should use the /I paramter with the IF statement though:

C:\>d:\temp\test

C:\>IF d: == C: (
ECHO.
ECHO ============================
ECHO START
ECHO.
ECHO Please attach the backup drive.
ECHO.
SET /P L="Type the drive letter, then press ENTER: "
SET D=:
ECHO Your drive letter is: d:
PAUSE
CLS
) ELSE (SET D=d: )
C:\>


Report •

Related Solutions

#4
April 1, 2010 at 15:04:57
thanks klint and mechanix2go! delayed expansion did the trick. :)

i did briefly read about delayed expansion - but i must have not fully understood how to use it b/c my attempts to incorporate it were different to your codes and so had not worked.

i love experts! thank you very much - my backups are now so easy. used to take half a day, but now they take me just minutes!


Report •

#5
April 1, 2010 at 15:08:16
quick forum-related question: is there a way to mark this post/thread/problem as solved? i'm new here... (probably will hear form me again!)

Report •

#6
April 2, 2010 at 08:02:24
Not that I know, but good question indeed

Report •

#7
April 2, 2010 at 08:04:55
Not that I know of but it's a great idea.

:)


=====================================
Helping others achieve escape felicity

M2


Report •

#8
April 2, 2010 at 16:10:53
Hi again,

so enabling the delayed expansion works script-wise, however, now the *.bat file takes about 8-9 seconds to open after double-clicking (i.e., for the command window to display) - it used to be instantaneous.

i tried disabling the delayed expansion (is the command case sensitive?), but this has still left the command window taking a long time to open, even if it's not my batch file (e.g., WindowksKey+R, "cmd" + ENTER) and even if i restart my computer - is this a result of playing with the 'local environment' with the expansion?

another related issue is that whatever i set in the batch file used to only affect the batch file (e.g., if i set colour and cmd window size), but since setting the "setlocal" code, anything i've 'set' in my batch file seems to seep over to the command window...? i read (later) that a set or setlocal should be coupled with an "endlocal" - is that where i've gone wrong there? also, would that make a difference with the "color" command?

by the way, i *think* i've figured out a way to get around delayed expansion with an if statement (though unsure if the setlocal is still making a muck of the place, if at all), and that is to have a goto in place of the code, and have the "then" and "else" code separate, as below:

::===============================
:START_MENU

::determine if program is run from local or other drive
IF %~d0==C: (GOTO START_ASK) ELSE (GOTO START_SET)

:START_ASK

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: "
SET D=%L%:
CLS
GOTO START_APPLY

:START_SET

::set drive letter of batch file automatically
SET D=%~d0
GOTO START_APPLY

:START_APPLY

:: set the variable for backup log file
SET BACK_LOG=%D%\BKP_LOG.txt

:: set the variable for restore log file
SET REST_FILE=RESTORE_LOG.txt

:: set the variable for restore log path
SET REST_LOG=C:\%REST_FILE%

which may be a better thing to have if setlocal plays around with real computer settings....


Report •

#9
April 3, 2010 at 03:35:24
Delayed expansion can't be the culprit, unless you've done something else while investigating how it works.

You can use ENDLOCAL but you don't have to because there is an implicit ENDLOCAL at the end of the batch file if you don't explicitly specify it.

The effect of SETLOCAL is that all the variables you define in the batch file will be local to that batch file and will not affect the environment at the command-line level. So in a way it is safer to use SETLOCAL than not to, because when you use it, it protects you from changing the environment outside.


Report •

#10
April 3, 2010 at 13:53:05
yeah, most definitely some other issue. No settings survive a reboot unless they're written to the registry or into a system control file somewhere. enabledelayedexpansion should have no effect on much of anything other than the batchfile it is written into, not even the current cmd environment.
you can (literally) jump around using the enabledelayed by calling to subs outside of blocks:
for /L %%a in (1 1 10) do (
set /a counter+=1
echo.counter not right:%counter%
call :showcount
)
goto :eof
:showcount
echo.counter is right in the sub:%counter%
::--
of course, goto works the same way, but it breaks the loop.

Report •

#11
April 3, 2010 at 18:04:43
hm. it's probably just my computer, then. i am running vista, after all, so god only knows what the thing's really up to! LOL

thanks guys, you've all been very helpful :)


Report •

#12
April 5, 2010 at 04:28:56
Debugging in a batch-script requires ECHO ON, otherwise one is often guessing

If you have time, change that, then forward all output to a text-file, and examine what you see. Put the code next to the output


Report •

#13
April 5, 2010 at 14:45:31
@tvc: I don't think that's the problem. To repeat, the command window takes 8 seconds to open after double-clicking on the batch file. During those 8 seconds, the batch file has not even started running yet. So having ECHO ON is not going to buy you anything.

Try this: instead of double-clicking on a batch file, press Windows Key + R and then type CMD and Enter. You should get the command prompt. Does this window take a long time to open?

From the command prompt window type CD <folder-name> and then run the batch file from there. How long does it take the batch file to run now?

Try again with your network(s) disabled. Unplug your ethernet cable and turn off your wireless, and try double-clicking on the batch file again. Does it still take a long time to open? If so, do you hear any noise from your CD-ROM or floppy disk drive when you double-click it?

Lastly, what sort of anti-virus do you have, perhaps it spends a long time checking your batch files before you can run them. Although I wouldn't recommend turning off your anti-virus, you might want to try it as a very last resort (but only temporarily, just to test, and whilst it's unplugged from the network.)


Report •

#14
April 5, 2010 at 16:39:53
hi klimt.

the command window itself, even when opening via [WindowsKey]+[R] > "CMD" [ENTER] opened in 8 seconds. once there was one command window open, any others would open instantly, but only if there was a window open already at the time (if all windows were then closed, and one clicked to re-open, it would again take 8 seconds to load)

i have since (for many other issues that were pissing me off - not that it helped) reset my computer to factory default. it has fixed the batch file /command window loading issue, but not any of my other problems. i'm pretty sick of vista but don't have the money to upgrade just yet. *sigh.* just this morning my computer has frozen about 4 times - and this after a reset! can't wait to get my hands on windows 7...


Report •

#15
April 5, 2010 at 18:28:10
you can maybe "downgrade" to winXP in meantime Lol! (or did you "burn your CDs behind you" aaahg, bad pun! sorry)That's what i would consider, i've not heard much good about vista. Reinstall/configure def! not fun either.

Report •

#16
April 5, 2010 at 19:30:56
XP would work too (and believe me, i'd consider that an UPgrade too!), but unfortunately i don't have a copy of that either. ho hum. time to think seriously about saving some money, me thinks.

a new, smaller-lighter laptop would be a nice change anyhow. i'm thinking one of those thin sony lappys- though not sure what OS is bundled with them...


Report •

#17
April 5, 2010 at 20:40:07
wouldn't it be nice, you order (and pay for!) hdwe, you could specify op.sys AND require full set of boot CDs and applications that YOU paid for!!
my pet peeve is: your computer no longer belongs to you, but is licensed to MS or whoever, not only on startup, but indefinitely and forever, because you are running their op.sys., and therefore they make the rules and ENFORCE them. I know there is some need for control, but GiMeABrk, not to that extent, and not at Expense, of enduser.
I will always keep some old crap around just for spite.
(similar situation with automobiles: how long, you guess, before GPS and misc. digital "safeties" are no longer optional, so at some point you are no longer the true owner of your vehicle, but just a "node" that can be "de-activated" or put into "safe mode" or worse, "logged"!)
sorry! pet peeve and conspiracy stuff not intended but there it is regardless. At least you still have option to "click off" my crap. Ha!

Report •

#18
April 6, 2010 at 03:32:58
No, after all, no issue with this script, "only" an issue with the OS ... well, you know what to do. Havent seen any such problem on either XP sp2 or sp3, but I recall MS saying Vista is much better than XP ... but since then, 7 has come out, and now Vista suddenly is a bad OS of course (otherwise they have less argument to say 7 is better).

Report •

#19
April 6, 2010 at 06:49:02
@nbrane, yes I see what you mean. What annoyed me is when I rang Dell and told them I wanted to buy a computer without any software on it, just a blank disk (even one without a disk drive at all would have been ok) so that I could install Linux on it myself. That option is just not available: if I wanted a computer, I had to have MS software on it, even if it meant I would have to spend valuable time deleting it once I unpacked it.

Report •

#20
April 6, 2010 at 09:22:31
Lol! makes you wonder... it might be like the infamous "soft-drink cup-count", where if you want a empty cup, you still got to pay for a softdrink: dell has to package MS on each computer sold or MS will sue them for breach of contract, prob'ly something like that.

Report •

#21
April 6, 2010 at 09:33:39
nbrane: dell has to package MS on each computer sold or MS will sue them for breach of contract
That'd be illegal. Not saying it didn't happen, but too many governments watch the computer market these days.

Report •

Ask Question