Loop (ファイル検索)
提供: AutoHotkey Wiki
実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 |
キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル |
レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト
Loop (ファイル検索)[編集]
パターンに一致するファイルそれぞれについて繰り返えしを行う。
Loop, FilePattern [, IncludeFolders?, Recurse?]
Parameters[編集]
引数名 | 説明 | |
---|---|---|
FilePattern | ファイル名、フォルダ名、ワイルドカード。 相対パスで指定した場合は、%A_WorkingDir%から検索される。 | |
IncludeFolders? | デフォルトは 0 。
| |
0 | ファイルのみを対象とする。 | |
1 | ファイルとフォルダを対象とする。 | |
2 | フォルダのみを対象とする。 | |
Recurse? | デフォルトは 0 。
| |
0 | サブフォルダ内は検索しない。 | |
1 | 全てのサブフォルダを再帰的に検索し、パターンにマッチするものを処理する。 |
Remarks[編集]
NTFSでは、ファイルは名前順に処理される。 FATでは、処理される順番は不定。 ファイルのフルパスが259文字以上のファイルは無視される。
処理対象になっているファイルの情報は、以下の組み込み変数として参照できる。
[重要] 本家の公式版やAHKLのANSI版の利用時で、日本語のファイルパスを含む場合組み込み変数の結果が不正なものとなる場合がある。
詳しくは 日本語を扱う上での不具合を参照のこと。
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 と同様。
Related[編集]
Loop, Break, Continue, Blocks, FileSetAttrib, FileSetTime
Example(s)[編集]
; 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% }