Quantcast
Channel: Batch Files – Computing Tidbits
Viewing all articles
Browse latest Browse all 7

Fast Delete Complex Directories

0
0
Last updated on 08/29/2018

Normally, deleting directories is fairly quick when they contain a modest number of sub-directories and files. But if directories have complex structures, consisting of hundreds or thousands of sub-directories and files, it may take several minutes or even hours to delete them. The command line, with less overhead, can delete complex directories in a fraction of that time, being 20 times or more faster than the traditional explorer option. This guide shows how a batch file may be used to to delete large directories using the command line quickly and easily.

delete

Explorer can be extremely slow when deleting directories because it calculates the folder size, the number of items in the folder, and the estimated completion time before processing, and reports on the items deleted, remaining deletions, current deletions, while monitoring and reporting on any conflicts during processing. These processes are still in effect even when the recycle bin is emptied or when files and folders are deleted directly using Shift+Del. Using the command line eliminates this overhead, resulting in a faster deletion process.

Commands

Two main commands act as a two-step process for deletion in the batch scripts:

del /f/q/s "%folder%" > nul
Acts as a first pass to delete files and outputs to nul to avoid the overhead of writing to the screen

rmdir /q/s "%folder%"
Removes the directory structure

  • /f – Force delete read-only files
  • /q – Quiet mode, do not ask to delete
  • /s – Include subdirectories

Using the Batch Files

Either batch file may be used from the command line (with parameters), from the send-to directory, the right-click context menu, or by drag and drop.

Command line

Either batch file may be executed from a Windows command prompt with parameters or added to the environmental path for convenience.

To execute from a command prompt, open a new cmd prompt, navigate to the batch file’s location and enter its full name, or enter the batch file’s full path. Be sure to add the directories and/or files to delete as parameters. Also include quotes around any file paths with spaces:

Ex. “C:\Program Files\delete_files_folders_fast.bat” “C:\Folder or file to delete”

Adding the batch file to the Windows environment variable enables the batch file to be available from any cmd prompt without typing its full path. To add the batch file use:

start–> Control Panel–> System–> Advanced system settings–> Advanced Tab–> Environment Variables–> System Variables–> Path–> Edit.

Add the path ;C:\path to \delete_files_folders_fast.bat; to the end (don’t forget the single semicolons at the beginning and end). Hit OK three times.

Right-click Context Menu

This option allows the user to right-click a folder and select the batch file from the pop-up menu. The steps to enable this are:

  1. Press the WINDOWS KEY + R to open the Run dialog box
  2. Enter regedit and press ENTER
  3. Navigate to HKEY_CLASSES_ROOT\Directory\shell\
  4. Right-click on the yellow shell key. Select New > Key
  5. Enter a name: Delete &Fast then press ENTER
  6. Right-click on the Delete &Fast folder created, and select New > Key
  7. Enter “command” and press ENTER.
  8. Left-click on the yellow command key created. Double-click the (Default) entry.
  9. In the Value Data field, enter: cmd /c \”C:\\Path to\\delete_files_folders_fast.bat\” %1″ then press OK

Another way to accomplish the above is to use a reg file. Copy the code below, paste it into a file editor and save it as “Delete Fast.reg”. Double click the file to merge its contents to the registry and the right-click menu is ready to use.


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Delete &Fast\command]
@="cmd /c \"C:\\Path to\\delete_files_folders_fast.bat\" %1"

Send to Directory

To add either batch file to the Send to directory, create a shortcut and add it to the appropriate directory:

For newer versions of Windows, the send to directory is located at:

C:\Users\<yourusername>\AppData\Roaming\Microsoft\Windows\SendTo

For XP, the location is:

C:\Documents and Settings\<yourusername>\SendTo

To use, right click a file or directory and select the batch file from the Send to menu.

Drag and Drop

Both batch files support drag and drop. Drag and drop folders or files onto either batch file to delete them.

Differences in the Batch Files

delete_files_folders_fast.bat – lists multiple files and folders to be deleted, the parent directory, limited folder/file attributes, and provides limited information on the names and numbers of files and folders deleted. Also includes a delayed exit so information about what was deleted can be reviewed before the window closes (default 10 seconds).

simple_delete_files_folders_fast.bat – limited to deleting one file or one folder at a time. Doesn’t provide much interaction or feedback except to ask whether the user actually wants to delete the file or folder.

 

Either batch file may be renamed and they will work identically. Make the appropriate changes to any of the above instructions if the file names are changed.

Batch File Download

Left-click to view script in the current window, use right-click and “Save Link As” to download.
Note: Remove the “.txt” extension before using either batch file.

Note: There is a file embedded within this post, please visit this post to download the file.

Note: There is a file embedded within this post, please visit this post to download the file.

 

References:

Fastest Way to Delete Large Folders in WindowsMatt’s Repository

 

Print Friendly, PDF & Email

Share


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images