William Brooker
William Brooker

Reputation: 29

Is there a way to loop a script so that it uses a different value with each pass through

I have a full script which works just fine. The issue is, I have a system of GUI's set up which are all used for varying roles and what I need is a select all function on one of these GUI's. For example;

###############################MONTH SELECTER############################################################

[array]$DropDownArrayItems = "","01","02","03","04","05","06","07","08","09","10","11","12", 'Select all months'
[array]$DropDownArray = $DropDownArrayItems | sort

function Return-DropDown {
    if ($DropDown.SelectedItem -eq $null){
        $DropDown.SelectedItem = $DropDown.Items[0]
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
    else{
        $script:Choice = $DropDown.SelectedItem.ToString()
        $Form.Close()
    }
}

function SelectGroup{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


    $Form = New-Object System.Windows.Forms.Form

    $Form.width = 300
    $Form.height = 150
    $Form.Text = ”Select Filter Month”

    $DropDown = new-object System.Windows.Forms.ComboBox
    $DropDown.Location = new-object System.Drawing.Size(100,10)
    $DropDown.Size = new-object System.Drawing.Size(130,30)

    ForEach ($Item in $DropDownArray) {
     [void] $DropDown.Items.Add($Item)
    }

    $Form.Controls.Add($DropDown)

    $DropDownLabel = new-object System.Windows.Forms.Label
    $DropDownLabel.Location = new-object System.Drawing.Size(10,10) 
    $DropDownLabel.size = new-object System.Drawing.Size(100,40) 
    $DropDownLabel.Text = "Select Month:"
    $Form.Controls.Add($DropDownLabel)

    $Button = new-object System.Windows.Forms.Button
    $Button.Location = new-object System.Drawing.Size(100,50)
    $Button.Size = new-object System.Drawing.Size(100,20)
    $Button.Text = "OK"
    $Button.Add_Click({Return-DropDown})
    $form.Controls.Add($Button)
    $form.ControlBox = $false

    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()


    return $script:choice
}

$Group = $null
$Group = SelectGroup
while ($Group -like ""){
    $Group = SelectGroup
}

###################################################################################

This selects a month and is used in things like file paths and filters the problem is, if someone wants data from every month, they have to manually repeat the script 12 times and answer all the GUI'S again.

I've tried;

if ($Group -eq 'Select all') {
    1..12 | ForEach-Object { '{0:00}' -f $_ }
}

But the file path gets confused and things break so including a select all function and taking numbers 1-12 doesn't quite work.

So is there a way to loop the entire script such that with each pass through it selects a different month whilst keeping all other variables in the script the same if the 'select all months option is selected'

Upvotes: 0

Views: 130

Answers (1)

Mark
Mark

Reputation: 434

I don't think the problem that you're having is in the code you posted.

The easiest thing to do would be to take all the code past the Select Group, and move it into it's own function.

function DoWork { params ([int]$Month)
$Month = '{0:00}' -f $Month

Echo $Month
#Do your work- this is where you code past the UI goes
}


#This is where your drop down labels, etc goes. 

if ($Group -eq 'Select All') {
    1..12 | ForEach-Object {DoWork($_)}
} else {
    DoWork($Group)
}

This will call your DoWork (or whatever you name it) function 12 times, each with a different $Month, if Select All is used.

Depending on variable scope- if your first run through changes a variable, your second run through might start with that changed variable. I'm worried that might be where "they file path gets confused" might be coming from.

If so, look at how you change the $Path variable- or whatever it is.

Upvotes: 1

Related Questions