パターンに一致するファイルそれぞれについて繰り返えしを行う
Loop, FilePattern [, IncludeFolders?, Recurse?]
| 引数名 | 説明 |
|---|---|
| FilePattern |
ファイル名、フォルダ名、ワイルドカード。 相対パスで指定した場合は、%A_WorkingDir%から検索される。 |
| IncludeFolders? |
「0」ならファイルのみを対象とする。 「1」ならファイルとフォルダを対象とする。 「2」ならフォルダのみを対象とする。 デフォルトは「0」。 |
| Recurse? |
「0」ならサブフォルダ内は検索しない。 「1」なら全てのサブフォルダを再帰的に検索し、パターンにマッチするものを処理する。 デフォルトは「0」。 |
NTFSでは、ファイルは名前順に処理される。
FATでは、処理される順番は不定。
ファイルのフルパスが259文字以上のファイルは無視される。
処理対象になっているファイルの情報は、以下の組み込み変数として参照できる。
| A_LoopFileName | ファイル名(ディレクトリパスを除く) |
| A_LoopFileExt | ファイルの拡張子。 「.」は含まない(例:「txt」) |
| A_LoopFileFullPath | ファイルのフルパス。 ただし、FilePatternで相対パスを指定した場合は、こちらも相対パスになる。 |
| A_LoopFileLongPath | ロングファイルネーム形式の、正式なフルパス。 |
| A_LoopFileShortPath | ファイルの8.3形式の短いパス。 ただし、FilePatternで相対パスを指定した場合は、こちらも相対パスになる。 NTFSでショートパスの生成を無効にしている場合、この変数は空になる。 |
| A_LoopFileShortName |
8.3形式のショートファイルネーム。 NTFSのショートファイルネーム生成が無効になっているなどの理由により取得に失敗した場合は、A_LoopFileNameと同じものになる。 |
| A_LoopFileDir |
ファイルのあるディレクトリ。 ただし、FilePatternで相対パスを指定した場合は、こちらも相対パスになる。 |
| A_LoopFileTimeModified | ファイルの最終更新日時。YYYYMMDDHH24MISS形式。 |
| A_LoopFileTimeCreated | ファイルの作られた日時。YYYYMMDDHH24MISS形式。 |
| A_LoopFileTimeAccessed | ファイルの最終アクセス日時。YYYYMMDDHH24MISS形式。 |
| A_LoopFileAttrib | ファイルの属性。FileGetAttribで取得されるものと同じ。 |
| A_LoopFileSize | ファイルサイズ(Byte単位)。4ギガバイト以上のサイズにも対応。 |
| A_LoopFileSizeKB | ファイルのサイズ(KB単位) |
| A_LoopFileSizeMB | ファイルのサイズ(MB単位) |
その他の仕様は、通常のLoopと同様。
Loop, Break, Continue, Blocks, FileSetAttrib, FileSetTime
; Example #1:
Loop, %ProgramFiles%\*.txt, , 1 ; Recurse into subfolders.
{
MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
IfMsgBox, No
break
}
; Example #2: Calculate the size of a folder, including the files in all its subfolders: SetBatchLines, -1 ; Make the operation run at maximum speed. FolderSizeKB = 0 FileSelectFolder, WhichFolder Loop, %WhichFolder%\*.*, , 1 FolderSizeKB += %A_LoopFileSizeKB% MsgBox Size of %WhichFolder% is %FolderSizeKB% KB.
; Example #3: Retrieve file names sorted by name (see next example to sort by date):
FileList = ; Initialize to be blank.
Loop, C:\*.*
FileList = %FileList%%A_LoopFileName%`n
Sort, FileList, R ; The R option sorts in reverse order. See Sort for other options.
Loop, parse, FileList, `n
{
if A_LoopField = ; Ignore the blank item at the end of the list.
continue
MsgBox, 4,, File number %A_Index% is %A_LoopField%. Continue?
IfMsgBox, No
break
}
; Example #4: Retrieve file names sorted by modification date:
FileList =
Loop, %UserProfile%\Recent\*.*, 1 ; UserProfile exists as an environment variable on some OSes.
FileList = %FileList%%A_LoopFileTimeModified%`t%A_LoopFileName%`n
Sort, FileList ; Sort by date.
Loop, parse, FileList, `n
{
if A_LoopField = ; Omit the last linefeed (blank item) at the end of the list.
continue
StringSplit, FileItem, A_LoopField, %A_Tab% ; Split into two parts at the tab char.
MsgBox, 4,, The next file (modified at %FileItem1%) is:`n%FileItem2%`n`nContinue?
IfMsgBox, No
break
}
; Example #5: Copy only the source files that are newer than their counterparts
; in the destination:
CopyIfNewer:
; Caller has set the variables CopySourcePattern and CopyDest for us.
Loop, %CopySourcePattern%
{
copy_it = n
IfNotExist, %CopyDest%\%A_LoopFileName% ; Always copy if target file doesn't yet exist.
copy_it = y
else
{
FileGetTime, time, %CopyDest%\%A_LoopFileName%
EnvSub, time, %A_LoopFileTimeModified%, seconds ; Subtract the source file's time from the destination's.
if time < 0 ; Source file is newer than destination file.
copy_it = y
}
if copy_it = y
{
FileCopy, %A_LoopFileFullPath%, %CopyDest%\%A_LoopFileName%, 1 ; Copy with overwrite=yes
if ErrorLevel <> 0
MsgBox, Could not copy "%A_LoopFileFullPath%" to "%CopyDest%\%A_LoopFileName%".
}
}
Return
; Example #6: Convert filenames passed in via command-line parameters to long names,
; complete path, and correct uppercase/lowercase characters as stored in the file system.
; This script requires v1.0.25+.
Loop %0% ; For each file dropped onto the script (or passed as a parameter).
{
GivenPath := %A_Index% ; Retrieve the next command line parameter.
Loop %GivenPath%
LongPath = %A_LoopFileLongPath%
MsgBox The case-corrected long path name of file`n%GivenPath%`nis:`n%LongPath%
}