删除目录中所有多余文件
我的 directoryA 被填充为 directoryB 的副本,并且更改或添加了一些文件。我想自动删除 directoryA 中所有在 directoryB 中具有冗余副本的文件。
这两个目录都有多层子目录,因此解决方案可能必须是递归的。
我的第一个想法是创建一个批处理脚本,但我对微软命令提示符很陌生,它似乎与 bash 脚本有很大不同,我对 bash 脚本的经验有限。
我使用的是 Windows XP,但想要一个也适用于 Windows 7 的解决方案。
I have directoryA that gets populated as a replica of directoryB, and some files are changed or added. I want to automate the process of deleting all files from directoryA that have redundant copies in directoryB.
Both directories have several layers of sub-directories, so the solution will likely have to be recursive.
My first thought is to create a batch script, but I'm new to the microsoft command prompt, and it seems to be widely different from bash scripting, with which I have some limited experience.
I am using Windows XP, but would like a solution that also worked on Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的情况下,我会采取懒人的方法,安装 mingw,并使用
查找目录 A 中与目录 B 中的文件具有相同 MD5 签名的每个文件,然后将其删除。
或者,如果您更喜欢一种不那么懒惰的解决方案,但不需要 mingw,请安装 Lua 和 Lua POSIX库(我认为可以安装在Windows上) 。你可以在 google 上搜索 MD5 库并在 Lua 中完成整个操作,并且它将是可移植的。与 mingw 解决方案不同的是,它可以轻松部署到任何人的 Windows 机器上;您可以制作一个独立的二进制文件。
In your situation I would take the lazy man's way out, install mingw, and use
to find every file in directoryA that has the same MD5 signature as a file in directoryB, then remove it.
Or if you prefer a less lazy solution but one that does not require mingw, install Lua and the Lua POSIX library (which I think can be installed on Windows). You can google for the MD5 library and do the entire operation in Lua, and it will be portable. And unlike the mingw solution, it will be easy to deploy to anybody's Windows box; you can make a standalone binary.
如果您想要一个不需要安装第三方软件的解决方案,请使用下面的脚本。它仅使用内置的命令行工具。
该脚本首先检查一些常见的错误情况。然后它递归地遍历清理目录中的所有文件。如果它在备份目录中找到同名文件,则会进行二进制比较以确定该文件是否冗余。
If you want a solution that does not require third-party software to be installed, use the script below. It only uses built-in command-line tools.
The script first checks some common error condition. Then it iterates recursively thru all the files in the cleanup directory. If it finds an equally named filed in the backup directory it does a binary comparison to determine if the file is redundant.
我对 Windows 敬而远之,但您可能会在 中找到所需的强大脚本功能Windows PowerShell(另请参阅Microsoft 文档) 。
PowerShell 对文件系统和其他地方的实体采用面向对象的方法。编写脚本来完成您需要的操作应该很容易,但当然您需要首先学习 PowerShell。
编辑:微软正在提供适用于 Windows XP 和其他一些操作系统的 PowerShell 下载,但我没有看到适用于 Windows 7 的 PowerShell。啊...维基百科说它已经集成在 Windows 7 中。所以这应该可以满足您的要求,而且它是已搭载最新版本的 Windows。
I make a wide berth around Windows, but you'll likely find the powerful scripting capabilities you're looking for in Windows PowerShell (see also Microsoft's documentation).
PowerShell takes an object-oriented approach to entities in the file system and elsewhere. It should be easy to whip up a script to do what you need, but you'd need to learn PowerShell first, of course.
EDIT: Microsoft is offering a download for PowerShell for Windows XP and a few others, but I don't see one for Windows 7. Ah... Wikipedia says it's already integrated in Windows 7. So that should cover your requirements, and it's already on-board with the newest versions of Windows.