2017-02-10 27 views
0

我试图使用AutoIt从多个大小超过500 MB的日志文件中提取文本,并且要提取的文本位于excel列中。我遇到了关于内存错误的FileRead问题。我甚至试过FileReadToArray,我认为这会让函数更容易处理巨大的字符串。所有文件的总大小大约为7.8 GB。最大的文件大小约为800 MB。使用excel中列的输入搜索日志文件中的文本

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 

该代码由jguinch在AutoIt论坛提供帮助。

+0

你可以尝试用grep的Windows版本或使用sed的正则表达式搜索文件 - https://sourceforge.net/projects/unxutils/?source=typ_redirect否则,你可能需要阅读一次写入一行 – Richard

回答

0

您可以一次读取一行文件以避免内存问题。

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 
相关问题