GEOSoft Posted January 29, 2004 Share Posted January 29, 2004 Just great !!! I can use FileReadLine to read a particular line number but FileWriteLine will only append to the end of the file. That is NotCool = ($sucks)!!! It would be great if we could just use FileWriteLine ($file,"This text",3) with the 3 meaning line 3. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Administrators Jon Posted January 29, 2004 Administrators Share Posted January 29, 2004 That's because the only way to do it (even in C ) is to read in the input file line by line and at the same time write out a temporary file line by line and then at the required line number quickly sneak in a new line and then continue... It won't be much slower doing that in the script yourself. Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
GEOSoft Posted January 29, 2004 Author Share Posted January 29, 2004 Okay thanks Jon. This one is critical so I guess I stay up all night until I find something that works. Exactly the type of virus situation that it is meant to protect against broke out yesterday and I will have to get around it quickly. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
scriptkitty Posted January 29, 2004 Share Posted January 29, 2004 Ugly code to read file, and then write any line you like: This is written more for educational purpose than application. First you read how many lines, then you dimention an array for that many lines, read each line into the array, and finally you output the file back, to the same file, or to a different one. I kept it simple, but if you really wanted, it would be simple to dynamically dimention the array, and also combine the two functions. expandcollapse popup$lines = readfile("test.csv") MsgBox(1,"line1="&$lines[1],"last is line"&$lines[0]&@crlf&$lines[$lines[0]]) writeline("output.csv","This is my line",10,$lines) Func Writeline($fileX,$line_text,$line_num,$array) $file = FileOpen($fileX, 2) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf For $x=1 To $array[0] If $x=$line_num Then FileWriteLine($fileX, $line_text&@CRLF) Else FileWriteLine($fileX, $array[$x]&@CRLF) EndIf Next FileClose($file) EndFunc Func readfile($fileX) $file = FileOpen($fileX, 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 $x=1 While 1 $myline = FileReadLine($file) If @error = -1 Then ExitLoop EndIf $x=$x+1 Wend FileClose($file) $file = FileOpen($fileX, 0) Dim $line[$x+10] $line[0]=$x-1 $x=1 While 1 $line[$X] = FileReadLine($file) If @error = -1 Then ExitLoop EndIf $x=$x+1 Wend FileClose($file) Return $line EndFunc AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
scriptkitty Posted January 29, 2004 Share Posted January 29, 2004 Ok, here is the function in replace mode. Still ugly, but seems to work ok. expandcollapse popup; usage, ReplaceLine("Filename to read","Filename to output","Line","Line number") replaceline("test.csv","test2.csv","This is my line",1) Func Replaceline($fileX,$fileY,$line_text,$line_num) $file = FileOpen($fileX, 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 $x=1 While 1 $myline = FileReadLine($file) If @error = -1 Then ExitLoop EndIf $x=$x+1 Wend FileClose($file) $file = FileOpen($fileX, 0) Dim $line[$x+10] $line[0]=$x-1 $x=1 While 1 $line[$X] = FileReadLine($file) If @error = -1 Then ExitLoop EndIf $x=$x+1 Wend FileClose($file) If $line_num>$line[0] Then MsgBox(1,"Error","Only " & $line[0] & " lines in file."&@CRLF& "Can not replace line "& $line_num) exit EndIf $file = FileOpen($fileY, 2) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf For $x=1 To $line[0] If $x=$line_num Then FileWriteLine($fileY, $line_text&@CRLF) Else FileWriteLine($fileY, $line[$x]&@CRLF) EndIf Next FileClose($file) EndFunc AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
Developers Jos Posted January 29, 2004 Developers Share Posted January 29, 2004 (edited) if the goals is to just replace/insert a record at a given record number would it be easier to use something like the below code ?? expandcollapse popup;=============================================================================== ; ; Description: Inserts or replaces a record in a file ; Parameter(s): $nFile - File to process/update. ; $nRectxt - Text to insert in the file. ; $nRecnum - Record number where to place the text. ; $nMode - 0 = Insert the record at the given record number. ; 1 = Replace the record at the given record number. ; Return Value(s): On Success - 1 else 0 ; On Failure - @ERROR = 1 (input file failed to open) ; @ERROR = 2 (Output file failed to open) ; @ERROR = 3 (requested record number doesn't exist) ;=============================================================================== Func _FileWriteLine ($nfile,$nRectxt,$recnum,$nMode) $x = 0 $fnri = FileOpen($nfile, 0) ; Check if file is opened If $fnri = -1 Then seterror(1) return (0) EndIf $fnro = FileOpen(@tempdir & "_FileWriteLine.tmp" , 2) If $fnro = -1 Then seterror(2) return ( 0 ) EndIf While 1 $nIrec = FileReadLine($fnri) If @error = -1 Then ExitLoop $x=$x+1 ; insert the text when requested record is reached if $x = $recnum then FileWriteLine($fnro, $Nrectxt & @LF) ; Also insert original record if nMode is insert if $nmode = 0 then FileWriteLine($fnro, $nIrec & @LF) else ; Write input record FileWriteLine($fnro, $nIrec & @LF) endif Wend ; if file has less records than requested if $x < $recnum then seterror(3) return 0 endif ; replace the origial file with the temp file Fileclose($fnri) Fileclose($fnro) filecopy(@tempdir & "_FileWriteLine.tmp",$nfile,1) fileDelete(@tempdir & "_FileWriteLine.tmp") return 1 EndFunc Edited January 29, 2004 by JdeB 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...
GEOSoft Posted January 30, 2004 Author Share Posted January 30, 2004 (edited) The problem here is that I don't write the file that had to be changed. Origionally it was determined that the line to be inserted should go into line 3. After discussion with the author he has agreed to letting me place the line in as line 1 so that made it simple. Here is what I used (it works) $txt = "#New text goes here"&@LF;The # comments the line FileOpen ($File2,1) FileWriteLine ($File2,$txt) FileClose ($File2) RunWait (@comspec & ' /c copy /b'&' '&$File2&' + '&$File1&' '&$File3,'',@SW_HIDE) FileCopy ($File3,$File1,1) FileDelete ($File3) FileDelete ($File2) ExitNow I think there is redundency in there that I don't need. What would happen if I changed the runWait line toRunWait (@comspec & ' /c copy /b'&' '&$File2&' + '&$File1&' '&$File1,'',@SW_HIDE) and then removed the FileCopy line and the FileDelete ($File3) because $File3 should not exist if the RunWait line works as I expect it to. Then the whole code should be $txt = "#New text goes here"&@LF;The # comments the line FileOpen ($File2,1) FileWriteLine ($File2,$txt) FileClose ($File2) RunWait (@comspec & ' /c copy /b'&' '&$File2&' + '&$File1&' '&$File1,'',@SW_HIDE) FileDelete ($File2) Exit Does that make sense? or should I just stay with the original?NOTE: The $txt is actually a random 7 digit integer that also gets written to the registry so it can be checked to verify the file.Edit: Never mind I just proved it doesn't I end up with just the new line in the file. So I guess I use the original. Edited January 30, 2004 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" 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