我正在尝试使用AutoIt从大小超过500 MB的多个日志文件中提取文本,并且要提取的文本位于Excel列中。我在使用文件读取时遇到问题,它抛出有关内存的错误。我什至尝试过FileReadToArray,我认为这将使函数更容易处理巨大的字符串。所有文件的总大小约为 7.8 GB。最大的文件大小约为 800 MB。
Global $aUserNames[] = _Excel_RangeRead($file,$Worksheet) ; Usernames need to be read from Excel
Global $sFolderPath = FileSelectFolder("Select Folder", "")
Global $aFileList = _FileListToArrayRec($sFolderPath, "*.*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If @error = 1 Then Exit MsgBox(0, "", "No Folders Found.")
If @error = 4 Then Exit MsgBox(0, "", "No Files Found.")
Local $sRegEx = "(?i)"
For $i = 0 To UBound($aUserNames) - 1
$sRegEx &= "b" & $aUserNames[$i] & "b|"
Next
$sRegEx = StringTrimRight($sRegEx, 1)
Global $Store
For $i = 1 To $aFileList[0]
$sFileContent = _FileReadToArray($aFileList[$i],$Store)
If StringRegExp($sFileContent, $sRegEx) Then MsgBox(0, "Info", "One of more users found in file " & $aFileList[$i])
Next
该代码由AutoIt论坛中的jguinch协助。
您可以一次读取一行文件以避免内存问题。
For $i = 1 To $aFileList[0]
$fileHandle = FileOpen($aFileList[$i])
While True
$fileLine = FileReadLine($fileHandle)
If @error Then Exitloop
If StringRegExp($fileLine , $sRegEx) Then MsgBox(0, "Info", "One of more users found in file " & $aFileList[$i])
WEnd
Next