2016-07-11 27 views
1

ファイルリストに対してフォルダを再帰的に作成するバッチファイルがあり、基本的に2つのレポートが作成されます。ファイルが存在する場合は1つ、ファイルが存在しない場合は1つです。 私が今したいのは、ファイルがディレクトリに存在していても、ファイルリストに存在しない(つまり、余分なファイルがある)かどうかを調べることです。ここでファイルリストに存在しないディレクトリにファイルが存在するかどうかを確認するバッチファイル

は私の現在のファイルは、標準のWindowsコンソールアプリケーションFINDSTRは間違いなく、このタスクには以下のコードで見ることができるように非常に良い選択のため..です

FOR /F "usebackqdelims=" %%f IN ("filelist.txt") DO (IF EXIST "%%f" (ECHO %%f exists >> "%~dp0\Exists.txt") ELSE (ECHO %%f doesn't exist >> "%~dp0\doesntexist.txt")) 
+0

あなたは 'findstr'コマンドに興味があるかもしれません:'ます。findstr/X/L/C: "FILE_NAME" "filelist.txtは、" ''場合には '0'の 'ErrorLevel'を返しfile_name'が持っていますそうでなければ 'filelist.txt'と' 1'の中に見つかりました。コマンドプロンプトウィンドウで 'findstr /?'と入力してください。 – aschipfl

答えて

1

です。

@echo off 
setlocal 
set "ListFiles=%~dp0FileList.txt" 
set "ListExist=%~dp0Exist.txt" 
set "ListMissing=%~dp0NotExist.txt" 
set "ListExtra=%~dp0Extra.txt" 
set "ListCurrent=%TEMP%\ListCurrent.tmp" 

rem Get list of all files in current directory and its subdirectories with 
rem full path into a temporary list file. If the current directory tree 
rem contains no file, delete empty list file and exit batch processing. 
dir /A-D /B /ON /S >"%ListCurrent%" 2>nul 

if errorlevel 1 (
    copy "%ListFiles%" "%ListMissing%" >nul 
    if exist "%ListExist%" del "%ListExist%" 
    if exist "%ListExtra%" del "%ListExtra%" 
    goto EndBatch 
) 

%SystemRoot%\System32\findstr.exe /I /L /X /G:"%ListFiles%" "%ListCurrent%" >"%ListExist%" 
%SystemRoot%\System32\findstr.exe /I /L /X /V /G:"%ListExist%" "%ListFiles%" >"%ListMissing%" 
%SystemRoot%\System32\findstr.exe /I /L /X /V /G:"%ListExist%" "%ListCurrent%" >"%ListExtra%" 

:EndBatch 
del "%ListCurrent%" 
endlocal 

このバッチコードは、バッチファイルのディレクトリにFileList.txtは、完全なパスとファイル名が含まれていることが必要です。

注:%~dp0は、バックスラッシュで既に終わっているバッチファイルのパスに展開されます。したがって、ファイル名の前に余分なバックスラッシュを指定しないでください。

使用されているコマンドとその動作方法を理解するには、コマンドプロンプトウィンドウを開き、次のコマンドを実行して、コマンドごとに表示されているすべてのヘルプページをすべてよく読んでください。

  • copy /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • findstr /? ...最も重要なリストの仕事をフィルタリングする方法を理解します。
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

>>nul2>nulの説明についてUsing command redirection operatorsに関するMicrosoftの記事を参照してください。

0
@echo off 
setlocal 

rem Load the list of filenames in *the subscripts* of an array 
FOR /F "usebackq delims=" %%f IN ("filelist.txt") DO set "name["%%f"]=1" 

rem Process files, remove elements of existent files from array 
for %%f in (*.*) do (
    IF defined name["%%f"] (
     ECHO %%f exists >> "%~dp0Exists.txt" 
     set "name["%%f"]=" 
    ) ELSE (
     ECHO %%f is extra >> "%~dp0ExtraFiles.txt" 
    ) 
) 

rem Report remaining elements in the array 
for /F "tokens=2 delims=[]" %%f in ('set name[') do (
    ECHO %%~f doesn't exist >> "%~dp0doesntexist.txt" 
) 
+0

ありがとうございます - これはほとんど動作します。問題は今、 'Extra files'出力は、バッチファイルと同じディレクトリにあるファイルのみを表示することです。 Exists/Does not Existレポートがありますが、私のファイルがあるディレクトリ構造を調べているようには見えません。 IN 'Filelist'txt'私は各ファイルへのパスを持っていますが、バッチファイル(つまり、folder1 \ folder2 \ file.txtのフルパスのみ)との関係でのみです。 – SwagBag

+0

Filelist.txtファイルにフルパスを追加しましたが同じ結果... – SwagBag

+0

あなたは 'filelist.txt'ファイルの内容を私たちに示していないので、推測しかできません。'for'コマンドは現在のディレクトリ内のファイルを処理するだけなので、'/R'スイッチをこのように追加する必要があります: '/ R %% f in(*。*)do('。テキストファイルは 'for/R'によって返された名前のフォーマットと同じフォーマットでなければなりません;コマンドプロンプトから' @echo%f'コマンドを使って 'for/R'を実行するだけです。 – Aacini

関連する問題