0
votes

I am using the following code to select a folder through the Windows Forms "Browse" function and then pass that path to the gci cmdlet

cls

Function Get-Directory($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenfolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
 $OpenfolderDialog.RootFolder = $initialDirectory
 $OpenfolderDialog.ShowDialog()| Out-Null
 $StartDir = $OpenfolderDialog.SelectedPath 
 Return $StartDir | Out-String
 } 

 $myDir = Get-Directory -initialDirectory "Desktop"

 $Child = gci -path $mydir -r -Filter *.jpg 

 Foreach ($item in $Child) {Move-Item -path $item.pspath -Destination $myDir -Force}

but I get these errors:

***At C:\Test\Combine Pics2.ps1:17 char:13 + $Child = gci <<<< -path $mydir -r -Filter *.jpg + CategoryInfo : ObjectNotFound: (C:\Test :String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Move-Item : Cannot bind argument to parameter 'Path' because it is null. At C:\Test\Combine Pics2.ps1:19 char:43 + Foreach ($item in $Child) {Move-Item -path <<<< $item.pspath -Destination $myDir -Force} + CategoryInfo : InvalidData: (:) [Move-Item], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.MoveItemCommand***

The $myDir variable is of type String, why does it not pass to the -path parameter.

3
It is of the type string, but what is actually in it? Have you output it to make sure it is right?EBGreen
have you tried running it with a debugger, like powershell_iseash

3 Answers

0
votes

I got a newline and carriage return at the end of the $mydir value, so try trimming with something like this to see if that is your issue:

$Child = gci -path $mydir.Trim("`r`n") -r -Filter *.jpg

Update: Better yet, just lose the Out-String in your function:

Return $StartDir
2
votes

What if the user canceled the dialog? Give this a try:

Function Get-Directory($initialDirectory)
{   
     [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

     $OpenfolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
     $OpenfolderDialog.RootFolder = $initialDirectory
     $result = $OpenfolderDialog.ShowDialog()

     if($result -eq 'ok')
     {
         $OpenfolderDialog.SelectedPath
     }
     else
     {
        "canceled"
     }
 } 


$mydir = Get-Directory -initialDirectory Desktop

if($mydir -ne 'canceled')
{
    gci -path $mydir
}
1
votes

Try this:

Function Get-Directory($initialDirectory)
{   
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

    $OpenfolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenfolderDialog.RootFolder = $initialDirectory
    if ($OpenfolderDialog.ShowDialog() -eq "OK") {
        #Continue only if a folder was selected
        $OpenfolderDialog.SelectedPath
    }
} 

$myDir = Get-Directory -initialDirectory "Desktop"

#Continue only if a folder was selected
if($myDir) {
    $Child = Get-ChildItem -path $mydir -Recurse -Filter *.jpg
    Foreach ($item in $Child) {
        Move-Item -path $item.pspath -Destination $myDir -Force
    }
}

I cleaned it up with a few if-tests so it doesn't return errors when people cancel the dialog. There was no need to Out-String as SelectedPath returns a single string by itself.