如何递归删除文件

发布于 2024-10-12 20:18:53 字数 182 浏览 3 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

青衫负雪 2024-10-19 20:18:53
rd /s /q /path/to/foo
md /path/to/foo
rd /s /q /path/to/foo
md /path/to/foo
贩梦商人 2024-10-19 20:18:53
del /f /s /q DirectoryWhichContainsFilesToDelete/\*

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

del /f /s /q DirectoryWhichContainsFilesToDelete/\*

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

死开点丶别碍眼 2024-10-19 20:18:53

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

然后创建一个空文件夹;

robocopy \empty_folder \folder_with_sub_folders /PURGE

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

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.

Create an empty folder then;

robocopy \empty_folder \folder_with_sub_folders /PURGE

All subfolders & files will be deleted.

我ぃ本無心為│何有愛 2024-10-19 20:18:53
del X /f /s /q 
rd X /s /q 

但这将删除 ROOT 目录。再次制作

 md X

或先复制一份。

否则你将不得不执行批处理 funkiness

 dir X /ad /b

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

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

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

 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

勿忘初心 2024-10-19 20:18:53

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

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

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

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.

不醒的梦 2024-10-19 20:18:53

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

deltree /foo/* should work fine.

秋日私语 2024-10-19 20:18:53

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

在这里,我从树上删除了 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

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

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

@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
溺深海 2024-10-19 20:18:53

尝试使用Powershell:

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

Try to use Powershell:

powershell -Command "Remove-Item '\foo\*' -Recurse -Force"
海未深 2024-10-19 20:18:53

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

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

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

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

cd c:\foo
rd /s /q c:\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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文