Batch File Help

Thorax_the_Impaler

Minecraft Veteran
Messages
352
Location
127.0.0.1
Hello everyone!

I wasn't sure where to post this, but this section seemed appropriate. I was going through a routine backup of all my files, and I found a folder that contained a slew of batch files I made when I was a sophomore in high school. These days and even probably back then I wouldn't say batch files are often used for any tasks but I believe having a couple handy never hurt anyone. With that being said, I have the script of one file here and I'd like to ask a question to you guys that I was never able to answer myself.

The script:

@ECHO OFF
cls
ECHO This program is designed to clear out a directory.
ECHO It deletes all files in the directory, but will not touch folders.
ECHO .
ECHO .
ECHO .
set /p N=Please type the directory you wish to clear:
ECHO Press Enter to Continue:
set /P 1=%N%

:DISPLAY
ECHO
ECHO .
ECHO These files will be deleted:
ECHO .
DIR %N% | FIND "Directory"
DIR %N% /B /P

ECHO .
ECHO .
ECHO To delete listed files,
ECHO Press any key:
ECHO .
ECHO To Cancel, Press: 'Control-C'
ECHO .

PAUSE >NUL
:DELETE \C
DEL %N%
ECHO .
ECHO .
ECHO Task completed. Press any key to close.
:END
PAUSE >NUL

Here's my question: Why would this script completely disregard folders and such but had no qualms deleting any other file in the directory specified? (Unless of course, it didn't have the permission necessary to do so)
 
Because, I believe, DEL doesn't delete folders recursively unless you specify it to (i.e. actually run a loop).

Use RD or RMDir instead, with the /S switch.

Also, batch files are indeed used. I made several at work to help with repetitive tasks that I had to do for a few things.
 
Makes sense. But can I run both the DEL command and RMDir? I made the file with the purpose of it completely clearing out a specified directory; folders and such included. If I need to get creative with it that's perfectly okay (and fun), I just want to know it's possible?
 
RMDir deletes files and folders.

Code:
>rmdir /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

    /S      Removes all directories and files in the specified directory
            in addition to the directory itself.  Used to remove a directory
            tree.

    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S
 
Another reason is because the folder itself isn't part of the current directory but part of the directory above it. In order to even use the rmdir command, the batch file would need to go up one level and delete the folder from there or use the /S parameter on the rmdir command.
 
RMDir deletes files and folders.

Code:
>rmdir /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

    /S      Removes all directories and files in the specified directory
            in addition to the directory itself.  Used to remove a directory
            tree.

    /Q      Quiet mode, do not ask if ok to remove a directory tree with /S

Fantastic; but can it be used in the same fashion as the DEL command in the script? Or would I have to specify the folders in the directory provided in order to delete them?
 
Back
Top Bottom