Variables problem (SOLVED)

Hi everyone,

I’m having a problem with variables.
I have a ini file with the following contents.
[EXTRAPATHS]
COUNT=3
1=FOLDER1
2=FOLDER2
3=FOLDER3

I want to read each line and create the folder.
Here’s my code. $INIContent is a function that reads the ini file.
So first it has to read $INIContent.EXTRAPATHS.1, then $INIContent.EXTRAPATHS.2 till the EXTRAPATHS.COUNT (3) is reached.

    For ($i=1; $i -le $INIContent.EXTRAPATHS.COUNT; $i++)
    {
        $EXTRAPATH= $PACKAGEPATH + "\" + $INIContent.EXTRAPATHS.$i
        New-Item $EXTRAPATH -type directory -force
    }

Sorry, Paul. Not sure I get what you need help with. If $INIContent is hashtable created by a function reading the INI file your code snippet works fine on my machine.

Do you need to use an INI file? I’m asking because there are other formats like CSV, JSON, PSD1 or XML which are easier to consume with PowerShell.

My working example:

$PACKAGEPATH = 'C:\TEMP'
$INIContent = @{
    EXTRAPATHS = @{
        COUNT = 3
        1 = 'FOLDER1'
        2 = 'FOLDER1'
        3 = 'FOLDER1'
    }
}

For ($i=1; $i -le $INIContent.EXTRAPATHS.COUNT; $i++)
{
    $EXTRAPATH = $PACKAGEPATH + "\" + $INIContent.EXTRAPATHS.$i
    New-Item $EXTRAPATH -type directory -force -WhatIf
}

Hi Daniel,

Contents of the INI are:

[EXTRAPATHS]
COUNT=3
1=FOLDER1
2=FOLDER2
3=FOLDER3

Here is the function to read the content from the ini file. (It is an inifile from a current application)

Function Get-IniContent
{  
    [CmdletBinding()]  
    Param
    (  
        [ValidateNotNullOrEmpty()]  
        [ValidateScript({(Test-Path $_)})]  
        [Parameter(ValueFromPipeline=$True,Mandatory=$True)]  
        [string]$FilePath  
    )  
    Process  
    {  
        $ini = @{}  
        switch -regex -file $FilePath  
        {  
        "^\[(.+)\]$" # Section  
            {  
                $section = $matches[1]  
                $ini[$section] = @{}  
                $CommentCount = 0  
            }  
        "^(;.*)$" # Comment  
            {  
                if (!($section))  
                {  
                    $section = "No-Section"  
                    $ini[$section] = @{}  
                }  
                $value = $matches[1]  
                $CommentCount = $CommentCount + 1  
                $name = "Comment" + $CommentCount  
                $ini[$section][$name] = $value  
            }   
        "(.+?)\s*=\s*(.*)" # Key  
            {  
                if (!($section))  
                {  
                    $section = "No-Section"  
                    $ini[$section] = @{}  
                }  
                $name,$value = $matches[1..2]  
                $ini[$section][$name] = $value  
            }  
        }  
        Return $ini  
    }  
}

Then I read the INI file with

$INIContent     = Get-IniContent "C:\Temp\test.ini"

The variable PackagePath is set

$PACKAGESPATH = "C:\temp\folders"

And then I want to create the folders

    #Creating the Extra folders
    For ($i=1; $i -le $INIContent.EXTRAPATHS.COUNT; $i++)
    {
        $EXTRAPATH= $PACKAGEPATH + "\" + $INIContent.EXTRAPATHS.$i
        New-Item $EXTRAPATH -type directory -force
    }

Is does not create the folder because $EXTRAPATH stays empty.

Thanks for the additional code. I think I’ve figured it out.

Just change

$INIContent.EXTRAPATHS.$i

to

$INIContent.EXTRAPATHS."$i"

and it should work.

$i needs to be converted into a string which “$1” does implicitly for you.

I hope above helps.

Thanks Daniel!
This is the solution.
I am a beginner in Powershell.
And did not see this.

My pleasure, Paul. Everyone here was a beginner at some point and got better over time. Just attempt to use PowerShell as much as possible for your daily tasks and projects, and you’ll get better. Additionally, I can really recommend to register a Twitter account and follow @PowerTip for example.