Jump to content

Trim text in a file without storing the contents of anywhere

Recommended Posts

This example is a cacophony a concordant mixture of scripts and ideas that can previously be found on this thread.
This exercise allows for processing the file in chunks.    And, allows for the search string to be found when laying cross the boundary of two chunks.

Plus,  it's been a rainy Sunday.

#cs ; -------------------------- Script Description -------------------------------------
This example uses a file pointer (using the FileSetPos and FileGetPos functions) to read data in chunks from a file which is directly
appended to a temp file.   Each block of data is searched, for a specific text.   In the case where searched text is across a chunk boundary,
a second search is performed on an increase chunk size.   The chunk size is increased by the character length of the search text.    
When the search text position is found, the file pointer on the original "read" file is increased by the number of characters that make up
the search text.    So on the next FileRead, the search text will not be read because the file pointer will be pointing to the end of the
search text.   And subsequently, the search text will not be appended to the temp file.   
When the file pointer reaches the end of the "read" file, the temp file replaces the original file, keeping the original file's name.
#ce; -------------------------------------------------------------------------------------

Local $FileName = "1a.txt" ; <-------------------------------------######### Change to your file name.
Local $sSearchText = "456" ; <-------------------------------------######### Change to your search text.
Local $iChunkSize = 6      ; <-------------------------------------######### Change the chunk size (in text characters) that your file will be divided into.
; On my system using a 27.344 MB text file, with a 1000000 (1 million) $iChunkSize value, the $sSearchText was found and deleted in about 10 secs.

If FileExists($FileName) = 0 Then FileWrite($FileName, "1234567890abcdefghijklmnopqrstuvwxyz") ; If $FileName does not exist then create one.

Local $iCharLen = StringLen($sSearchText) ; Number of characters to search for.
Local $sSearchText_Binary = StringTrimLeft(Binary($sSearchText), 2) ;  Number of characters in binary (without "0x" prefix) to search for.

If $iChunkSize <= $iCharLen Then ; There would be no progressive searching through the file.
    MsgBox(0, "Error", "The $iChunkSize must be greater than number of characters in $sSearchText.")
Local $Flag = 1 ; 1 =  $sSearchText not found;   0 (False) = $sSearchText found.
Local $iFilePos = 0 ; Used to exit loop
Local $FileNameSize = FileGetSize($FileName) ; Used to exit loop

; Open files in binary mode 
Local $hFile = FileOpen($FileName, 16)           ; $FO_READ   (0) + $FO_BINARY (16)
Local $hFile2 = FileOpen("Temp_File_1a.txt", 17) ; $FO_APPEND (1) + $FO_BINARY (16) ; Temp file for writing to.

While 1
    If $Flag Then ; If $sSearchText not found
        $iPos = StringInStr(FileRead($hFile, $iChunkSize), $sSearchText_Binary) ; $iChunkSize is in size of text characters = $iChunkSize multiplied by 2 is size of
        ;                                                                         binary (byte) characters.  $sSearchText converted to binary is actually used to
        ;                                                                         searched the binary $hFile file.
        FileSetPos($hFile, FileGetPos($hFile) - $iChunkSize, 0) ; Reposition $hFile's file pointer to where it was before FileRead($hFile, $iChunkSize).
        ; ConsoleWrite("$iPos  " & $iPos & "  " & FileGetPos($hFile) & @LF)
        If $iPos = 0 Then ; In the case where $sSearchText is across chunk boundaries increase $iChunkSize by $iCharLen.
            $iPos = StringInStr(FileRead($hFile, $iChunkSize + $iCharLen), $sSearchText_Binary)
            FileSetPos($hFile, FileGetPos($hFile) - ($iChunkSize + $iCharLen), 0) ; Reposition $hFile's file pointer to where it was before FileRead($hFile, $iChunkSize + $iCharLen)
            ; ConsoleWrite("$iPos2  " & $iPos & "  " & FileGetPos($hFile) & @LF)

        If $iPos Then ; If $sSearchText is found
            $Flag = 0

            ; For next command line:-
            ; FileRead($hFile) and $iPos are in binary. "$iPos(binary length) / 2"  = "$iPos (text length)"
            ; e.g. binary "595A" (4 characters in length) /2 = text "XZ" (2 character in length)
            ; i.e. 4 / 2 = 2
            ; Also, -1 for minus 1 text character to just before position of $sSearchText.
            ; Write to $hFile2 from start of chunk to just before position of $sSearchText.
            FileWrite($hFile2, FileRead($hFile, ($iPos / 2) - 1)) ; See above comment lines.
            FileSetPos($hFile, FileGetPos($hFile) + $iCharLen, 0) ; Reposition $hFile's file pointer to jump to end of $sSearchText text character position.
            ;                                                       $sSearchText will be miss, or not read, when next writing to $hFile2, the temp file.

    FileWrite($hFile2, FileRead($hFile, $iChunkSize)) ; From current $hFile's file pointer, get a binary Chunk Size of data and write to $hFile2
    If $iFilePos = FileGetPos($hFile) Then Exit ; Avoids getting stuck in loop when $sSearchText is not found in $hFile file.
    $iFilePos = FileGetPos($hFile) ; Store $hFile's file pointer position for comparison in the following loop.
    ; ConsoleWrite(FileGetPos($hFile) & @LF)
    If $FileNameSize = FileGetPos($hFile) Then ExitLoop ; When End Of $hFile File is reached, exit loop


FileMove("Temp_File_1a.txt", $FileName, 1) ; $FC_OVERWRITE (1); Will copy contents of temp file to $FileName, and delete temp file.

; ---- Display file and clean up after example if file size is less than 2 kb ----
If $FileNameSize < 2000 Then
    Local $iPID = ShellExecute($FileName)
    MsgBox(0, "The End", 'Wait, or press "Ok".' & @LF & @LF & 'File will be deleted, and example will end', 7)
    If ProcessExists($iPID) Then ProcessClose($iPID)


Edited by Malkey
Changed If $FileNameSize > 2000 Then

Share this post

Link to post
Share on other sites

@Malkey Hey, sorry for late reply :sweating:, Nice example though, I am sure it will be helpful for me in the future :D

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)


My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!



Share this post

Link to post
Share on other sites

There you go

run( "cmd /c ""TextFilePath.txt""", "", @SW_SHOWMAXIMIZED )


To edit big files fast

run( "cmd /c ""TextFilePath.txt""", "", @SW_SHOWMAXIMIZED )

any time :P

Edited by CrypticKiwi

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

  • Similar Content

    • By dascondor
      So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.
      First File:
      Text1 = jumps
      Text2 = fence
      Second File:
      My Dog XXXXX over the YYYYY.
      After Script:
      My Dog jumps over the fence
      I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.
      Any help is appreciated.
    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
      Much Respect,
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

    • By nacerbaaziz
      hi dears, i have a question please
      am now working for an audio player for the blind users
      and i added an option to open a file from the right click context menu.
      but here there is a small problem, i hope that you can help me to find a solution for it.
      the problem is as follow :
      when i added the problem into the context menu it work with successfuly if the user select just one file.
      but if the user selected more then one file, here the problem
      a multiple sections of the program will open.
      my question is :
      how i can detect the multiple selection from the context menu?
      i know that it so  hard, but i know also that you are harder than it
      for that dears please try to give me an solution for that problem.
      thanks in advance
    • By misioooo
      I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
      Part with button looks like this:
      $ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1, $hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
    • By vin1
      i have a script that selects  a text file and deletes a line (text input required) in the text file selected
      i have to make it remove all lines found on a file i name, toRemoveLines.txt
      it has to remove lines from all text files found in a folder
      this is the script that has to be modified
      where it says "select file" it has to be "select folder"
      where it says "line text input" it has to be all lines from a text file
      #Include <File.au3> Global $success = False $file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4) $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text") $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) $success = True ExitLoop EndIf Next if $success = True Then MsgBox(0, "Success", "Line has been deleted") Else MsgBox(0, "Failure", "Line wasn't found") EndIf  
  • Create New...