Sign in to follow this  
Followers 0
vickerps

I Want To Insert Text Into A File

9 posts in this topic

I want to insert text to a line within a file. Filewrite only let you add to the end. Does anyone know of a way

Share this post


Link to post
Share on other sites



i think there are info about that in the helpfile...

:iamstupid:

Share this post


Link to post
Share on other sites

Follow this link ! :whistle:

Share this post


Link to post
Share on other sites

Thank guys but that add to the begining of a file i want to add some where in the middle

for example

line23

Share this post


Link to post
Share on other sites

Larry as i read what you say fear consumes my whole body. I'll give a go but if you have the time can you give me a helping hand.

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanks it would of taken a long long time for me to do that.

Why isn't there a option like filewriteline that will do this. Can it be put into the next release

Yes i know i'am cheeky lol.

Share this post


Link to post
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.

Share this post


Link to post
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...

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