RoboCopy ForEach循环是否带有标签?

时间:2019-09-25 15:40:44

标签: powershell foreach tags robocopy

我们有一个PowerShell GUI,用于备份文件。该GUI包含多个复选框,每个复选框代表一个文件路径,可以根据需要选中或取消选中它们。 我们有一个带有ForEach循环的函数,该函数在单击“备份”按钮时被调用,然后使用每个复选框的Tag属性定义源路径和目标路径,并且该函数中的Tag是“ Split”,因此Powershell知道用作来源,以及用作目的地。

这很好用。 问题在于,对于其中一个复选框,我们要备份多个路径,而不仅仅是Tag中定义的一个源。例如,当选中“ Folder3”复选框时,我们希望同时备份C:\ Src \ Folder3和D:\ Src2 \ Folder3。 我们将如何做? -是否可以向复选框添加更多标签?如果是这样,那么在ForEach循环中必须对Split进行哪些更改?

感谢您提供任何帮助!

完整代码在这里:


#ENABLE VISUALSTYLES#
[System.Windows.Forms.Application]::EnableVisualStyles()

#------------------------------------------------------------------------------FORM START------------------------------------------------------------------------------#

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1450,842)
$form.FormBorderStyle = "FixedDialog"
$form.StartPosition = "CenterScreen"
$form.Topmost = $False
$form.add_Load($FormEvent_Load)

$outputBox = New-Object System.Windows.Forms.TextBox 
$outputBox.Location = New-Object System.Drawing.Size(200,110)
$outputBox.Size = New-Object System.Drawing.Size(1220,643)
$outputBox.MultiLine = $True 
$outputBox.ScrollBars = "Both"
$outputBox.BackColor = "White"
$outputBox.ForeColor = "Black"
$outputBox.WordWrap = $False
$outputBox.ReadOnly = $True
$outputBox.Font = New-Object System.Drawing.Font("Consolas",10)
$Form.Controls.Add($outputBox)      

#------------------------------------------------------------------------------FORM END------------------------------------------------------------------------------#

#------------------------------------------------------------------------------BUTTONS START------------------------------------------------------------------------------#

$CopyButton = New-Object System.Windows.Forms.Button
$CopyButton.Location = New-Object System.Drawing.Size(10,200)
$CopyButton.Size = New-Object System.Drawing.Size(180,35)
$CopyButton.Text = "Backup selected files"
$CopyButton.Add_Click({FILE_BACKUP})
$form.Controls.Add($CopyButton)

$CloseButton = New-Object System.Windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(10,250)
$CloseButton.Size = New-Object System.Drawing.Size(180,35)
$CloseButton.Text = "Close"
$CloseButton.Add_Click({$form.Close()})
$form.Controls.Add($CloseButton)


#------------------------------------------------------------------------------BUTTONS END------------------------------------------------------------------------------#

#------------------------------------------------------------------------------CHECKBOXES START------------------------------------------------------------------------------#

$CheckBoxFolder1 = New-Object System.Windows.Forms.checkbox
$CheckBoxFolder1.Location = New-Object System.Drawing.Size(10,15)
$CheckBoxFolder1.Size = New-Object System.Drawing.Size(90,20)
$CheckBoxFolder1.Checked=$True
$CheckBoxFolder1.Text = "Path 1"
$CheckBoxFolder1.Tag = "C:\src\Folder1>C:\dest\Folder1"
$form.Controls.Add($CheckBoxFolder1)

$CheckBoxFolder2 = New-Object System.Windows.Forms.checkbox
$CheckBoxFolder2.Location = New-Object System.Drawing.Size(10,35)
$CheckBoxFolder2.Size = New-Object System.Drawing.Size(94,20)
$CheckBoxFolder2.Checked=$True
$CheckBoxFolder2.Text = "Path 2"
$CheckBoxFolder2.Tag = "C:\src\Folder2>C:\dest\Folder2"
$form.Controls.Add($CheckBoxFolder2)

$CheckBoxFolder3 = New-Object System.Windows.Forms.checkbox
$CheckBoxFolder3.Location = New-Object System.Drawing.Size(10,55)
$CheckBoxFolder3.Size = New-Object System.Drawing.Size(115,20)
$CheckBoxFolder3.Checked=$True
$CheckBoxFolder3.Text = "Path 3"
$CheckBoxFolder3.Tag = "C:\src\Folder3>C:\dest\Folder3"
$form.Controls.Add($CheckBoxFolder3)

#------------------------------------------------------------------------------CHECKBOXES END------------------------------------------------------------------------------#

#------------------------------------------------------------------------------FUNCTIONS START------------------------------------------------------------------------------#

function FILE_BACKUP {
$CheckBoxes = @($CheckBoxFolder1, $CheckBoxFolder2, $CheckBoxFolder3)

    foreach ($CheckBox in $CheckBoxes)
    {
    $FilePath=$($CheckBox.Tag.Split(">"))
     if ($CheckBox.Checked) 
        {
            robocopy $FilePath[0] $FilePath[1] /njh /njs /ndl /np /tee | ForEach-Object { $outputBox.AppendText($_ + "`r`n") }
        } 
        else 
        {
            $outputBox.AppendText("$($checkbox.Text) NOT SELECTED" + "`r`n")
        }
    }
}

#------------------------------------------------------------------------------FUNCTIONS END------------------------------------------------------------------------------#

$form.ShowDialog()```

1 个答案:

答案 0 :(得分:0)

您可以使用逗号运算符(.Tag)将,制成数组。

$checkboxfolder2.Tag = "C:\src\Folder2>C:\dest\Folder2","C:\src\Folder3>C:\dest\Folder3"

然后在您的函数调用中,您可以将每个标签传递到Foreach-Object中并进行相应处理。

function FILE_BACKUP {
$CheckBoxes = $CheckBoxFolder1,$CheckBoxFolder2,$CheckBoxFolder3

    foreach ($CheckBox in $CheckBoxes)
    {
        $CheckBox.Tag | Foreach-Object {
            $FilePath = $_.Split(">")
            if ($CheckBox.Checked) 
            {
                robocopy $FilePath[0] $FilePath[1] /njh /njs /ndl /np /tee |
                    ForEach-Object { $outputBox.AppendText($_ + "`r`n") }
            } 
            else 
            {
                $outputBox.AppendText("$($checkbox.Text) NOT SELECTED" + "`r`n")
            }
        }
    }
}