DELTREE - Delete all subfolders and files.

Very old versions of Windows (Windows 95 and earlier) had the DELTREE command to delete all sub-folders and files. Newer versions of Windows do not have this command, but we can easily write a short batch script to do the same thing.

Deleting from the command line is significantly faster than using Windows Explorer, often seconds instead of minutes, there is no running calculation of file sizes and no recycle bin. This does nean there is no possibility of an undo other than restoring a backup.

The two key commands required are DEL /s to delete all files including hidden and system files, followed by RD /s to remove the now empty folders.

When iterating through thousands of files, supressing the output of DEL *.* by redirecting it to NUL, will make the process run a little faster.

:: DelTree.cmd
:: Delete a folder plus all files and subfolders
@Echo Off
Set _folder=%1
if [%_folder%]==[] goto:eof
Pushd %_folder%
Del /f /q /s *.* >NUL
CD \
RD /s /q %_folder%
:: repeat because RD is sometimes buggy 
if exist %_folder% RD /s /q %_folder%
Popd

Example

Supply the full path to the folder to be deleted surrounded in quotes:
deltree.cmd "c:\demo\sample files"

Delete the Current Folder

This deletes all contents of the current folder. This means you can copy the script to a folder and simply double click it. The script is made read-only with ATTRIB so that it does not delete itself.
This version also does not delete the root folder itself.

:: FastDel.cmd
:: Remove all files and subfolders
@Echo Off
cls
Set _folder="%~dp0"
Attrib +R %0
pushd %_folder%
ECHO Delete all contents of the folder: %_folder% ?
Pause
:: Delete the files
Del /f /q /s /a:-R %_folder% >NUL
:: Delete the folders
For /d %%G in (%_folder%\*) do RD /s /q "%%G"
Attrib -R %0
Popd

If you use this on a UNC path like \\Server64\share1\somefolder the CMD shell will not be able to set a current directory. To avoid an automatic fallback to C:\Windows\ the script uses %~dp0 to grab the location and then popd will, if needed, map a temporary drive.

Delete only Empty folders

Delete all folders and subfolders below the current folder.
Again you can copy the script below to a folder and simply double click it.

:: DelEmpty.cmd
:: Remove all empty folders and subfolders
@Echo off
Set _folder="%~dp0"
pushd %_folder%
Echo remove empty folders from %_folder% ?
pause
For /f "delims=" %%d in ('dir /s /b /ad %_folder% ^| sort /r') do RD "%%d" 2>nul

Alternative PowerShell one-liner to delete empty folders:
PS C:\> Get-ChildItem -Recurse . | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item

“However beautiful the strategy, you should occasionally look at the results” ~ Sir Winston Churchill

Related:

RD - Delete folders or entire folder trees
DELPROF Delete NT user profiles
DEL - Delete files
PowerShell: Remove-Item - Delete the specified items.
Equivalent bash command (Linux): rmdir / rm - Remove folders/ files


Copyright © SS64.com 1999-2017
Some rights reserved