DOS脚本按内容删除相同的文件

发布于 2024-10-10 17:02:42 字数 448 浏览 0 评论 0原文

我有一个目录,其中包含需要处理的数千个文本文件。其中一些文件是相同的,而其他文件是相同的,只是时间戳有几秒/毫秒的变化。我需要某种方法来自动删除相同的文件并只保留一个副本。

我在想这样的事情:

while there are files in the directory still
{
    get file                    // e.g., file0001

    while (file == file + 1)    // e.g., file0001 == file0002 using 'fc' command
    {
        delete file + 1
    }

    move file to another directory
}

在 Microsoft Windows Server 2003 的 DOS 中,这样的事情可能吗?

I have a directory with several thousand text files that I need to process. Some of these files are identical while others are identical except the timestamp varies by a few seconds / milliseconds. I need some way to automate the deletion of identical files and only keep one copy.

I'm thinking of something like:

while there are files in the directory still
{
    get file                    // e.g., file0001

    while (file == file + 1)    // e.g., file0001 == file0002 using 'fc' command
    {
        delete file + 1
    }

    move file to another directory
}

Is something like this even possible in Microsoft Windows Server 2003's DOS?

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

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

发布评论

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

评论(1

天暗了我发光 2024-10-17 17:02:42

当然是。批量化一切皆有可能。 :D

这个批处理实际上并没有删除文件。它只是呼应了比较的结果。如果发现两个文件相同,则可以删除其中一个文件。

将代码另存为 CleanDuplicates.bat 并使用 CleanDuplicates {Folder} 启动程序

按原样提供,没有任何保证!我不想让你敲我的门,因为你的整个服务器都搞砸了。 ;-)

该代码实际上递归地调用自身。这也许可以用不同的方式来完成,但是嘿,它有效。它还会在新的 cmd 中重新启动,因为这使得清理变得更容易。我在 Windows Vista Business 中测试了该脚本,但它也应该可以在 Server 2003 上运行。嘿,它甚至还有帮助功能。 ;-)
它包含两个循环,每个循环返回每个文件,因此当您实现实际删除时,它可能会报告某些文件不存在,因为它们在较早的迭代中被删除。

@echo off
rem Check input. //, /// and //// are special parameters. No parameter -> help.
if %1check==//check goto innerloop
if %1check==///check goto compare
if %1check==////check goto shell
if %1check==/hcheck goto help
if %1check==check goto help

rem Start ourselves within a new cmd shell. This will automatically return to
rem the original directory, and clear our helper environment vars.
cmd /c %0 //// %1
echo Exiting
goto end

:shell
rem Save the current folder, jump to target folder and set some helper vars
set FCOrgFolder=%CD%
cd %2
set FCStartPath=%0
if not exist %FCStartPath% set FCStartPath=%FCOrgFolder%\%0

rem Outer loop. Get each file and call ourselves with the first special parameter.
for %%a in (*.*) do call %FCStartPath% // "%2" "%%a"

goto end

:innerloop
rem Get each file again and call ourselves again with the second special parameter.
for %%b in (*.*) do call %FCStartPath% /// %2 %3 "%%b"
goto end

:compare
rem Actual compare and some verbose.
if %3==%4 goto end
echo Comparing
echo * %3
echo * %4

fc %3 %4 >nul

rem Get results
if errorlevel 2 goto notexists
if errorlevel 1 goto different

echo Files are identical
goto end

:different
echo Files differ
goto end

:notexists
echo File does not exist
goto end

:help

echo Compares files within a directory.
echo Usage: %0 {directory}
goto end

:end

Of course it is. Everything is possible in batch. :D

This batch doesn't actually delete files. It just echos the result of the comparison. You can delete either one of the files if you find two that are the same.

Save the code as CleanDuplicates.bat and start the program with CleanDuplicates {Folder}

Provided AS IS, without any guarantees! I don't want you knocking on my door because your entire server is messed up. ;-)

The code actually calls itself recursively. This could maybe be done in a different way but hey, it works. It also starts itself again in a new cmd, because that makes cleaning up easier. I tested the script in Windows Vista Business, but it should work on Server 2003 as well. Hey, it even has a help function. ;-)
It contains two loops that each return every file, so when you implement the actual deleting, it may report that some files don't exist, because they are deleted in an earlier iteration.

@echo off
rem Check input. //, /// and //// are special parameters. No parameter -> help.
if %1check==//check goto innerloop
if %1check==///check goto compare
if %1check==////check goto shell
if %1check==/hcheck goto help
if %1check==check goto help

rem Start ourselves within a new cmd shell. This will automatically return to
rem the original directory, and clear our helper environment vars.
cmd /c %0 //// %1
echo Exiting
goto end

:shell
rem Save the current folder, jump to target folder and set some helper vars
set FCOrgFolder=%CD%
cd %2
set FCStartPath=%0
if not exist %FCStartPath% set FCStartPath=%FCOrgFolder%\%0

rem Outer loop. Get each file and call ourselves with the first special parameter.
for %%a in (*.*) do call %FCStartPath% // "%2" "%%a"

goto end

:innerloop
rem Get each file again and call ourselves again with the second special parameter.
for %%b in (*.*) do call %FCStartPath% /// %2 %3 "%%b"
goto end

:compare
rem Actual compare and some verbose.
if %3==%4 goto end
echo Comparing
echo * %3
echo * %4

fc %3 %4 >nul

rem Get results
if errorlevel 2 goto notexists
if errorlevel 1 goto different

echo Files are identical
goto end

:different
echo Files differ
goto end

:notexists
echo File does not exist
goto end

:help

echo Compares files within a directory.
echo Usage: %0 {directory}
goto end

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