Jump to content

Recommended Posts

Posted

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

Posted

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.

Posted

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

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
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
Posted

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

Posted

_FileWriteToLine()

Thanks for providing the easiest solution ^_^

Also thanks Authenticity and WideBoyDixon for their examples.

Regards,

Dennis

  • 14 years later...
Posted



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

Posted

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

 

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
×
×
  • Create New...