jeudi 6 juin 2019

Robocopy ForEach, but completely different paths?

We have a PowerShell GUI form, with some checkboxes corresponding to folders, and if a checkbox is checked, a Robocopy will be performed for that specific folder. For instance, if checkbox1 is checked, C:\Source\Folder1 will be copied to C:\Target\Folder1, and so on. This currently works perfectly with a ForEach loop, where the script takes the foldername from each checkbox Text. BUT, soon we will get a different file structure, so the paths will no longer be identical (folder will no longer be located under same folder, like they are now). For instance, folder1 might be moved to C:\Plans\folder1 while folder2 might be moved to C:\ForDesignDept\folder2 - these are just examples.

Idea for solution: We realize we can add a .Tag property to the checkboxes and add any data, like a filepath, but we'd need at least 2 values to define a source and target path.

We believe this can be done with just a little bit of tinkering with our current Function show below?

TL;DR: We need to modify our current ForEach loop to use different paths for every loop, instead of identical paths where "folder1, folder2 etc" are all located under same folder like they currently are.

Any help would be appreciated! Thank you

We have tried different variations of adding several values to .Tag property, like so: We have tried double quotes, single quotes, and so on. And the next problem might be that Powershell will say no destination is specified because it will think the .Tag property is just 1 value. $checkbox1.Tag = "C:\Plans\Folder1" "C:\ForDesignDept\Folder1"

As shown here, the script takes the foldername from each checkbox Text.

{
    $checkboxes = @($checkbox1, $checkbox2, $checkbox3)

    foreach ($checkbox in $checkboxes) 
    {
        if ($checkbox.Checked) 
        {
            robocopy "c:\source\$($checkbox.Text)" "c:\target\$($checkbox.Text)" /njh /njs /ndl /np /tee | ForEach-Object { $outputBox.AppendText($_ + "`r`n") }
        } 
        else 
        {
            $outputBox.AppendText("$($checkbox.Text) NOT SELECTED" + "`r`n")
        }
    $checkbox.Checked=$False
    }
}

Expect identical behaviour to current solution, except using non-identical paths. No specific errors since we just don't know how to do this.

Aucun commentaire:

Enregistrer un commentaire