Sign in to follow this  
Followers 0
Braese

txt file same sections how can i change only one of it

16 posts in this topic

Hi there,

have a problem. A text file with:

[server]

port = 10000

dyndns = xyz.abc.com

username = root

[server]

port = 20000

dyndns = abc.xyz.com

username = root

and so on. It can be 1 [server] section or 10 or anything else. How can i do that, that i if i only change entries of one section, in example: if dyndns is xyz.abc.com so change username and change port but not modifiy the other entries and write all to a new file.

Thanks

Share this post


Link to post
Share on other sites



You should be able to use the INI* functions...INIWrite(), INIRead(), etc...

Share this post


Link to post
Share on other sites

Braese,

The ini functions will not work as each header is isdentical. Is every section the same? If so then we shoudl be able to look for the specific dyndns value and change the port and username - let me play with it for a while. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yes, always the same with name [server] but the key = values within are not identical! IniRead and so on doesnt work because of the same Names and because of key = vaue isnt the right format or?

Thanks Melba! ;)

Share this post


Link to post
Share on other sites

Braese,

Something along these lines should do the trick: :)

#include <File.au3>
#include <Array.au3>

$sDynDNS = "xyz.abc.com"
$sNewUser = "fred"
$sNewPort = "999999"

; Read in file
Global $aLines
_FileReadToArray("test.txt", $aLines)

; Look for specifis dyndns
$iIndex = _ArraySearch($aLines, $sDynDNS, 0, 0, 0, 1)

; Now we look at the line above
$aSplit = StringSplit($aLines[$iIndex - 1], " = ", 1)
$aLines[$iIndex - 1] = $aSplit[1] & " = " & $sNewPort

; And now the line below
$aSplit = StringSplit($aLines[$iIndex + 1], " = ", 1)
$aLines[$iIndex + 1] = $aSplit[1] & " = " & $sNewUser

; Now rewrite the file
_FileWriteFromArray("new_test.txt", $aLines, 1)

; And this is what we get
MsgBox(0, "New file", FileRead("new_test.txt"))

Please ask if you have any questions. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

My screen didnt refresh with Melbas reply, but I suppose I'll leave this behind anyway...

#Include <File.au3>

$Locator = "dyndns = abc.xyz.com"
$SETPort = "NEW PORT NUMBER"
$SETusername = "NEW USERNAME"

$Max = _FileCountLines ("test.txt")
$i = 1

for $i = $i to $Max


$port = filereadline ("test.txt" , $i + 1)

$dyndns = filereadline ("test.txt" , $i + 2)

$username = filereadline ("test.txt" , $i + 3)

If $dyndns = $Locator Then
    filewriteline ("testwrite.txt" , "[server]")
    filewriteline ("testwrite.txt" , "port = " & $SETPort)
    filewriteline ("testwrite.txt" , $dyndns)
    filewriteline ("testwrite.txt" , "username = " & $SETusername)
    filewriteline ("testwrite.txt" , @CRLF)
Else
    filewriteline ("testwrite.txt" , "[server]")
    filewriteline ("testwrite.txt" , $Port)
    filewriteline ("testwrite.txt" , $dyndns)
    filewriteline ("testwrite.txt" , $username)
    filewriteline ("testwrite.txt" , @CRLF)
EndIf

$i += 4

next
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

boththose,

Yes, the "New reply" function is about a much use as a chocolate fireguard - not that you youngsters know what a "fireguard" is anyway! ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I did my time in, pulled plenty of fireguard; though my often sleeping self was probably as useful as the chocolate variety.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

boththose,

Not sure we are talking about the same thing - this is what I meant! ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Now thats some proper OT. Cant believe the military jacked my understanding of a colloquialism, that and I dont ever hear those items called fireguards so always assumed that the instructors were using it correctly when degrading folk.

A nice array solution and a verbiage correction, useful as always! ;)


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

@boththose,

Having stood many a watch I thought the same thing !

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thank you!

A small problem.

[server]

port = 10000

dyndns = xyz.abc.com

username = root

but it can be happen that it looks like:

[server]

port = 10000

dyndns = xyz.abc.com

username = root

password = root

or

[server]

port = 10000

dyndns = xyz.abc.com

password = root

username = root

and so on, it can have more variables and so the array trick with "go back 1 line" and so on dont work or?

Share this post


Link to post
Share on other sites

Braese,

Moving the goalposts in mid-thread, eh? ;)

We just need a different strategy: :D

#include <File.au3>
#include <Array.au3>

$sDynDNS = "xyz.def.com"
$sNewUser = "fred"
$sNewPort = "999999"

; Read in file
Global $aLines
_FileReadToArray("test.txt", $aLines)

; Look for specifis dyndns
$iIndex = _ArraySearch($aLines, $sDynDNS, 0, 0, 0, 1)

; Now we go up to the [server] line above it
For $i = $iIndex - 1 To 0 Step -1
    If StringInStr($aLines[$i], "[server]") Then
        ; And we start looking from the line below
        $iIndex = $i + 1
        ExitLoop
    EndIf
Next

; Run through the lines and change when required
For $i = $iIndex To $iIndex + 100
    Select
        Case StringInStr($aLines[$i], "port")
            $aSplit = StringSplit($aLines[$i], " = ", 1)
            $aLines[$i] = $aSplit[1] & " = " & $sNewPort
        Case StringInStr($aLines[$i], "username")
            $aSplit = StringSplit($aLines[$i], " = ", 1)
            $aLines[$i] = $aSplit[1] & " = " & $sNewUser
        ; Check if we are the next [server] section or the end of the file
        Case StringInStr($aLines[$i], "[server]") Or $i > $aLines[0]
            ExitLoop
    EndSelect
Next

; Now rewrite the file
_FileWriteFromArray("new_test.txt", $aLines, 1)

; And this is what we get
MsgBox(0, "New file", FileRead("new_test.txt"))

to work on this file:

[server]
port = 10000
dyndns = xyz.abc.com
username = root

[server]
port = 10000
dyndns = xyz.def.com
username = root
password = root

[server]
port = 10000
dyndns = xyz.ghi.com
password = root
username = root

Happy now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melbas surely scales nicer but the long way around looks like so:

#Include <File.au3>
#Include <Array.au3>


$Locator = "dyndns = abc.xyz.com"
$SETPort = "NEW PORT NUMBER"
$SETusername = "NEW USERNAME"
$SetPassword = "NEW PASSWORD"
$Max = _FileCountLines ("test.txt")
$i = 1

for $i = $i to $Max

$Port = ""
$dyndns = ""
$username = ""
$password = ""
$1 = ""
$2 = ""
$3 = ""
$4 = ""
$5 = ""
$6 = ""


$1 = filereadline ("test.txt" , $i + 1)
$2 = filereadline ("test.txt" , $i + 2)
$3 = filereadline ("test.txt" , $i + 3)
$4 = filereadline ("test.txt" , $i + 4)
If $4 <> "" Then $5 = filereadline ("test.txt" , $i + 5)
If $5 <> "" Then $6 = filereadline ("test.txt" , $i + 6)

$string = $1 & "," & $2 & "," & $3 & "," & $4 & "," & $5 & "," & $6

$aArray = stringsplit($string, ",")

For $k = 1 to ubound ($aArray) - 1
    If stringleft($aArray[$k] , 4) = "port" then $Port = $aArray[$k]
    If stringleft($aArray[$k] , 4) = "dynd" then $dyndns = $aArray[$k]
    If stringleft($aArray[$k] , 4) = "user" then $username = $aArray[$k]
    If stringleft($aArray[$k] , 4) = "pass" then $password = $aArray[$k]
Next

If $dyndns = $Locator Then
    filewriteline ("testwrite.txt" , "[server]")
    filewriteline ("testwrite.txt" , "port = " & $SETPort)
    filewriteline ("testwrite.txt" , $dyndns)
    filewriteline ("testwrite.txt" , "username = " & $SETusername)
    If $password <> "" Then
    filewriteline ("testwrite.txt" , "password = " & $SETpassword)
    Endif
    filewriteline ("testwrite.txt" , @CRLF)
Else
    filewriteline ("testwrite.txt" , "[server]")
    filewriteline ("testwrite.txt" , $Port)
    filewriteline ("testwrite.txt" , $dyndns)
    filewriteline ("testwrite.txt" , $username)
    If $password <> "" Then
    filewriteline ("testwrite.txt" , $password)
    Endif
    filewriteline ("testwrite.txt" , @CRLF)
EndIf

if $6 <> "" Then
$i += 7
Elseif $5 <> "" Then
$i += 6
Elseif $4 <> "" Then
$i += 5
Else
$i += 4
Endif


next

working on this file

[server]
port = 10000
dyndns = xyz.abc.com
username = root (user 1)

[server]
port = 20000
dyndns = xyz.abc.com
username = root (user 2)
password = root (pass 2)

[server]
port = 30000
dyndns = abc.xyz.com
password = root (pass 3)
username = root (user 3)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Works like a charm, many thanks u2 ;)

Share this post


Link to post
Share on other sites

Braese,

My pleasure. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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