Jump to content

I Want To Insert Text Into A File


Recommended Posts

  • Developers

Here's a UDF that is based on Larry's post which add's a record at the beginning.

This one will add the text at the given record and position within the record.

FileInsertText("FileName.txt", 5,0,"I am new text" & @CRLF)


; function will insert "$SZTEXT" in file "$SZFILE" at Record "$SZREC" at record position "$SZPOS"
Func FileInsertText($SZFILE,$SZREC,$SZPOS,$SZTEXT)
   If Not FileExists($SZFILE) Then Return
   $SZBUFFER = FileRead($SZFILE,FileGetSize($SZFILE))
   $RECPOS=0          ; holds crlf position 
   $RECCNT=0          ; record counter
   $SZBUFFER_New = "" ; new file buffer
   While 1
      $RECPOS = StringInStr($SZBUFFER,@CRLF)
      If $RECPOS > 0 Then
         $RECCNT=$RECCNT+1
         If $RECCNT = $SZREC Then
            If $SZPOS > 0 And $SZPOS < $RECPOS Then                             ; if specified record position is within the record
               $SZBUFFER_New = $SZBUFFER_New & StringLeft($SZBUFFER,$szPOS-1)   ; save bit to $RECPOS in newoutput string
               $SZBUFFER_New = $SZBUFFER_New & $SZTEXT                          ; insert specified text
               $SZBUFFER_New = $SZBUFFER_New & StringTrimLeft($SZBUFFER,$szPOS-1); add remainder of the file
            Else
               $SZBUFFER_New = $SZBUFFER_New & $SZTEXT                          ; insert specified text
               $SZBUFFER_New = $SZBUFFER_New & $SZBUFFER                        ; add remainder of the file
            EndIf
            ExitLoop
         Else
            $SZBUFFER_New = $SZBUFFER_New & StringLeft($SZBUFFER,$RECPOS+1) ; save bit to crlf in newoutput string
            $SZBUFFER = StringTrimLeft($SZBUFFER,$RECPOS+1)           ; trim rest of record after crlf
         EndIf
      Else
         $SZBUFFER_New = $SZBUFFER_New & $SZBUFFER    ; save last record newoutput string
      EndIf   
   Wend
   FileDelete($szFile)
   Return(FileWrite($SZFILE,$SZBUFFER_New))
EndFunc  ;==>FileInsertText

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

you can do a lot with the current options, here is one version, but it has no checks, so it will only write on the line if that line exists:

filewriteatline("test.txt","I am line2",2)


Func filewriteatline($_file,$_line,$_linenum)
$linecount=0
$file = FileOpen($_file, 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
     $linecount=$linecount+1
If $linecount=$_linenum Then FileWriteLine("tempfilename.txt",$_line)
FileWriteLine("tempfilename.txt",$line)
Wend
FileClose($file)
FileCopy("tempfilename.txt",$_file,1)
FileDelete("tempfilename.txt")
EndFunc

and of course maybe you wanted a fileline replace function:

filelinereplace("test.txt","I am line2",2)


Func filelinereplace($_file,$_line,$_linenum)
$linecount=0
$file = FileOpen($_file, 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
     $linecount=$linecount+1
If $linecount=$_linenum Then 
FileWriteLine("tempfilename.txt",$_line)
else
FileWriteLine("tempfilename.txt",$line)
EndIf
Wend
FileClose($file)
FileCopy("tempfilename.txt",$_file,1)
FileDelete("tempfilename.txt")
EndFunc

This is almost line by line out of the help file, with added variables. Might be a bit easier to read/understand.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

The reason was that this done this way, is that the same sort of thing would have to happen inside AutoIt and that would require a huge amount of overhead to take care of it and the memory requirements would be almost the same as doing it in a UDF. It was considered just way too much work, size, overhead, etc.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...