バッチコマンドのforfiles構文の詳細な例



Detailed Examples Batch Command Forfiles Syntax



まず、forfilesはWindows Server 2003に組み込まれているコマンドラインファイルであり、他のシステムには適用できない場合があることに注意してください。
ただし、win7、Windows Server 2008など、win2003以降のシステムは適用可能です。

forfilesの例を見てください。



1.バッチファイルを使用して、現在のディレクトリにある拡張子が7日前のbkfファイルを削除します(現在のシステム時刻に基づく)
例えば:

forfiles /m *.bkf /d -7 /c 'cmd /c del @file /f'

2. forfilesは、7日前にファイルを自動的に削除します(現在のシステム時刻に基づく)
例:



forfiles /p 'd: est' /s /m *.* /d -7 /c 'cmd /c del @path'

d: testを必要なディレクトリパスに置き換えます/ d -7は、7日前にファイルを削除することを意味します。

3.すべての空のディレクトリを削除します(例としてd: testディレクトリを削除します):

dir /ad/b/s d: est |sort /r &gtd:kill.txt For /f 'tokens=*' %%i in (d:kill.txt) DO rd '%%i' del d:kill.txt

4.最初に7日前にファイルを削除し、次にすべての空のディレクトリを削除して、以下をbatファイルにコピーします。



@echo off forfiles /p 'd: est' /s /m *.* /d -7 /c 'cmd /c del @path' dir /ad/b/s d: est |sort /r &gtd:kill.txt For /f 'tokens=*' %%i in (d:kill.txt) DO rd '%%i' del d:kill.txt

5. forfilesコマンドの使用法とパラメーター:
forfiles / p / d / c
/ pは、ファイルを検索するディレクトリを指定します。デフォルトは、現在の作業ディレクトリです。
/ dファイルの最終変更日が条件を満たしているかどうかを比較する日付または日数を指定します。
/ c見つかったファイルごとに実行されるコマンド。

例1.Cドライブのルートディレクトリにある2010年1月7日以降の最終変更日を持つファイルを、Dドライブのルートディレクトリにコピーするには、次の手順を実行します。

forfiles /p 'c:' /d '2007-7-1' /c 'cmd /c copy @path d:'

例2.Cドライブのバックアップディレクトリにある最終変更日が10日前のファイルを削除します。

forfiles /p 'c:ackup' /d -10 /c 'cmd /c echo deleting @file ... && del /f @path'

6.6。forfiles / pには、削除するファイルのフルパスが含まれます(例:F: Logfiles)/ m * .log -d -7 / c'cmd / c del / f @path '

関連するパラメータとコマンド
/ p:ファイルの検索を開始する場所を指定します。指定しない場合、デフォルトで現在のディレクトリになります。
/ m:コード内の「* .log」など、ファイル検索に使用されるワイルドカードは、すべてのログファイルを意味します。もちろん、「manmee _ *。log」のように指定することもできます。manmeeで始まるすべてのログファイル。このパラメーターが指定されていない場合、デフォルトは「*。*」です。
/ d [-] []:選択したいファイルの最終変更時刻を指定します。上記では、「/ d-7」が使用されています。当日と7日前に基づくすべてのドキュメントを示します。もちろん、ここで特定の時刻を指定することもできます。たとえば、「/ d -01/7/2010」のように、すべてのファイルが2010年1月7日より前になります。指定する時刻は「MM /」の形式である必要があります。 DD / YYYY '。

/ cは、指定されたコマンドをすべてのファイルに対して実行します。コマンドの本文は二重引用符( ')で囲む必要があります。デフォルトは' cmd / c echo @ file 'です。上記は 'cmd / c del / f @ path'です。指定されたファイルを削除します。 (ここで、@ fileと@pathは変数であり、以下で説明します。)

上記で使用されたパラメーター:
@PATH:ファイルの完全なパスを示します。
@File:ファイル名を示します。

フォルダを削除する操作を紹介しましょう。
forfiles / pには、フォルダーのパスが含まれます(例:F:)/ mフォルダー名(例:LogFiles)-d 0 / c'cmd / c if @ ISDIR == true rd / s / q @path '

ここでの「フォルダを含めるパス」には、削除するフォルダを含めることはできないことに注意してください。上記のコードが示すように、FドライブでLogFilesという名前のファイルまたはフォルダーを検索します(検索フォルダーを指定することはできませんが、削除するときに判断しました)。
現在のファイルタイプが「フォルダタイプ」であるかどうかを判断するために使用される新しいパラメータ「@ISDIR」があります。そうである場合はtrue、そうでない場合はfalseです。
最後に、コードをバッチファイルとして保存し、スケジュールされたタスクを追加して定期的に実行します。

バッチファイルの内容の段落を共有します。

@echo off rem ****************************** rem * Batch processing of deleting file directories by time * rem ****************************** rem set the path of the temporary directory set tempDir=%tmp% emove_%date:~0,10% if not exist %tempDir% md %tempDir% rem sets the path of the script file for processing the date set scriptFile=%tempDir%get_date.vbs rem gets the number of days to keep set days=%~1 if '%days%' == '' goto printUsage rem get the path of the target directory set dirPath=%~2 if '%dirPath%' == '' set dirPath=. rem get the file format to be operated set fileSpec=%~3 if '%fileSpec%' == '' set fileSpec=*.* rem generates a script file that calculates the date and obtains the deadline for deletion echo d=date()-%1 &gt %scriptFile% echo s=right('0000' ^&amp year(d),4) ^&amp '-' ^&amp right('00' ^&amp month(d),2) ^&amp '-' ^&amp right('00'^&amp day(d),2) &gt&gt %scriptFile% echo wscript.echo s &gt&gt %scriptFile% for /f %%i in ('cscript /nologo %scriptFile%') do set lastDate=%%i rem processes each object in the target directory for /f 'tokens=1,2,3* delims=<> ' %%i in ('dir'%dirPath%\%fileSpec%' /a /-c /tc') do call :proc '%%i' '%%j' '%%k' '%%l' goto :done rem process of processing objects in the target directory :proc rem get the creation date of the object and determine whether it is a valid format set fileDate=%~1 echo %fileDate% | findstr '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' &gt nul if errorlevel 1 goto end rem gets the type of object set fileType=%~3 if '%fileType%' == '' goto end rem gets the name of the object set fileName=%~4 if '%fileName%' == '' goto end if '%fileName%' == '.' goto end if '%fileName%' == '..' goto end if '%fileName%' == 'byte' goto end if '%fileName%' == 'Available bytes' goto end rem determines whether the object date is less than or equal to the deadline for deletion if '%fileDate:~0,10%' leq '%lastDate%' ( echo deleting '%fileName%' ... if '%fileType%' == 'DIR' ( rd /s /q '%dirPath%\%fileName%' ) else ( del /q /f '%dirPath%% fileName%' ) ) goto end :error echo An error occurred during backuping. :done rd /s /q %tempDir% goto end :printUsage echo Usage: %0 ^&ltDays^&gt [Work directory] [Target file specification (can include wildcards)] goto end :end

上記のコードは、Windowsのスクリプト関数を使用して削除するファイルの有効期限を計算し、次にforとdirコマンドを使用してファイルの日付を抽出して判断します。
forfilesおよびforの詳細については、Windowsのヘルプおよびサポートドキュメントを参照してください。使用法については、forfiles /?を使用できます。理解する。