windows 如何递归删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4756203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:00:09  来源:igfitidea点击:

How to delete files recursively

windowscmd

提问by Akshay

I have the directory structure /foo/bar/fooBar/.. . I want to write a Windows command where I can mention the path till foo directory and it deletes all the files and directory recursively in /foo, but it should NOT delete the foo directory.

我有目录结构 /foo/bar/fooBar/.. 。我想写一个 Windows 命令,我可以在其中提到直到 foo 目录的路径,它会递归地删除 /foo 中的所有文件和目录,但它不应该删除 foo 目录。

I have been using rmdir /q /s [path to foo]but this command deletes the foo directory as well. Let me know if there is any command(s) to accomplish this.

我一直在使用,rmdir /q /s [path to foo]但此命令也会删除 foo 目录。让我知道是否有任何命令可以完成此操作。

回答by Ferruccio

rd /s /q /path/to/foo
md /path/to/foo

回答by Libish Jacob

del /f /s /q DirectoryWhichContainsFilesToDelete/\*

This will delete all files in the folder DirectoryWhichContainsFilesToDeletewithout deleting the folder itself. Have fun :)

这将删除文件夹中的所有文件DirectoryWhichContainsFilesToDelete而不删除文件夹本身。玩得开心 :)

回答by Neil

I had been scratching my head on this one as well. It is easy enough to create a for loop that uses rmdir however it leaves behind folders that have spaces in the long names. It is possible to manipulate a dir list and get the 8.3 filenames however here is a much simpler solution.

我也一直在挠头。创建一个使用 rmdir 的 for 循环很容易,但是它会留下长名称中有空格的文件夹。可以操作目录列表并获取 8.3 文件名,但是这里有一个更简单的解决方案。

Create an empty folder then;

然后创建一个空文件夹;

robocopy \empty_folder \folder_with_sub_folders /PURGE

All subfolders & files will be deleted.

所有子文件夹和文件都将被删除。

回答by Neil

del X /f /s /q 
rd X /s /q 

this WILL remove the ROOt directory though. make it again with

不过,这将删除 ROOt 目录。再做一次

 md X

or make a copy of it first.

或者先复制一份。

otherwise you'll have to do batch funkiness

否则你将不得不做批量时髦

 dir X /ad /b

will give you a list of the immediate subdirectories of X. you can work out the rest

会给你一个 X 的直接子目录的列表。你可以计算出其余的

回答by fup

I was looking for a simple command to delete all files in a directory recursively but leaving the directory structure unchanged. So, maybe this could be interesting ;)

我正在寻找一个简单的命令来递归删除目录中的所有文件,但保持目录结构不变。所以,也许这会很有趣;)

for /f "delims=" %i in ('dir /B /S /A:-DH') do @del /F /Q /A:H "%i"

The command 'dir /B /S /A:-D' lists only files (/A:-D) in current directory recursively (/S) without 'dir' summary report (/B). The 'for' loops through each full line (/delims=) and executes the delete command, forced and quiet. I additionally used the hidden flag (/H) both for listing and deletion for some mysterious (e.g. thumbs.db) files.

命令 'dir /B /S /A:-D' 仅递归列出当前目录中的文件 (/A:-D) (/S),而没有 'dir' 摘要报告 (/B)。'for' 循环遍历每一行 (/delims=) 并执行删除命令,强制和安静。我还使用隐藏标志 (/H) 来列出和删除一些神秘的(例如 thumbs.db)文件。

回答by Dan Paradox

deltree /foo/* should work fine.

deltree /foo/* 应该可以正常工作。

回答by Rosberg Linhares

Try to use Powershell:

尝试使用 Powershell:

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

回答by Jim Cubbage Jr

I have used this in a batch file in the past. It uses a for loop to navigate the directory structure.

我过去曾在批处理文件中使用过它。它使用 for 循环来导航目录结构。

Here I remove the cvs sub directories off of a tree, needed when copying from one branch to another.

在这里,我从树中删除了从一个分支复制到另一个分支时需要的 cvs 子目录。

@echo off
if /I exist CVS. rd CVS /s /q >nul
for /F %%z in ('dir cvs /ad /s /b') do echo %%z && rd /s /q %%z
echo Batchfile %0 is complete

回答by user7415858

To prevent deleting of the foo directory try change directory to foo prior to the delete such as:

要防止删除 foo 目录,请尝试在删除之前将目录更改为 foo,例如:

cd c:\foo rd /s /q c:\foo

cd c:\foo rd /s /qc:\foo

This will delete all the files and folders under foo but NOT foo. An error message will be displayed as follow "The process cannot access the file because it is being used by another process."

这将删除 foo 下的所有文件和文件夹,但不会删除 foo。将显示如下错误消息“该进程无法访问该文件,因为它正在被另一个进程使用。”