vickerps Posted March 23, 2004 Share Posted March 23, 2004 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 Link to comment Share on other sites More sharing options...
Dev Posted March 23, 2004 Share Posted March 23, 2004 i think there are info about that in the helpfile... :iamstupid: Link to comment Share on other sites More sharing options...
Helge Posted March 23, 2004 Share Posted March 23, 2004 Follow this link ! Link to comment Share on other sites More sharing options...
vickerps Posted March 23, 2004 Author Share Posted March 23, 2004 Thank guys but that add to the begining of a file i want to add some where in the middle for example line23 Link to comment Share on other sites More sharing options...
vickerps Posted March 24, 2004 Author Share Posted March 24, 2004 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. Link to comment Share on other sites More sharing options...
Developers Jos Posted March 24, 2004 Developers Share Posted March 24, 2004 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 More sharing options...
vickerps Posted March 24, 2004 Author Share Posted March 24, 2004 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. Link to comment Share on other sites More sharing options...
scriptkitty Posted March 24, 2004 Share Posted March 24, 2004 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 More sharing options...
Nutster Posted March 26, 2004 Share Posted March 26, 2004 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 NuttallNuttall 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 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