Jump to content

Inserting a line in a text file


Recommended Posts

I need to add a key to an INI file section, but without a "=" or a value. This means that I can't use the "IniWrite" function (which if I null the value still appends the "=").

Can anyone point me to an example script from which I could learn how to insert a line under a "[Header]" in a text file? (not append at the end of the file)

Edited by Spartanicus
Link to comment
Share on other sites

Hi,

_FileWriteToLine or _FileReadToArray insert _FileWriteFromArray

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

Hi,

that are function names. Have a look at the examples in the great helpfile.

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

that are function names. Have a look at the examples in the great helpfile.

Already did that.

I can only find a "FileWriteLine" function (note no preceeding underscore and no "To"), and "FileWriteLine" afaics can only append a line to the end of the file. As I noted in my OP this isn't what I need, I need to insert a line somewhere before the end of the file.

As for "_FileReadToArray" and "_FileWriteFromArray" a search through the help file came up blank, same if I ommit the underscore.

Link to comment
Share on other sites

Hi,

okay I'll help you. Do you want to insert a line as line 2 or after a specific text or line ow what?

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

Do you want to insert a line as line 2 or after a specific text or line ow what?

As per the OP I need to insert a line below a header in an INI file, structure is something like this:

[header1]

foo

bar

text to be added

[header2]

blah=foo

boo=bar

The position of "[header1]" or its content isn't fixed.

Link to comment
Share on other sites

HI,

#include<Array.au3>
#include<File.au3>
_insertLineToFile(@ScriptDir & '\test.ini', '[24324]', 'test')

Func _insertLineToFile($filePath, $after, $insertText)
    Local $lines
    If Not _FileReadToArray($filePath, $lines) Then Return -1
    _ArrayInsert($lines, _ArraySearch($lines, $after, 1, 0, 0) + 1, $insertText)
    If _FileWriteFromArray($filePath, $lines, 1) Then Return 1
    Return -2
EndFunc   ;==>_insertLineToFile

So long,

Mega

Edited 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

As for "_FileReadToArray" and "_FileWriteFromArray" a search through the help file came up blank, same if I ommit the underscore.

These are User "Defined Functions" that are installed in the "Include" folder. For the help file, open "UDFs3.chm" in you main AutoIt3 installation folder.

Link to comment
Share on other sites

These are User "Defined Functions" that are installed in the "Include" folder. For the help file, open "UDFs3.chm" in you main AutoIt3 installation folder.

Ah! Apologies to Mega for not getting that, I hadn't spotted the UDF's and their help file yet, reading up on them now.

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