Loop,Read

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

Loop,Read[編集]

テキストファイルを1行ずつ読み込み、繰り返し処理を行う。

Loop, Read, InputFile [, OutputFile]

Parameters[編集]

引数名 説明
Read 第一引数は READとする。変数に格納して参照してもかまわない。
InputFile 読み込みたいファイル名。
相対パスで指定すると、%A_WorkingDir% から検索される。
OutputFile ループの間、FileAppendコマンドを第2引数を省略して実行すると、このファイルに書き込まれる。
ループ中で最初にFileAppendを使用したときに開かれ、その後ループを抜けるまで開いたままになる。
コマンドを実行するたびに開き直すことが無いので、第2引数をつけて実行するよりパフォーマンスがよくなる。
相対パスで指定すると、%A_WorkingDir% から検索される。
ファイル名の先頭に *をつけると、バイナリモードで開かれ、改行コードの自動変換(LF→CR+LF)が行われなくなる。
ファイル名を *とすると、第2引数なしのFileAppendでスクリプトの標準出力に文字列を出力できる。

AHKL コマンドのオプションしてのエンコードは指定できないが、あらかじめ FileEncoding で指定した形式で読み込むことが可能である。

Remarks[編集]

読み出された行(改行は含まない)は A_LoopReadLineとして参照できる。

65534文字を超える行は、複数回に分けて読み込まれる。

改行コードは、CR+LFとLFに対応(CRには非対応)

ファイルの各行を読み出すには、FileReadLineコマンドを使う方法もあるが、Loop, Readを使うほうが効率がよい。

読み込んだ行をStringSplitコマンドやLoop,Parseで更に分割することもできる。

その他の仕様は、通常のLoopと同様。

AHKL FileOpen() で作成したファイルオブジェクトでも同様の事が可能である。

Related[編集]

FileRead, FileReadLine, FileAppend, Sort, Loop, Break, Continue, Blocks, FileSetAttrib, FileSetTime

AHKL FileEncoding, FileOpen()

Example(s)[編集]

; 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