Quantcast
Channel: Scripts – Computing Tidbits
Viewing all articles
Browse latest Browse all 8

Fast Delete Complex Directories

$
0
0
Last updated on 02/17/2019

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, may delete complex directories in a fraction of that time, often quoted as being 20 times or more faster than the traditional explorer option. This guide shows how a batch file may be used to automate deleting large directories using batch files or the command line.

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, and 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

Another method for deleting folder files is Robocopy. A destination folder is mirrored from an empty source directory, which forces Robocopy to delete any files not in the source directory.

The main command for Robocopy:

robocopy "EmptyDir" "DestinationDir" /MIR /ETA /R:30 /W:4

  • /MIR – Mirror a directory
  • /ETA – Estimated Time
  • /R:30 – Number of retries
  • /W:4 – Time between retries (secs)

In the example batch files, RMDIR is still used to remove empty directories, and DEL deletes individual files

Using Batch Files

Batch files, including the example files below, may be used from the command line (with parameters), from the send-to directory, the right-click context menu, or by drag and drop (the easiest method).

Command line

The batch files 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_file_folder_fast_simple_v2.bat” “C:\Folder or file to delete”

Separate multiple parameters with a space between parameters.

Adding a batch file’s folder to the Windows environment variable allows it to be available from any cmd prompt without navigating to the batch file’s location or typing its full path. To add a batch file’s path to the Windows environment use:

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

Add the path e.g., ;C:\path to the batchfile; 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 the batch file to use, e.g., cmd /c \”C:\\Path to\\delete_file_folder_fast_simple_v2.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_file_folder_fast_simple_v2.bat\" %1"

Send to Directory

Another option is the Send to directory, Just 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

The example batch files support drag and drop. Just drag and drop onto a batch file to delete files and folders.

Differences in the Example Batch Files

delete_files_folders_fast_v2.bat – lists multiple files and folders to be deleted, the parent directory, limited folder/file attributes, and limited information on the names and numbers of files and folders deleted. Also includes a delayed exit, allowing the files and folders deletion information to be reviewed before the window closes (default 10 seconds).

delete_file_folder_fast_simple_v2.bat – limited to deleting one file or one folder at a time. Doesn’t provide much interaction or feedback except to ask  the user if they actually want to delete the displayed file or folder.

delete_files_folders_fast_robocopy_v2.bat – essentially the same as delete_file_folder_fast_simple_v2.bat except that Robocopy’s output is displayed during execution and after processing files. The batch file is paused after execution allowing the user to view the results without automatically exiting.

delete_file_folder_fast_simple_robocopy_v2.bat – almost identical in operation to delete_file_folder_fast_simple_v2.bat except that it uses Robocopy instead of DEL for deleting a folder or file, and Robocopy output is redirected to nul to eliminate command output. Users shouldn’t see any difference between them except possibly in performance.

The batch files may be renamed and will work identically.

Example Batch Files

Left-click to view the file in the current window, or use right-click and “Save Link As” to download.The batch files are changed from the previous versions as the /S switch greatly slowed performance when deleting individual files. The /S switch is still used, but only for folders.
Note: Remove the “.txt” extension before using.

The batch files below use only the RMDIR and DEL commands.

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.

The below batch files use Robocopy to delete folder files, RMDIR to delete  empty folders, and DEL to delete individual files. If Robocopy is installed but not included in the Windows environment path, be sure to change the path for Robocopy for your system in the batch file (don’t forget to remove the REM to enable it). No changes to the batch files are required if Robocopy is already installed and included in the path. To check, type “robocopy: in a command prompt. If Robocopy executes, it’s already installed and included in the environment path.

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

How to delete huge number of files on WindowsSuperuser

Print Friendly, PDF & Email

Share


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images