Sign in to follow this  
Followers 0
flxfxp

Write a specific line (as in position) in a text file

10 posts in this topic

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

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by Authenticity

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by flxfxp

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Edit: Moi mistake, escape the backslash. Use stringreplace on the resulting string to double the backslashes. Edited by Authenticity

Share this post


Link to post
Share on other sites

_FileWriteToLine()


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

#10 ·  Posted

_FileWriteToLine()

Thanks for providing the easiest solution ^_^

Also thanks Authenticity and WideBoyDixon for their examples.

Regards,

Dennis

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