Jump to content
Sign in to follow this  
Spartanicus

Inserting a line in a text file

Recommended Posts

Spartanicus

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

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
Spartanicus

_FileWriteToLine or _FileReadToArray insert _FileWriteFromArray

Beginner here, I've no idea what that means.

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
Spartanicus

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.

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
Spartanicus

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.

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
ResNullius

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.

Share this post


Link to post
Share on other sites
Spartanicus

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.

Share this post


Link to post
Share on other sites
gsglive

u can use

Send("Text")

- gsglive


- gsglive

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  

×