jacksont123 Posted May 27, 2006 Share Posted May 27, 2006 (edited) say I have a text file [or any other type of file] with the contents of AutoIt is not cooland i'd like to change the exact 3rd line so the contents would be AutoIt is so cool I looked up FileWriteLine but it doesn't allow me to edit a specific line in the text file. How would I change a specific line of a file in Autoit? Edited May 27, 2006 by The Great 'Awesoma-Powa!' Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 27, 2006 Share Posted May 27, 2006 HI, try this udf. expandcollapse popup;======================================== ;Function name: _FileWriteToLine ;Description: Write text to specified line in a file ;Parameters: ; $sFile - The file to write to ; $iLine - The line number to write to ; $sText - The text to write ; $fOverWrite - if set to 1 will overwrite the old line ; if set to 0 will not overwrite ;Requirement(s): None ;Return Value(s): On success - 1 ; On Failure - 0 And sets @ERROR ; @ERROR = 1 - File has less lines than $iLine ; @ERROR = 2 - File does not exist ; @ERROR = 3 - Error opening file ; @ERROR = 4 - $iLine is invalid ; @ERROR = 5 - $fOverWrite is invalid ; @ERROR = 6 - $sText is invalid ;Author(s): cdkid ;Note(s): ;========================================= Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0) If $iLine <= 0 Then SetError(4) Return 0 EndIf If Not IsString($sText) Then SetError(6) Return 0 EndIf If $fOverWrite <> 0 And $fOverWrite <> 1 Then SetError(5) Return 0 EndIf If Not FileExists($sFile) Then SetError(2) Return 0 EndIf $filtxt = FileRead($sFile, FileGetSize($sFile)) $filtxt = StringSplit($filtxt, @CRLF, 1) If UBound($filtxt, 1) < $iLine Then SetError(1) Return 0 EndIf $fil = FileOpen($sFile, 2) If $fil = -1 Then SetError(3) Return 0 EndIf For $i = 1 To UBound($filtxt) - 1 If $i = $iLine Then if $fOverWrite = 1 then if $sText <> '' Then FileWrite($fil, $sText & @CRLF) Else filewrite($fil, $sText) EndIf EndIf If $fOverWrite = 0 Then FileWrite($fil, $filtxt[$i] & @CRLF) EndIf ElseIf $i < UBound($filtxt, 1) - 1 Then FileWrite($fil, $filtxt[$i] & @CRLF) ElseIf $i = UBound($filtxt, 1) - 1 Then FileWrite($fil, $filtxt[$i]) EndIf Next FileClose($fil) Return 1 EndFunc;==>_FileWriteToLine So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
jacksont123 Posted May 27, 2006 Author Share Posted May 27, 2006 (edited) i saved that udf as 'fwritetl.au3' into my autoit include folder i ran the following script: #include <fwritetl.au3> _FileWriteToLine('test.txt', '2', 'autoit', '1') and got this error in a message box: Error reading the file: fwritetl.au3 edit: nevermind i guess the problem's fixed. i was running this through SciTE, but when I launched this through Autoit it worked edit#2: apparently, I can run this through SciTE, but not on beta run mode Edited May 27, 2006 by The Great 'Awesoma-Powa!' Link to comment Share on other sites More sharing options...
Zedna Posted May 27, 2006 Share Posted May 27, 2006 i saved that udf as 'fwritetl.au3' into my autoit include folder i ran the following script: #include <fwritetl.au3> _FileWriteToLine('test.txt', '2', 'autoit', '1') and got this error in a message box: edit: nevermind i guess the problem's fixed. i was running this through SciTE, but when I launched this through Autoit it worked edit#2: apparently, I can run this through SciTE, but not on beta run mode Try to use: #include "fwritetl.au3" Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
jacksont123 Posted May 27, 2006 Author Share Posted May 27, 2006 still doesn't work Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 27, 2006 Share Posted May 27, 2006 still doesn't work HI,#include<File.au3> is enough. It is already in the beta. So long,Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
jacksont123 Posted May 27, 2006 Author Share Posted May 27, 2006 I renamed 'fwritetl.au3' to 'filewrite.au3' instead of a message box with that same text, a different widnow opened up with the title Au3Check Errors, and a textbox with the following text: C:\Scripts\fileedit.au3(21,10) : ERROR: can't open include file "filewrite.au3" #include "filewrite.au3" Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 27, 2006 Share Posted May 27, 2006 I renamed 'fwritetl.au3' to 'filewrite.au3' instead of a message box with that same text, a different widnow opened up with the title Au3Check Errors, and a textbox with the following text: C:\Scripts\fileedit.au3(21,10) : ERROR: can't open include file "filewrite.au3" #include "filewrite.au3" Hi, why do you want to include it twice? As I mentioned above the func is already in file.au3. So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
jacksont123 Posted May 27, 2006 Author Share Posted May 27, 2006 HI, #include<File.au3> is enough. It is already in the beta. So long, Mega but <File.au3> doesn't have the same functions as the udf you gave me. file.au3 expandcollapse popup#include-once ; ------------------------------------------------------------------------------ ; ; AutoIt Version: 3.0 ; Language: English ; Description: Functions that assist with files and directories. ; ; ------------------------------------------------------------------------------ ;=============================================================================== ; ; Description: Returns the number of lines in the specified file. ; Syntax: _FileCountLines( $sFilePath ) ; Parameter(s): $sFilePath - Path and filename of the file to be read ; Requirement(s): None ; Return Value(s): On Success - Returns number of lines in the file ; On Failure - Returns 0 and sets @error = 1 ; Author(s): Tylo <tylo@start.no> ; Note(s): It does not count a final @LF as a line. ; ;=============================================================================== Func _FileCountLines($sFilePath) Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1 EndFunc ;==>_FileCountLines ;=============================================================================== ; ; Description: Creates or zero's out the length of the file specified. ; Syntax: _FileCreate( $sFilePath ) ; Parameter(s): $sFilePath - Path and filename of the file to be created ; Requirement(s): None ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 and sets: ; @error = 1: Error opening specified file ; @error = 2: File could not be written to ; Author(s): Brian Keene <brian_keene@yahoo.com> ; Note(s): None ; ;=============================================================================== Func _FileCreate($sFilePath) ;============================================== ; Local Constant/Variable Declaration Section ;============================================== Local $hOpenFile Local $hWriteFile $hOpenFile = FileOpen($sFilePath, 2) If $hOpenFile = -1 Then SetError(1) Return 0 EndIf $hWriteFile = FileWrite($hOpenFile, "") If $hWriteFile = -1 Then SetError(2) Return 0 EndIf FileClose($hOpenFile) Return 1 EndFunc ;==>_FileCreate ;=============================================================================== ; ; Description: Reads the specified file into an array. ; Syntax: _FileReadToArray( $sFilePath, $aArray ) ; Parameter(s): $sFilePath - Path and filename of the file to be read ; $aArray - The array to store the contents of the file ; Requirement(s): None ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 and sets @error = 1 ; Author(s): Jonathan Bennett <jon at hiddensoft com> ; Note(s): None ; ;=============================================================================== Func _FileReadToArray($sFilePath, ByRef $aArray) ;============================================== ; Local Constant/Variable Declaration Section ;============================================== Local $hFile $hFile = FileOpen($sFilePath, 0) If $hFile = -1 Then SetError(1) Return 0 EndIf $aArray = StringSplit( FileRead($hFile, FileGetSize($sFilePath)), @LF) FileClose($hFile) Return 1 EndFunc ;==>_FileReadToArray ;=============================================================================== ; ; Description: Writes the specified text to a log file. ; Syntax: _FileWriteLog( $sLogPath, $sLogMsg ) ; Parameter(s): $sLogPath - Path and filename to the log file ; $sLogMsg - Message to be written to the log file ; Requirement(s): None ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 and sets: ; @error = 1: Error opening specified file ; @error = 2: File could not be written to ; Author(s): Jeremy Landes <jlandes@landeserve.com> ; Note(s): If the text to be appended does NOT end in @CR or @LF then ; a DOS linefeed (@CRLF) will be automatically added. ; ;=============================================================================== Func _FileWriteLog($sLogPath, $sLogMsg) ;============================================== ; Local Constant/Variable Declaration Section ;============================================== Local $sDateNow Local $sTimeNow Local $sMsg Local $hOpenFile Local $hWriteFile $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg $hOpenFile = FileOpen($sLogPath, 1) If $hOpenFile = -1 Then SetError(1) Return 0 EndIf $hWriteFile = FileWriteLine($hOpenFile, $sMsg) If $hWriteFile = -1 Then SetError(2) Return 0 EndIf FileClose($hOpenFile) Return 1 EndFunc ;==>_FileWriteLog ;=============================================================================== ; ; Function Name: _TempFile() ; Description: Generate a name for a temporary file. The file is guaranteed ; not to already exist in the user's %TEMP% directory. ; Parameter(s): None. ; Requirement(s): None. ; Return Value(s): Filename of a temporary file which does not exist. ; Author(s): Dale (Klaatu) Thompson ; Notes: None. ; ;=============================================================================== Func _TempFile() Local $s_TempName Do $s_TempName = "~" While StringLen($s_TempName) < 8 $s_TempName = $s_TempName & Chr(Round(Random(97, 122), 0)) Wend $s_TempName = @TempDir & "\" & $s_TempName & ".tmp" Until Not FileExists($s_TempName) Return ($s_TempName) EndFunc Link to comment Share on other sites More sharing options...
jacksont123 Posted May 27, 2006 Author Share Posted May 27, 2006 (edited) whoops I think I had the old version of File.au3 it works fine now. Thanks for your help Mega! Edited May 27, 2006 by The Great 'Awesoma-Powa!' Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 27, 2006 Share Posted May 27, 2006 (edited) Hi, are you using beta? If not, then try the beta and have a look in the helpfile for _FileWriteToLine . I think that's it. So long, Mega Edit: OKay! No problem! Edited May 27, 2006 by th.meger Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times 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