如何递归删除文件
我有目录结构 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这将删除文件夹
DirectoryWhichContainsFilesToDelete
中的所有文件,而不删除文件夹本身。玩得开心 :)
This will delete all files in the folder
DirectoryWhichContainsFilesToDelete
without deleting the folder itself.Have fun :)
我也一直在为这个问题摸不着头脑。创建使用 rmdir 的 for 循环很容易,但是它会留下长名称中包含空格的文件夹。可以操作目录列表并获取 8.3 文件名,但是这里有一个更简单的解决方案。
然后创建一个空文件夹;
所有子文件夹和文件将被删除。
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;
All subfolders & files will be deleted.
但这将删除 ROOT 目录。再次制作
或先复制一份。
否则你将不得不执行批处理 funkiness
会给你一个 X 的直接子目录的列表。你可以计算出其余的
this WILL remove the ROOt directory though. make it again with
or make a copy of it first.
otherwise you'll have to do batch funkiness
will give you a list of the immediate subdirectories of X. you can work out the rest
我正在寻找一个简单的命令来递归删除目录中的所有文件,但保持目录结构不变。所以,也许这可能很有趣;)
命令“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 ;)
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.
deltree /foo/* 应该可以正常工作。
deltree /foo/* should work fine.
我过去曾在批处理文件中使用过它。它使用 for 循环来导航目录结构。
在这里,我从树上删除了 cvs 子目录,这是从一个分支复制到另一个分支时需要的。
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.
尝试使用Powershell:
Try to use Powershell:
为了防止删除 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."