Sign in to follow this  
Followers 0
Eru

Question: Writing to a Specific Line in a File

14 posts in this topic

Question 1

I'm making a script that is going to use saved variables in a .ini file. It's set up like this:

[ColorList]
List=Color1|Color2|Color3|
Color1=0x000000
Color2=0xFFFFFFF
Color3=oxF0F0F0

I've made an input box and am using what's typed in that to add new colors. I use the FileWrite command and I can create a new color at the bottom, [i.e. Color4=0xF0F0F0], but I can't seem to figure out how to add it to the List= line. Is there a command that I'm missing?

Question 2

I'm using a combo box also for selecting the saved color and loading it. Is it possible to use the combo box as the input instead of having it *and* the input box?

Thanks for any help. This is my first script and it's turning out to be quite the challenge. :whistle:

Share this post


Link to post
Share on other sites



Answer 1

FileWriteLine



Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

Well I would use IniWrite :whistle:


Start here if you are new Valuater's AutoIT 1-2-3Looking for an UDF - Look hereDo you need to do it twice - Autoit

Share this post


Link to post
Share on other sites

Hmm. IniWrite will replace everything that is on the List= line when I run it, and FileWriteLine still only adds to the very end of the file. I suppose I could use a combination of the two of them to add the variable to the end of each, but how can I make it add the new color to the end of the List instead of replacing what's on there? The Idea is to keep a running list that I can choose from with a combo box for quick configuration of the main script.

Share this post


Link to post
Share on other sites

You read the list key with IniRead.

Use StringSplit to get separate colors.

Use the info.

Join the colors together again into a string separated with |.

IniWrite the string back to the list key with IniWrite.

:whistle:

Share this post


Link to post
Share on other sites

*sigh* My knowledge of the autoit options are painfully lacking, but here's what I've gotten so far:

Case $MSG = $Save
    $SaveList = IniRead ("List.ini", "Color", "List", "Not Found")
    $Savelist = StringSplit ($Savelist, "|") ;Delemiter = |
;What do do here?
    $IniWrite "List.ini", "Color", "List", $SaveList & GUICtrlRead ($InputBox)

Does anyone have an example that I can look at? I seem to be a lot better at learning things if I'm reverse engineering them instead of perusing pages of functions to find what I need.

Thanks for all the help guys.

Share this post


Link to post
Share on other sites

CODE

$path = @ScriptDir & "\color.ini"

addColor("0xF1F1F1")

addColor("0xF2F2F2")

Func addColor($color)

$section = IniReadSection($path, "ColorList")

IniWrite($path,"ColorList","List", IniRead($path, "ColorList", "List","") & "Color" & $section[0][0] & "|")

IniWrite($path,"ColorList","Color" & $section[0][0], $color)

EndFunc

Share this post


Link to post
Share on other sites

;~ #include <array.au3>
Local $s_List = IniRead ("List.ini", "ColorList", "List", "Not Found")
Local $as_List = StringSplit ($s_List, "|")
;~ _ArrayDisplay($as_List,"List")
Local $i_subtractamount
If $as_List[$as_List[0]] = "" Then;checks to see if the last result is erraneous because of a trailing |
    $i_subtractamount = 1
    ConsoleWrite('List = ""' & @LF & 'The above means that there was a trailing "|"' & @LF)
Else
    $i_subtractamount = 0
    ConsoleWrite('List <> ""' & @LF & 'The above means that there was no trailing "|"' & @LF)
EndIf
ConsoleWrite('The last value in the list string is="' & $as_List[Int($as_List[0]) - $i_subtractamount] & "'" & @LF);shows what the last Color# was in the list= string.
Local $si_LastColor = StringTrimLeft($as_List[Int($as_List[0]) - $i_subtractamount],5);Trims the word color off the left so that we are just left with the number
ConsoleWrite("Last List Color Number=" & $si_LastColor & @LF)
Local $s_ColorNumToAdd = String(Int($si_LastColor) + 1);add one to that number so that we can the next Color# to the list
ConsoleWrite("Now adding Color"&$s_ColorNumToAdd & " to the list string:" & @LF)
IF $i_subtractamount = 1 Then;If there was a trailing | then this is true
    ConsoleWrite("List=" & $s_List & "Color" & $s_ColorNumToAdd & "|" & @LF)
    IniWrite("List.ini", "ColorList", "List", $s_List & "Color" & $s_ColorNumToAdd & "|" )
Else;if there wasn't a trailing |, then this will be used instead
    ConsoleWrite("List=" & $s_List & "|Color" & $s_ColorNumToAdd & "|" & @LF)
    IniWrite("List.ini", "ColorList", "List", $s_List & "|Color" & $s_ColorNumToAdd & "|" )
EndIf


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

hmmmmm why not just do this...? Its reuseable too :whistle:

$path = @ScriptDir & "\color.ini"
addColor("0xF1F1F1")
addColor("0xF2F2F2")
printColors()
Func addColor($color)
    $section = IniReadSection($path, "ColorList")
    IniWrite($path,"ColorList","List", IniRead($path, "ColorList", "List","") & "Color" & $section[0][0] & "|")
    IniWrite($path,"ColorList","Color" & $section[0][0], $color)
EndFunc
Func printColors()
    $section = IniReadSection($path, "ColorList")
    ConsoleWrite("===ColorList===" & @CRLF)
    For $x = 1 to $section[0][0]
        ConsoleWrite(IniRead($path,"ColorList","Color" & $x,"") & @CRLF)
    Next    
EndFunc

Color.ini

[ColorList]

List=Color1|Color2|Color3|

Color1=0x000000

Color2=0xFFFFFFF

Color3=oxF0F0F0

Edited by Sykeo

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks Sykeo! It's working like a charm, except for one problem. It writes sucessfully to a file as long as I use AddColor("Input"), but if I try to change the input to something from my box, it returns a different value. :whistle: Here's what I've constructed:

Func test()
    Local $Action = GUIGetMsg()
    Select
        Case $Action = $runtest
            Global $path = @ScriptDir & "\Test.ini"
            AddColor($TestInput)
EndSelect
EndFunc

Func AddColor($Color)
    $ColorString = StringFormat ("%s", $Color)
    $section = IniReadSection($path, "ColorList")
    IniWrite ($path, "ColorList", "List", IniRead($path, "ColorList", "List", "") & "Color" & $section[0][0] & "|")
    IniWrite ($path,"ColorList","Color" & $section[0][0], $ColorString)
EndFunc
Edited by Eru

Share this post


Link to post
Share on other sites

Hmm. That doesn't look as pretty as you guys' posts. :whistle: Did you use copy/paste to get the pretty colors in your posts or was that manual editing?

Share this post


Link to post
Share on other sites

You use the AutoIt tag instead of the Code tag to get the color syntax highlighting on the forum.


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Func test()
    Local $Action = GUIGetMsg()
    Select
        Case $Action = $runtest
            Global $path = @ScriptDir & "\Test.ini"
            AddColor($TestInput)
EndSelect
EndFunc

Func AddColor($Color)
    $ColorString = StringFormat ("%s", $Color)
    $section = IniReadSection($path, "ColorList")
    IniWrite ($path, "ColorList", "List", IniRead($path, "ColorList", "List", "") & "Color" & $section[0][0] & "|")
    IniWrite ($path,"ColorList","Color" & $section[0][0], $ColorString)
EndFunc

Nice, thank you. :whistle: Anyone know how I can get it to get the color value from the input box?

Share this post


Link to post
Share on other sites

Eru why don't you talk to Shynd, or Briant?

Sorry, I don't know who that is, this is my first topic on these forums. How would I contact them and why do I need to?

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