Jump to content

How to delete sections from INI multiple times


Recommended Posts

I am reading from an INI and then after performing my tasks I try to delete the section from the INI. I cant seem to figure out how to delete a section, then read the INI and then delete another section and so on.

My INI:

[class.07.08.2011.1700]
room=b
classLength=30
cueLength=55000
instructorFirst=Liana
instructorLast=Liana
className=Express_ABS
[class.07.08.2011.1730]
room=b
classLength=30
cueLength=55000
instructorFirst=Elena
instructorLast=Elena
className=Booty_Beat

And My AutoIt script:

$IniSource = @DesktopDir & "\Autoit Scripts\classSchedule.ini"
;Get section names from INI source. Section names are class.MM.DD.YY.HHMM
$sections = IniReadSectionNames($IniSource)
If @error Then
    MsgBox(4096, "", "Error occurred with IniReadSectionNames.")
Else
    ; count sections
    $sectionsLength = UBound($sections) - 1
EndIf

; check if $sections array contains anything
If $sectionsLength > 1 Then

    ; setup infinite loop so we keep checking class schedules
    $classLoop = 0
    While $classLoop == 0
        ;loop through array to get schedule times
        For $i = 1 To $sections[0]
            
            ;Get now time to compare to schedule times
            $nowTime = "class." & @MON & "." & @MDAY & "." & @YEAR & "." & @HOUR & @MIN
            ;Get Hour and MIN for future check against late/early starting classes (TODO)
            $nowHour = @HOUR
            $nowMin = @MIN
            
            
            $endTime = "" & $endHour & $endMin & ""
            ;MsgBox(4096, "", $endTime, 2)

            ;If the time matches a class time
            If $nowTime == $sections[$i] Then
                MsgBox(4096, "", $sections[$i], 1)
                
                ;Get the room letter that will be live. Default to room x so we can issue error if x
                $classRoom = IniRead($IniSource, $sections[$i], "room", "x")
                ;Get the length of class minutes
                $classLength = IniRead($IniSource, $sections[$i], "classLength", "60")
                $iCueLength = IniRead($IniSource, $sections[$i], "cueLength", "60000")
                $sInstructorFirst = IniRead($IniSource, $sections[$i], "instructorFirst", "a")
                $sInstructorLast = IniRead($IniSource, $sections[$i], "instructorLast", "b")
                $sClassName= IniRead($IniSource, $sections[$i], "className", "c")
                If @error Then
                    MsgBox(4096, "IniRead Error", "IniRead encountered an error line 50 schedule-ini.au3", 3)
                Else
                    
                    ; If no error in IniRead then initiate _RecordClass function
                    _RecordClass($classRoom, $classLength, $sections[$i], $iCueLength, $sClassName, $sInstructorFirst, $sInstructorLast)
                    ; Delete recording class from IniSource
                    IniDelete($IniSource, $sections[$i])
                    If @error Then
                        MsgBox(4096, "IniDelete Error", "Ini Delete encountered an error line 55 schedule-ini.au3")
                    EndIf
                    _ClassEmail($sections[$i])
                EndIf
            EndIf
        Next
        
        ;Sleep for 10 seconds between searching for classes in .ini
        Sleep (10000)
        
        $updatedSections = IniReadSectionNames($IniSource)
        $updatedSectionsLength = UBound($updatedSections) - 1
        If $updatedSectionsLength < 2 Then
            ;If no more class schedule end loop
            $classLoop = 1
        EndIf
        
    WEnd

EndIf
Exit

I'm a complete noob with AutoIt and so I know there is probably A LOT going wrong here. Any help is appreciated!

Edited by SheanHoxie
Link to comment
Share on other sites

IniDelete() without a "key" parameter, see help file.

:)

Thanks for your help. I'm trying to delete the whole section, not just the key. I've gone through the IniDelete in the help file a few times, but I will go through again, maybe I'm missing something obvious ;)

Link to comment
Share on other sites

So I've reread the IniDelete() in the help file, and I think that I have it correct. I think the problem is actually within my loop what I am passing to $section[$i] isn't being updated properly.

Here is how my brain is working:

Getting the INI and reading it...

$IniSource = @DesktopDir & "\Autoit Scripts\classSchedule.ini"
$sections = IniReadSectionNames($IniSource)

Since INI section name is class.MM.DD.YYYY.HHMM set a var with right nows time and date...

$nowTime = "class." & @MON & "." & @MDAY & "." & @YEAR & "." & @HOUR & @MIN

If it matches, do stuff...

If $nowTime == $sections[$i] Then
;
; Do Stuff
;
; Then Delete the $section[$i]
IniDelete($IniSource, $sections[$i])
EndIf

So far, this works if I only have 1 section. If I have multiple sections, after I delete the first section, it fails to delete any other sections. I think that I am missing something when I loop that fails to replace the $sections[$i] variable, but it will loop through ok and perform all functions perfectly except the IniDelete() portion.

Again, thanks for any help.

Edited by SheanHoxie
Link to comment
Share on other sites

  • Moderators

SheanHoxie,

You are specifying the $nowTime to the minute, which is pretty limiting - how many of your ini sections are you expecting to match? :)

You need to post your script and a representative ini file so that we can see how you loop through the elements of the $sections array. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

If you have it in a For..Next loop like this, it should work.

$IniSource = @DesktopDir & "\Autoit Scripts\classSchedule.ini"
$sections = IniReadSectionNames($IniSource)

For $i = 1 To $sections[0]
    If $nowTime = $sections[$i] Then
    ;
    ; Do Stuff
    ;
    ; Then Delete the $section[$i]
    IniDelete($IniSource, $sections[$i])
    EndIf
Next

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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