1

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

我在想类似的东西:

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 中是否可能发生这样的事情?

4

1 回答 1

7

当然是。批量化一切皆有可能。: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
于 2011-01-06T20:27:10.887 回答