1

処理する必要のある数千のテキストファイルを含むディレクトリがあります。これらのファイルの一部は同一ですが、タイムスタンプが数秒/ミリ秒異なることを除いて他のファイルは同一です。同一のファイルの削除を自動化し、コピーを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

このバッチは実際にはファイルを削除しません。比較の結果をエコーするだけです。同じファイルが2つ見つかった場合は、どちらかのファイルを削除できます。

としてコードを保存しCleanDuplicates.bat、でプログラムを開始しますCleanDuplicates {Folder}

現状のまま提供され、保証はありません!サーバー全体が台無しになっているので、ドアをノックしてほしくない。;-)

コードは実際にそれ自体を再帰的に呼び出します。これは別の方法で行うこともできますが、うまくいきます。また、クリーンアップが容易になるため、新しいcmdで再起動します。スクリプトをWindowsVistaBusinessでテストしましたが、Server2003でも動作するはずです。ねえ、それもヘルプ機能を持っています。;-)それぞれがすべてのファイルを返す2つのループが含まれているため、実際の削除を実装すると、以前の反復で削除されたため、一部のファイルが存在しないことが報告される場合があります。

@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 に答える