Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

windows - What ever happened to deltree, and what's its replacement?

In earlier versions of MS-DOS - I want to say version 7, but I could be wrong - there was a deltree command, which recursively deleted all subdirectories and files from a given path.

deltree no longer exists, but del didn't seem to inherit the ability to delete a tree. del /s deletes files, but not folders.

How to you easily (i.e., in one command) delete a tree from a batch file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:az.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:az.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:az.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:az.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...