Batch file, prompt drive letter

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JessP
Posts: 1
Joined: 11 Sep 2016 09:46

Batch file, prompt drive letter

#1 Post by JessP » 11 Sep 2016 10:03

Hi folks. I've been trying this code that deletes all files
and folders within a specific location, and in case the drive wasn't
mounted, it should prompt the user to enter a drive letter, here:[/b]

Code: Select all

@echo off 
set folder="x:\"
IF EXIST "%folder%" (
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

)ELSE (@echo This drive is not mounted, please enter a drive letter:
    set /p folder= ""
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
@pause)


But once i enter a correct drive, like D: it says:

"The system cannot find the drive specified. File not found."

Do you guys know why it won't use the drive letter I provide in cd /d %folder% ?
Last edited by aGerman on 11 Sep 2016 11:51, edited 1 time in total.
Reason: code tags added

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file, prompt drive letter

#2 Post by aGerman » 11 Sep 2016 12:02

Variables inside of a command line or inside of a block of command lines that is enclosed into parentheses will be expanded to their values only once and before the line/block was even executed.
To avoid this "early" expansion you have to define a sub environment with delayed variable expansion enabled. The surrounding percent signs have to be replaced with exclamation marks in that case.

Code: Select all

...
) ELSE (
    echo This drive is not mounted, please enter a drive letter:
    set /p folder=
    setlocal EnableDelayedExpansion
    cd /d !folder!
    endlocal
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
    pause
)

Steffen

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

Re: Batch file, prompt drive letter

#3 Post by penpen » 11 Sep 2016 16:15

In this case, you could also simplify your algorithm, so code blocks are not needed (move same code out of code blocks):

Code: Select all

@echo off 
set folder="x:\"
IF not EXIST "%folder%" (
   @echo This drive is not mounted, please enter a drive letter:
   set /p folder= ""
)
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
@pause
(Assumed: Using "pause" to debug only.)


penpen

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Batch file, prompt drive letter

#4 Post by thefeduke » 21 Sep 2016 17:27

As it happens I was working on something similar with many good ideas from Post subject: "Re: check if a removable drive exists before running". I came up with this:

Code: Select all

@echo off
SetLocal
set folder="x:\"
::  first argument to override previous set command
If /i "%~1" NEQ "" set "Folder=%~1"
::  Do whatever validitycheck that fails
IF not EXIST "%folder%" Call :RemDir folder

Echo.
If /i "%folder%" NEQ "" (Echo.Now working with removable drive %folder%
) Else Echo.No Removable drives available.

set folder="%folder%\"
::  Insert rest of dangerous code here.

Exit /B

:RemDir     DirVariableName[In]
@echo Off & SetLocal EnableDelayedExpansion
    Set "Choices=0"
::  It should work, if you escape the comma (so it is not handled as a special character (delimiter); FOR loop analougous):
For /F "usebackq tokens=1* Delims= " %%d In (`
    WMIC logicaldisk get DeviceId^,drivetype ^| FINDSTR /B "[A-Z]:.*[2]"
`) DO (Set "Drive=%%d"&Set "Choices=!Choices!!Drive:~,1!")

If "%Choices%" EQU "0" Exit /B 1
If "%Choices%" EQU "0%Drive:~,1%" (Set "Chose=1"&GoTo :OnlyOne)
Echo.
Echo. Select '0' to terminate without making a choice.
Echo. Choose from the following drive letters:
Echo.
Choice /C %Choices%
Set "Chose=%errorlevel%"
If "%Chose%" EQU "1" (Echo.Leaving, as requested.&Exit /B 1)
Set /A "Chose-=1"
:OnlyOne
Set "MyDrive=!Choices:~%Chose%,1!:"
(EndLocal & Rem.Save "%~1" value
    If "%~1" NEQ "" (SET "%~1=%MyDrive%") ELSE Echo."No variable to save Drive=%MyDrive%")
Exit /B

John A.

Post Reply