Tsukihime Posted April 27, 2011 Share Posted April 27, 2011 I have a file with thousands of lines and want to replace specific patterns of strings. So I will use regexpreplace for that. I have already come up with the pattern, and everything works. Except.....I think it could be faster. I'm currently just reading lines from the source, doing the replace, and then writing it out to a file, but is there a way to just grab all lines at once as one huge string, run the regex (which I expect is much faster than file I/O), then write everything out at once? Link to comment Share on other sites More sharing options...
Tsukihime Posted April 27, 2011 Author Share Posted April 27, 2011 (edited) Found a solution using FileRead. It seems to read all bytes by default if I don't provide a count. Not sure how it will handle large files though. Edited April 27, 2011 by Tsukihime Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 27, 2011 Moderators Share Posted April 27, 2011 (edited) Tsukihime, I fear you might run into RAM problems doing that as you will have at least 2 copies (original and amended) of the file in memory. You might try using FileRead with a fairly large buffer size along these lines: expandcollapse popup$sFileIn = "Fred.txt" $sFileOut = "Harry.txt" ; Create a large file $sString = "My Name is Fred Bloggs " For $i = 1 To 20 $sString &= $sString Next ConsoleWrite(StringLen($sString) & @CRLF) FileWrite($sFileIn, $sString) ; Open input and output files $hFileInHandle = FileOpen(@ScriptDir & "\" & $sFileIn, 0) $hFileOutHandle = FileOpen(@ScriptDir & "\" & $sFileOut, 2) $iRemaining = FileGetSize($sFileIn) $iTotal = 0 $iBufferSize = 20000 ; Read in by chunks For $i = 1 To Ceiling($iRemaining / $iBufferSize) If $iRemaining > $iBufferSize Then ConsoleWrite($i & @CRLF) $sSection = FileRead($hFileInHandle, $iBufferSize) ; Change what you need $sNewSection = StringReplace($sSection, "Fred", "Harry") ; Write to output FileWrite($hFileOutHandle, $sNewSection) $iRemaining -= $iBufferSize $iTotal += $iBufferSize Else ConsoleWrite("Final" & @CRLF) $sSection = FileRead($hFileInHandle, $iRemaining) $sNewSection = StringReplace($sSection, "Fred", "Harry") FileWrite($hFileOutHandle, $sNewSection) $iTotal += $iRemaining EndIf Next ; Close files FileClose($hFileInHandle) FileClose($hFileOutHandle) You can obviously adjust the buffer size to match your available RAM. Any help? M23 Edit: Our posts crossed. Not sure how it will handle large files thoughYou have your answer! ;D Edited April 27, 2011 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now