删除目录中所有多余文件

发布于 2024-09-10 07:57:01 字数 369 浏览 0 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(3

澉约 2024-09-17 07:57:01

在你的情况下,我会采取懒人的方法,安装 mingw,并使用

find directoryA directoryB -type f -exec md5sum '{}' ';' |
my-bash-script

查找目录 A 中与目录 B 中的文件具有相同 MD5 签名的每个文件,然后将其删除。

或者,如果您更喜欢一种不那么懒惰的解决方案,但不需要 mingw,请安装 LuaLua POSIX库(我认为可以安装在Windows上) 。你可以在 google 上搜索 MD5 库并在 Lua 中完成整个操作,并且它将是可移植的。与 mingw 解决方案不同的是,它可以轻松部署到任何人的 Windows 机器上;您可以制作一个独立的二进制文件。

In your situation I would take the lazy man's way out, install mingw, and use

find directoryA directoryB -type f -exec md5sum '{}' ';' |
my-bash-script

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.

风柔一江水 2024-09-17 07:57:01

如果您想要一个不需要安装第三方软件的解决方案,请使用下面的脚本。它仅使用内置的命令行工具。

该脚本首先检查一些常见的错误情况。然后它递归地遍历清理目录中的所有文件。如果它在备份目录中找到同名文件,则会进行二进制比较以确定该文件是否冗余。

@echo off
rem delete files from a directory that have a redundant copy in a backup directory

setlocal enabledelayedexpansion

rem check arguments
if "%~2"=="" (
    echo.Usage: %~n0 cleanup_dir backup_dir
    echo.Delete files from cleanup_dir that have a redundant copy in backup_dir
    exit /b 1
)

set CLEANUP_DIR=%~f1
if not exist "%CLEANUP_DIR%" (
    echo."%CLEANUP_DIR%" does not exist.
    exit /b 1
)

set BACKUP_DIR=%~f2
if not exist "%BACKUP_DIR%" (
    echo."%BACKUP_DIR%" does not exist.
    exit /b 1
)

rem ensure that dirs are different
if "%CLEANUP_DIR%" == "%BACKUP_DIR%" (
    echo.backup directory must not be the same as cleanup directory.
    exit /b 1
)

rem ensure that backup_dir is not a sub dir of cleanup_dir
if not "!BACKUP_DIR:%CLEANUP_DIR%=!" == "%BACKUP_DIR%" (
    echo.backup directory must not be a sub directory of cleanup directory.
    exit /b 1
)

rem iterate recursively thru files in cleanup_dir
for /R "%CLEANUP_DIR%" %%F in (*) do (
    set FILE_PATH=%%F
    set BACKUP_FILE_PATH=!FILE_PATH:%CLEANUP_DIR%=%BACKUP_DIR%!
    if exist "!BACKUP_FILE_PATH!" (
        rem binary compare file to file in backup dir
        fc /B "!FILE_PATH!" "!BACKUP_FILE_PATH!" >NUL 2>&1
        if not errorlevel 1 (
            rem if files are identical delete file from cleanup_dir
            echo.delete redundant "!FILE_PATH!".
            del "!FILE_PATH!"
        ) else (
            echo.keep modified "!FILE_PATH!".
        )
    ) else (
        echo.keep added "!FILE_PATH!".
    )
)

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.

@echo off
rem delete files from a directory that have a redundant copy in a backup directory

setlocal enabledelayedexpansion

rem check arguments
if "%~2"=="" (
    echo.Usage: %~n0 cleanup_dir backup_dir
    echo.Delete files from cleanup_dir that have a redundant copy in backup_dir
    exit /b 1
)

set CLEANUP_DIR=%~f1
if not exist "%CLEANUP_DIR%" (
    echo."%CLEANUP_DIR%" does not exist.
    exit /b 1
)

set BACKUP_DIR=%~f2
if not exist "%BACKUP_DIR%" (
    echo."%BACKUP_DIR%" does not exist.
    exit /b 1
)

rem ensure that dirs are different
if "%CLEANUP_DIR%" == "%BACKUP_DIR%" (
    echo.backup directory must not be the same as cleanup directory.
    exit /b 1
)

rem ensure that backup_dir is not a sub dir of cleanup_dir
if not "!BACKUP_DIR:%CLEANUP_DIR%=!" == "%BACKUP_DIR%" (
    echo.backup directory must not be a sub directory of cleanup directory.
    exit /b 1
)

rem iterate recursively thru files in cleanup_dir
for /R "%CLEANUP_DIR%" %%F in (*) do (
    set FILE_PATH=%%F
    set BACKUP_FILE_PATH=!FILE_PATH:%CLEANUP_DIR%=%BACKUP_DIR%!
    if exist "!BACKUP_FILE_PATH!" (
        rem binary compare file to file in backup dir
        fc /B "!FILE_PATH!" "!BACKUP_FILE_PATH!" >NUL 2>&1
        if not errorlevel 1 (
            rem if files are identical delete file from cleanup_dir
            echo.delete redundant "!FILE_PATH!".
            del "!FILE_PATH!"
        ) else (
            echo.keep modified "!FILE_PATH!".
        )
    ) else (
        echo.keep added "!FILE_PATH!".
    )
)
讽刺将军 2024-09-17 07:57:01

我对 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.

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