windowsbatch-filermdirbatch-choice

Deleting a folder after a confirmation prompt using a batch file


I am attempting to use a batch file to delete a folder, lets say it's called "D:\Pictures\1\Test". I want cmd to confirm that I want to delete the file before it deletes it using a single press of the "Y" key for yes, or the "N" key for no.

The code below prompts me to give a Y/N input and then closes the cmd window; however, it does not delete the file no matter what I press. If I simply type rmdir /s /q "D:\Pictures\1\Test", it deletes the folder and all containing files without issue, but does not give me a confirmation prompt. Here is my current code. What am I doing wrong?:

CHOICE /C YN /M "Are you sure?"
IF ERRORLEVEL 2 close
IF ERRORLEVEL 1 rmdir /s /q "D:\Pictures\1\Test"

Solution

  • There is no close command. Use exit /B instead.

    You could simply remove the /Q switch from the rmdir command so it prompts you once whether or not to delete the given directory tree.