Sign in to follow this  
Followers 0
Tsukihime

Replace all instances in a file

3 posts in this topic

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Tsukihime

Share this post


Link to post
Share on other sites

#3 ·  Posted (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:

$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. :unsure:

Any help? :>

M23

Edit: Our posts crossed.

Not sure how it will handle large files though

You have your answer! ;D Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0