none
Reading INI files with dashes in the section name RRS feed

  • Question

  • I am trying to read an INI file from a web site. Within that INI file are sections that have dashes in them. I need to be able to read several sections so I can locate specific files to download referenced within the INI files. I found a script for parsing INI files, but when I run it, the script fails to handle the sections that have dashes. Any ideas on how I can work around the issue?

    $Web = New-Object System.Net.WebClient
    Function Get-INIContent ($ContentList){
        $INI = @()
        Switch -regex ($contentList)
            {
            "^\[(.+)\]" 
                {
                [string]$Section = $Matches[1]
                $INI[$Section] = @{}
                $CommentCount = 0
                }
            }
    
    }
    
    $ContentList = $Web.Downloadstring("http://download.nai.com/PRODUCTS/COMMONUPDATER/avvdat.ini")
    Get-INIContent $contentlist

    What I was hoping to do was search through the INI until I found the sections I want, and then read the fields for that section so I know what to download.  I won't be pulling all of the files, so I need to find a way to search for the sections I am looking for then read all of the lines for that section and process them.

    Monday, October 6, 2014 4:53 PM

Answers

All replies

  • Found part of the problem... When I pull the file into a variable I lose the ability to search it line by line.  For example:

    $Web = New-Object System.Net.WebClient
    $ContentList = $Web.Downloadstring("http://download.nai.com/PRODUCTS/COMMONUPDATER/avvdat.ini")
    
    #For some reason this returns the entire file
    $contentlist | select -first 1
    
    $contentlist | %{
          #Thought this would be one line, but it is also
          #the entire file...
          $_
    }

    I have since shifted to a different method of reading the ini and I can process row by row...

    $Web         = New-Object System.Net.WebClient
    $INILocation = "c:\SomeLocation\avvdat.ini"
    
    
    $Web.DownloadFile("http://download.nai.com/PRODUCTS/COMMONUPDATER/avvdat.ini",$INILocation)
    $INIFile = [System.IO.File]::OpenText($INILocation)
    
    Do {
        $CurrentLine = $INIFile.ReadLine()
        #This is now one line like it should be.
        $CurrentLine
        }
    While ($INIFile.EndOfStream -ne $True)
    

    Monday, October 6, 2014 5:32 PM
  • Here is a PowerShell INI file parser that works just right.

    http://gallery.technet.microsoft.com/Edit-old-fashioned-INI-f8fbc067


    ¯\_(ツ)_/¯

    • Marked as answer by Oldguard Monday, October 6, 2014 7:29 PM
    Monday, October 6, 2014 6:43 PM
  • That's very nice. Thank you.
    Monday, October 6, 2014 7:30 PM
  • Why parse a file when we already have a parser in the system.  Good old p/Invoke.


    ¯\_(ツ)_/¯

    Monday, October 6, 2014 8:43 PM