選取並執行某個檔案或一組檔案的命令。Selects and runs a command on a file or set of files. 此命令最常用於批次檔中。This command is most commonly used in batch files.


forfiles [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] [{<date> | <days>}]]


參數Parameter 描述Description
/P <pathname>/P <pathname> 指定開始搜尋的路徑。Specifies the path from which to start the search. 依預設,搜尋會在目前的工作目錄中開始。By default, searching starts in the current working directory.
一樣 <searchmask>/M <searchmask> 根據指定的搜尋遮罩來搜尋檔案。Searches files according to the specified search mask. 預設 searchmask 為 *The default searchmask is *.
/S/S 指示 forfiles 命令以遞迴方式在子目錄中搜尋。Instructs the forfiles command to search in subdirectories recursively.
/C <command>/C <command> 在每個檔案上執行指定的命令。Runs the specified command on each file. 命令字串應以雙引號括住。Command strings should be wrapped in double quotes. 預設的命令為 "cmd /c echo @file"The default command is "cmd /c echo @file".
/D [{+\|-}][{<date> | <days>}]/D [{+\|-}][{<date> | <days>}] 選取在指定時間範圍內最後修改日期的檔案:Selects files with a last modified date within the specified time frame:
  • 選取上次修改日期晚于或等於 (+) 或早于或等於 (-) 指定日期的檔案,其中 日期 的格式為 MM/DD/YYYY。Selects files with a last modified date later than or equal to (+) or earlier than or equal to (-) the specified date, where date is in the format MM/DD/YYYY.
  • 選取上次修改日期晚于或等於 (+) 目前的日期加上指定的天數,或早于或等於 (-) 目前的日期減去指定的天數。Selects files with a last modified date later than or equal to (+) the current date plus the number of days specified, or earlier than or equal to (-) the current date minus the number of days specified.
  • 有效的 天數 值包含0到32768範圍中的任何數位。Valid values for days include any number in the range 0–32,768. 如果未指定正負號, + 預設會使用。If no sign is specified, + is used by default.
/?/? 在 cmd 視窗中顯示解說文字。Displays the help text in the cmd window.


  • forfiles /S 命令類似于 dir /SThe forfiles /S command is similar to dir /S.

  • 您可以使用命令字串中的下列變數,如 /C 命令列選項所指定:You can use the following variables in the command string as specified by the /C command-line option:

    變數Variable 描述Description
    @FILE 檔案名稱File name.
    @FNAME 不含副檔名的檔案名。File name without extension.
    @EXT 副檔名。File name extension.
    @PATH 檔案的完整路徑。Full path of the file.
    @RELPATH 檔案的相對路徑。Relative path of the file.
    @ISDIR 如果檔案類型是目錄,則評估結果為 TRUE。Evaluates to TRUE if a file type is a directory. 否則,此變數會評估為 FALSE。Otherwise, this variable evaluates to FALSE.
    @FSIZE 檔案大小(以位元組為單位)。File size, in bytes.
    @FDATE 檔案的上次修改日期戳記。Last modified date stamp on the file.
    @FTIME 上次修改檔案的時間戳記。Last modified time stamp on the file.
  • Forfiles命令可讓您在上執行命令,或將引數傳遞至多個檔案。The forfiles command lets you run a command on or pass arguments to multiple files. 例如,您可以對副檔名為 .txt 的樹狀目錄中的所有檔案執行 類型 命令。For example, you could run the type command on all files in a tree with the .txt file name extension. 或者,您可以在磁片磁碟機 C 上執行每個批次檔 ( * .bat) ,其檔案名 Myinput.txt 為第一個引數。Or you could execute every batch file (*.bat) on drive C, with the file name Myinput.txt as the first argument.

  • 此命令可以:This command can:

    • 使用 /d 參數,依絕對日期或相對日期來選取檔案。Select files by an absolute date or a relative date by using the /d parameter.

    • 使用和之類的變數來建立檔案的封存樹狀 @FSIZE 目錄 @FDATE 。Build an archive tree of files by using variables such as @FSIZE and @FDATE.

    • 使用變數區分目錄中的檔案 @ISDIR 。Differentiate files from directories by using the @ISDIR variable.

    • 使用字元的十六進位代碼,在命令列中包含特殊字元(使用 0xHH 格式) (例如,0x09 用於索引標籤) 。Include special characters in the command line by using the hexadecimal code for the character, in 0xHH format (for example, 0x09 for a tab).

  • 此命令的運作方式是 recurse subdirectories 在設計用來處理單一檔案的工具上執行旗標。This command works by implementing the recurse subdirectories flag on tools that are designed to process only a single file.


若要列出磁片磁碟機 C 上的所有批次檔,請輸入:To list all of the batch files on drive C, type:

forfiles /P c:\ /S /M *.bat /C "cmd /c echo @file is a batch file"

若要列出磁片磁碟機 C 上的所有目錄,請輸入:To list all of the directories on drive C, type:

forfiles /P c:\ /S /M *.* /C "cmd /c if @isdir==TRUE echo @file is a directory"

若要列出目前目錄中至少有一年的所有檔案,請輸入:To list all of the files in the current directory that are at least one year old, type:

forfiles /S /M *.* /D -365 /C "cmd /c echo @file is at least one year old."

若要顯示目前目錄中的每個檔案在2007年1月1日之前的每個檔案 都已過期的文字檔, 請輸入:To display the text file is outdated for each of the files in the current directory that are older than January 1, 2007, type:

forfiles /S /M *.* /D -01/01/2007 /C "cmd /c echo @file is outdated."

若要以資料行格式列出目前目錄中所有檔案的副檔名,並在擴充功能前面加上一個索引標籤,請輸入:To list the file name extensions of all the files in the current directory in column format, and add a tab before the extension, type:

forfiles /S /M *.* /C "cmd /c echo The extension of @file is 0x09@ext"

其他參考資料Additional References