テキストファイルを1行ずつ読み込み、繰り返し処理を行う
Loop, Read, InputFile [, OutputFile, FutureUse]
| 引数名 | 説明 |
|---|---|
| Read | 第一引数は「READ」とする。変数に格納して参照してもかまわない。 |
| InputFile | 読み込みたいファイル名。 相対パスで指定すると、%A_WorkingDir% から検索される。 |
| OutputFile |
ループの間、FileAppendコマンドを第2引数を省略して実行すると、このファイルに書き込まれる。 ループ中で最初にFileAppendを使用したときに開かれ、その後ループを抜けるまで開いたままになる。 コマンドを実行するたびに開き直すことが無いので、第2引数をつけて実行するよりパフォーマンスがよくなる。 相対パスで指定すると、%A_WorkingDir% から検索される。 ファイル名の先頭に「*」をつけると、バイナリモードで開かれ、改行コードの自動変換(LF→CR+LF)が行われなくなる。 ファイル名を「*」とすると、第2引数なしのFileAppendでスクリプトの標準出力に文字列を出力できる。 |
| FutureUse | 将来の拡張のために確保されている。今のところこの引数は無視される。 |
読み出された行(改行は含まない)は「A_LoopReadLine」として参照できる。
65534文字を超える行は、複数回に分けて読み込まれる。
改行コードは、CR+LFとLFに対応(CRには非対応)
ファイルの各行を読み出すには、FileReadLineコマンドを使う方法もあるが、Loop, Readを使うほうが効率がよい。
読み込んだ行をStringSplitコマンドやLoop,PARSEで更に分割することもできる。
その他の仕様は、通常のLoopと同様。
FileRead, FileReadLine, FileAppend, Sort, Loop, Break, Continue, Blocks, FileSetAttrib, FileSetTime
; Example #1: Only those lines of the 1st file that contain
; the word FAMILY will be written to the 2nd file. Uncomment
; this line to overwrite rather than append to any existing file:
;FileDelete, C:\Docs\Family Addresses.txt
Loop, read, C:\Docs\Address List.txt, C:\Docs\Family Addresses.txt
{
IfInString, A_LoopReadLine, family, FileAppend, %A_LoopReadLine%`n
}
; Example #2: A working script that attempts to extract all FTP and HTTP
; URLs from a text or HTML file:
FileSelectFile, SourceFile, 3,, Pick a text or HTML file to analyze.
if SourceFile =
return ; This will exit in this case.
SplitPath, SourceFile,, SourceFilePath,, SourceFileNoExt
DestFile = %SourceFilePath%\%SourceFileNoExt% Extracted Links.txt
IfExist, %DestFile%
{
MsgBox, 4,, Overwrite the existing links file? Press No to append to it.`n`nFILE: %DestFile%
IfMsgBox, Yes
FileDelete, %DestFile%
}
LinkCount = 0
Loop, read, %SourceFile%, %DestFile%
{
URLSearchString = %A_LoopReadLine%
Gosub, URLSearch
}
MsgBox %LinkCount% links were found and written to "%DestFile%".
return
URLSearch:
; It's done this particular way because some URLs have other URLs embedded inside them:
StringGetPos, URLStart1, URLSearchString, http://
StringGetPos, URLStart2, URLSearchString, ftp://
StringGetPos, URLStart3, URLSearchString, www.
; Find the left-most starting position:
URLStart = %URLStart1% ; Set starting default.
Loop
{
; It helps performance (at least in a script with many variables) to resolve
; "URLStart%A_Index%" only once:
StringTrimLeft, ArrayElement, URLStart%A_Index%, 0
if ArrayElement = ; End of the array has been reached.
break
if ArrayElement = -1 ; This element is disqualified.
continue
if URLStart = -1
URLStart = %ArrayElement%
else ; URLStart has a valid position in it, so compare it with ArrayElement.
{
if ArrayElement <> -1
if ArrayElement < %URLStart%
URLStart = %ArrayElement%
}
}
if URLStart = -1 ; No URLs exist in URLSearchString.
return
; Otherwise, extract this URL:
StringTrimLeft, URL, URLSearchString, %URLStart% ; Omit the beginning/irrelevant part.
Loop, parse, URL, %A_Tab%%A_Space%<> ; Find the first space, tab, or angle (if any).
{
URL = %A_LoopField%
break ; i.e. perform only one loop iteration to fetch the first "field".
}
; If the above loop had zero iterations because there were no ending characters found,
; leave the contents of the URL var untouched.
; If the URL ends in a double quote, remove it. For now, StringReplace is used, but
; note that it seems that double quotes can legitimately exist inside URLs, so this
; might damage them:
StringReplace, URLCleansed, URL, ",, All
FileAppend, %URLCleansed%`n
LinkCount += 1
; See if there are any other URLs in this line:
StringLen, CharactersToOmit, URL
CharactersToOmit += %URLStart%
StringTrimLeft, URLSearchString, URLSearchString, %CharactersToOmit%
Gosub, URLSearch ; Recursive call to self.
return