Forum Discussion

GGuezet's avatar
GGuezet
Contributor
6 years ago
Solved

Method RemoveOption don't work ?

Hi, I'm trying to use the method RemoveOption (and removeOptionByIndex) on one of my ini files, but it didn't seem to work

 

This is what my code (VBS) looks like:

 

 

function clear_watchdog

    INI_PATH = C:\xxx\xxx\variables.ini


    for i = 0 to Storages.INI(INI_PATH).GetSubSection("VARIABLES").OptionCount-1
        OptionName = Storages.INI(INI_PATH).GetSubSection("VARIABLES").GetOptionName(i)		
        if InStr(OptionName,"MEM_USAGE_STARTUP") <> 0 then
            Storages.INI(INI_PATH).GetSubSection("VARIABLES").RemoveOption(OptionName)
        end if	
    next

    Storages.INI(INI_PATH).Save
    End Function

 

And there is an extract of my .ini file :

 

[VARIABLES]
D_START=09/05/18
H_START=10:09:34
BDD_TYPE=SQL
TEST_EXECUTION_ID=XXXXXX
RunTheProject=true
D_DEBUT=16/05/18
H_DEBUT=17:10:50
MS_XXX_13268_MEM_USAGE_STARTUP=75185
MS_XXX_10804_MEM_USAGE_STARTUP=88404
MS_XXX_6520_MEM_USAGE_STARTUP=24888
MS_XXX_13396_MEM_USAGE_STARTUP=20056

[Root]

 

Basicly everything works fine, but when my code goes to the line

"Storages.INI(INI_PATH).GetSubSection("VARIABLES").RemoveOption(OptionName) " it execute it (Or at least dont log any errors), but my .ini file still contain the option it was supposed to delete...

 

I've tried with RemoveOption and RemoveOptionByIndex, still nothing.

 

How do I do to remove thoses options ?

Thanks

 

  • I'm not sure... but I think it might have to do with you referencing the INI file as "Storages.INI(INI_PATH)" throughout the code rather than assigning the particular object.  I'm not a VBSCript guy but what happens if your change your code like so:

    function clear_watchdog
    
        INI_PATH = C:\xxx\xxx\variables.ini
        INIFile = Storages.INI(INI_PATH)
    
    
        for i = 0 to INIFile.GetSubSection("VARIABLES").OptionCount-1
            OptionName = INIFile.GetSubSection("VARIABLES").GetOptionName(i)		
            if InStr(OptionName,"MEM_USAGE_STARTUP") <> 0 then
                INIFile.GetSubSection("VARIABLES").RemoveOption(OptionName)
            end if	
        next
    
        INIFile.Save
        
    End Function

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I'm not sure... but I think it might have to do with you referencing the INI file as "Storages.INI(INI_PATH)" throughout the code rather than assigning the particular object.  I'm not a VBSCript guy but what happens if your change your code like so:

    function clear_watchdog
    
        INI_PATH = C:\xxx\xxx\variables.ini
        INIFile = Storages.INI(INI_PATH)
    
    
        for i = 0 to INIFile.GetSubSection("VARIABLES").OptionCount-1
            OptionName = INIFile.GetSubSection("VARIABLES").GetOptionName(i)		
            if InStr(OptionName,"MEM_USAGE_STARTUP") <> 0 then
                INIFile.GetSubSection("VARIABLES").RemoveOption(OptionName)
            end if	
        next
    
        INIFile.Save
        
    End Function
    • GGuezet's avatar
      GGuezet
      Contributor

      Hummm, it has actually done the trick !

       

      But I'm not really sure why it didn't work in the first place, Storages.INI(INI_PATH) is supposed to return the FileSection object.

       

      Assigning it to an object via "Set INIFile = Storage.INI(INI_PATH)" and then calling "INIFile.GetSubSection("VARIABLES").RemoveOption" worked fine !

       

      Thanks you again

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        The reason being is that Storages.INI(INI_PATH) returns a new instance of the ini file object each time. So, when you do the remove, it's removed from that instance... but then your save call is saving a different instance.  By doing the Set INIFILE first, you are instantiating the object and then working with that instance through your code so everything is happening against the same object instance.