1

I've been working on this simple batch in windows 7, it has a drive letter set to delete all files and folders within. And if the drive wasn't mounted, it should prompt the user to enter one, here:

@echo off 
set folder="t:\"

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 new one.

set /p drive=

    cd /d %drive%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
@pause)

As for the IF EXIST part, it works perfectly, but when the drive is not mounted, once you enter a new drive letter, it won't work. Any ideas?, thanks in advance.

5
  • You say "drive" but then in the code use "folder". How can a folder be mounted? If you really want to delete all files and folders on a drive why don't just format it? If you want to delete a folder and its files and subdirectories use things like deltree
    – phuclv
    Sep 11, 2016 at 16:41
  • It says folder, then it says drive in the else part, as far as i'm concerned variables can take whatever name comes to mind, i might be wrong though. Sep 11, 2016 at 16:43
  • Whatever you want to do, you've done it the wrong way. Remove files and folders one by one is extremely costly compared to a single deltree
    – phuclv
    Sep 11, 2016 at 16:50
  • My question says that on user interaction, once the drive letter is entered it won't work so you're basically focusing on something that is irrelevant, it's drives and not delete methods that matter here. Sep 11, 2016 at 16:53
  • You can mount partitions to folders, ever since win 2k Sep 19, 2016 at 5:48

1 Answer 1

1

when the drive is not mounted, once you enter a new drive letter, it won't work

You need to enabledelayedexpansion:

Add

setlocal enabledelayedexpansion

to the start of your batch file and replace %drive% with !drive!

Corrected batch file:

@echo off 
setlocal enabledelayedexpansion
set folder="t:\"

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 new one.
  set /p drive=
  cd /d !drive!
  pause
  for /F "delims=" %%i in ('dir /b') do (
    rmdir "%%i" /s/q || del "%%i" /s/q
    )    
  )
  @pause
endlocal

Further Reading

6
  • It works now, I had to get rid of both "echo rmdir" and "echo dir" and just leave 'rmdir' and 'del' in order to actually delete the contents of the folder or drive in this case. enableDelayedExpansion did it. Sep 11, 2016 at 17:11
  • @PedroVelez Sorry, I put the echos in as a safety precaution when testing it and forgot to remove them :/ Answer updated.
    – DavidPostill
    Sep 11, 2016 at 17:17
  • So the devil was in the detail @DavidPostill. The program needed to reference a variable in execution time rather than parse time. Thus in my test it successfully run the code in the IF EXIST part and not in the ELSE one. :/ Sep 11, 2016 at 17:29
  • @PedroVelez Yes, that is kind of correct, but nothing to do with the if. The whole file is parsed but the value drive is not available until run time because you read it in with set /p. folder is available at parse time because it is explicitly given a value in the batch file itself.
    – DavidPostill
    Sep 11, 2016 at 17:36
  • @PedroVelez It is worth reading Enable and Disable Delayed Expansion, what does it do? and the linked questions as they explain it better than I can :)
    – DavidPostill
    Sep 11, 2016 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .