Braese Posted April 12, 2012 Share Posted April 12, 2012 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 Link to comment Share on other sites More sharing options...
MrMitchell Posted April 12, 2012 Share Posted April 12, 2012 You should be able to use the INI* functions...INIWrite(), INIRead(), etc... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Braese Posted April 12, 2012 Author Share Posted April 12, 2012 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! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
iamtheky Posted April 12, 2012 Share Posted April 12, 2012 (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 April 12, 2012 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
iamtheky Posted April 12, 2012 Share Posted April 12, 2012 I did my time in, pulled plenty of fireguard; though my often sleeping self was probably as useful as the chocolate variety. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 12, 2012 Moderators Share Posted April 12, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
iamtheky Posted April 12, 2012 Share Posted April 12, 2012 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! ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kylomas Posted April 12, 2012 Share Posted April 12, 2012 @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 Link to comment Share on other sites More sharing options...
Braese Posted April 13, 2012 Author Share Posted April 13, 2012 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? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 13, 2012 Moderators Share Posted April 13, 2012 Braese, Moving the goalposts in mid-thread, eh? We just need a different strategy: expandcollapse popup#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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
iamtheky Posted April 13, 2012 Share Posted April 13, 2012 Melbas surely scales nicer but the long way around looks like so: expandcollapse popup#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) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Braese Posted April 13, 2012 Author Share Posted April 13, 2012 Works like a charm, many thanks u2 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 13, 2012 Moderators Share Posted April 13, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now