Deltree 到底怎么了,它的替代品是什么?

在 MS-DOS 的早期版本中-我想说版本7,但是我可能错了-有一个 deltree命令,它递归地从给定的路径中删除所有子目录和文件。

deltree不再存在,但是 del似乎没有继承删除树的能力。del /s删除文件,但不删除文件夹。

如何轻松地(例如,在一个命令中)从批处理文件中删除树?

76940 次浏览
rmdir /s /q directory

It was replaced with the commands: RMDIR or RD

Delete all subdirectories with /S

Use it quietly with the /Q

Example:

RMDIR /S /Q Folder2Delete
RD /S /Q Folder2Delete

Documentation:

$ help rd
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

Actually RMDIR and RD commands in modern Windows operating system merge both the commands RD and Deltree of Win 98 in a single command. It's an internal command that's why you won't find any RD.exe and RMDIR.exe.

By typing this "RD /?" in cmd without double qoutes you'll get exactly what you want.

As others have mentioned, the rd command has the /s switch to recursively remove sub-directories. You can combine it with the /q switch to forcibly delete a sub-directory (and its contents) without prompting as so

rd /s /q c:\foobar

What everybody is missing is that rd is not an exact replacement for deltree as seemingly (almost) every page returned by Googling for windows deltree would have you believe. The deltree command worked for both directories and files, making it a single convenient, all-purpose deletion command. That is both of the following are valid:

deltree /y c:\foobar
deltree /y c:\baz.txt

However rd (not surprisingly) only works for directories. As such only the first of these commands is valid while the second gives and error and leaves the file un-deleted:

rd /s /q c:\foobar
rd /s /q c:\baz.txt

Further, the del command only works for files, not directories, so only the second command is valid while the first gives an error:

del /f /q c:\foobar
del /f /q c:\baz.txt

There is no built-in way to delete files and directories as could be done with deltree. Using rd and del individually is inconvenient at best because it requires distinguishing whether a file-system object (file-/folder-name) is a file or directory which is not always possible or practical.

You can copy the deltree command from a previous OS, however it will only work on 32-bit versions of Windows since it is a 16-bit DOS command (even in Windows 9x).

Another option is to create a batch-file that calls both del and rd; something like this:

::deltree.bat


@echo off
rd  %* 2> nul
del %* 2> nul

You would call it as so:

deltree.bat /s /q /f c:\foobar
deltree.bat /s /q /f c:\baz.txt

This calls both rd and del, passing in the arguments and redirecting the output to nul to avoid the error that one of them will invariably emit.

You will probably want to customize the behavior to accomodate or simplify parameters or allow error messages, but even so, it is not ideal and not a direct replacement for deltree.

An alternative is to get a third-party tool, though finding one is a real exercise in search-query-crafting.

Feeling nostalgic, I wrote my own deltree.exe. It works with both directories and files, and uses SHFileOperation() for speed.

https://github.com/ai7/toolbox/tree/master/deltree

deltree v1.01 [Mar 27 2015, 16:31:02] (gcc 4.9.1)


Usage: deltree [options] <path> ...


Options:
-y    yes, suppresses prompting for confirmation
-s    silent, do not display any progress dialog
-n    do nothing, simulate the operation
-f    force, no prompting/silent (for rm compatibility)
-r    ignored (for rm compatibility)


Delete directories and all the subdirectories and files in it.

It takes wildcards and you can use it like unix rm:

deltree -rf *

to delete a directory and all it's contents recursively

rd /s MY_DOOMED_DIR

Nowadays, you can use Powershell to do the same task:

powershell -Command "Remove-Item 'PathToMyDirectory\*' -Recurse -Force"

Use this:

cd (your directory here)
del *.* /f /s /q

Delete all files and subdirectories

cd /d Directory && rd /s /q .\