flxfxp Posted May 6, 2009 Posted May 6, 2009 lets say i have a text file that contains the following: # SciTE settings for AutoIt v3 # # January 19, 2008 - Jos van der Zande (jdeb (at) autoitscript (dot) com) # # # *** Specify here your AutoIt program directory *** autoit3dir=..\AutoIt openpath.$(au3)=$(autoit3dir)\include openpath.beta.$(au3)=$(autoit3dir)\beta\include au3=*.au3 filter.au3=AutoIt (au3)|$(au3)| lexer.$(au3)=au3 It also contains a whole lot more. Lets say i want to replace line 7 (autoit3dir=..\AutoIt) with a new text. How do I do that. FileReadLine is available, but how to do a "FileWriteLine" on a specific line in a text file? Thanks, Dennis
Authenticity Posted May 6, 2009 Posted May 6, 2009 Annoying thing to do but that is what you can do. Read the file as a whole, use StringReplace or one of the String* functions, open the file in write mode with erase previous content and write all back.
flxfxp Posted May 6, 2009 Author Posted May 6, 2009 Good call! I was already working on that but didnt got to figure it out.I wanted todo it with StringRegExpReplace but I'm horrible with regular expressions$TextFileName = @MyDocumentsDir & "\My Dropbox\Apps\AutoIt3\SciTe\properties\au3.properties" $FindText = "autoit3dir=*" $ReplaceText = "cat" $FileContents = FileRead($TextFileName) $FileContents = StringRegExpReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents)Basically I want to replace the line autoit3dir=..\WHATEVERDIRITMIGHTBE\Autoit to "autoit3dir=" & @MyDocumentsDir & "\My Dropbox\Apps\AutoIt3"I want to do this to preserve the portability of SciTE. You see, for some reason if you use autoit3dir=..\AutoIt3 it looks in Program Files for some reason and starts to use the local installment of Autoit instead of the one on my dropbox. If I change this line to the full Dropbox path, it does use the correct installment.Regards,Dennis
Authenticity Posted May 6, 2009 Posted May 6, 2009 (edited) $TextFileName = @MyDocumentsDir & "\My Dropbox\Apps\AutoIt3\SciTe\properties\au3.properties" $FindText = "(?i)autoit3dir=[^\r\n]+" $ReplaceText = "autoit3dir=cat" $FileContents = FileRead($TextFileName) $FileContents = StringRegExpReplace($FileContents,$FindText,$ReplaceText, 1) ;FileDelete($TextFileName) =( $hFile = FileOpen($TextFileName, 2) FileWrite($hFile, $FileContents) FileClose($hFile)Edit: You can do also: "(?i)autoit3dir=.+" but I guess [^\r\n] is self explanatory. Edited May 6, 2009 by Authenticity
Aceguy Posted May 6, 2009 Posted May 6, 2009 what about what you first suggested.? FileWriteLine ( filehandle or "filename", "line" ) [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock
flxfxp Posted May 6, 2009 Author Posted May 6, 2009 (edited) Awesome Authenticity! It almost works perfectly! However, when i change the replace text to: $ReplaceText = "autoit3dir=" & @MyDocumentsDir & "\My Dropbox\Apps\AutoIt3" It write the line as: autoit3dir=C:Documents and SettingsFLXMy DocumentsMy DropboxAppsAutoIt3 It doesn't love my backslashes anymore How do I fix this behaviour? Thanks, Dennis Aceguy: As the function description says: Append a line of text to the end of a previously opened text file. Appending, sadly not giving you the option of specifying a line. Edited May 6, 2009 by flxfxp
WideBoyDixon Posted May 6, 2009 Posted May 6, 2009 Provided the file in question is sufficiently small you can use: #include <File.au3> #include <Array.au3> Global $aFile $TextFileName = @MyDocumentsDir & "\My Dropbox\Apps\AutoIt3\SciTe\properties\au3.properties" _FileReadToArray($TextFileName, $aFile) $LineNumber = _ArraySearch($aFile, "autoit3dir=", 0, 0, 0, 1) $aFile[$LineNumber] = "autoit3dir=cat" _FileWriteFromArray($TextFileName, $aFile, 1) WBD [center]Wide by name, Wide by nature and Wide by girth[u]Scripts[/u]{Hot Folders} {Screen Calipers} {Screen Crosshairs} {Cross-Process Subclassing} {GDI+ Clock} {ASCII Art Signatures}{Another GDI+ Clock} {Desktop Goldfish} {Game of Life} {3D Pie Chart} {Stock Tracker}[u]UDFs[/u]{_FileReplaceText} {_ArrayCompare} {_ToBase}~ My Scripts On Google Code ~[/center]
Authenticity Posted May 6, 2009 Posted May 6, 2009 (edited) Edit: Moi mistake, escape the backslash. Use stringreplace on the resulting string to double the backslashes. Edited May 6, 2009 by Authenticity
Skruge Posted May 6, 2009 Posted May 6, 2009 _FileWriteToLine() [font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]
flxfxp Posted May 7, 2009 Author Posted May 7, 2009 _FileWriteToLine() Thanks for providing the easiest solution Also thanks Authenticity and WideBoyDixon for their examples.Regards,Dennis
ashraful089 Posted February 13, 2024 Posted February 13, 2024 $Sleep_1 = FileReadLine($Values_To_read, 10) ; where $Values_To_read= @ScriptDir & "\Values_To_read.txt" reading the texts in line 10 off the text file. is there have any simple command to write a spcific line ?? Such as, $Sleep_1 = FileWriteLine($Values_To_Write, 10) ; where $Values_To_Write= @ScriptDir & "\Values_To_Write.txt"
Nine Posted February 13, 2024 Posted February 13, 2024 14 years later ? You should have started a new thread, but here one way : #include <FileConstants.au3> FileWriteLineEx("Test - Copie.txt", 3, "New line here") Func FileWriteLineEx($sFile, $iLine, $sNewLine) Local $hFileOpen = FileOpen($sFile) Local $sLine = FileReadLine($hFileOpen, $iLine) Local $iPos = FileGetPos($hFileOpen) Local $sEnd = FileRead($hFileOpen) FileSetPos($hFileOpen, 0, $FILE_BEGIN) $sBegin = FileRead($hFileOpen, $iPos - StringLen($sLine) - 2) FileClose($hFileOpen) $hFileOpen = FileOpen($sFile, $FO_OVERWRITE) FileWrite($hFileOpen, $sBegin & $sNewLine & @CRLF & $sEnd) FileClose($hFileOpen) EndFunc ;==>Example “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
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