2011-01-06 22 views
1

私は数千のテキストファイルを処理する必要があるディレクトリがあります。これらのファイルのいくつかは同一ですが、タイムスタンプが数秒/ミリ秒で変化することを除いて、他のファイルは同じです。私は、同一のファイルの削除を自動化するための方法が必要で、1つのコピーだけを保持しています。内容によって同一のファイルを削除するDOSスクリプト

私のようなものを考えています:

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でさえも可能にこのようなものですか?

答えて

7

もちろんです。すべてがバッチで可能です。 :D

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

CleanDuplicates.batとしてコードを保存し、任意の保証なしに、AS IS提供CleanDuplicates {Folder}

でプログラムを開始!あなたのサーバー全体が台無しになっているので、私はあなたのドアをノックしてはいけません。 ;-)

実際には、コード自体が再帰的に呼び出されます。これはおそらく別の方法で行うことができますが、ちょっと、それは動作します。また、それは新しいクライアントで再び始まります。クリーンアップが容易になるからです。私はWindows Vista Businessでスクリプトをテストしましたが、Server 2003でも動作するはずです。ちょっと、ヘルプ機能もあります。 ;-) このファイルには、すべてのファイルを返す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 
+0

答えはいいですか?そうでない場合、何が間違っていますか?私はそれを改善することはできますか? – GolezTrol

+0

それはトリックです!私は現在、重複する同一ファイルを削除するように変更しています。スクリプトが異なる2つのファイルを見つけたときに次のファイルに移動するように変更することは可能ですか?そして、ファイルリストの先頭にループバックするのではなく、それを次のシーケンシャルファイルと比較するだけですか?今、すべてのファイル(O^2)と比較していますが、2つのファイルが異なる場合、後続のファイルは同じではありません。 – tridium

+0

はい、良い解決策はここにあります:http://stackoverflow.com/questions/1184408/exit-in-for-loop-windows-command-processor-cmd-exe – GolezTrol

関連する問題