Jump to content

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


Recommended Posts

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

Link to comment
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

Link to comment
Share on other sites

$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
Link to comment
Share on other sites

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
Link to comment
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

Link to comment
Share on other sites

  • 14 years later...



$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"

Link to comment
Share on other sites

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...