0

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.

2
  • It is of the type string, but what is actually in it? Have you output it to make sure it is right? Commented Jan 23, 2013 at 19:38
  • have you tried running it with a debugger, like powershell_ise Commented Jan 23, 2013 at 20:00

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

1 Comment

I output the type for a test and I was just getting an object and not a string. That is why I used the Out-string. I will try the trim and see what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.