Search code examples
powershellget-childitem

Get-ChildItem not finding files


I am trying to recursively find files in folders, I have gotten the file names into an Array but the Get-ChildItem doesn't find the files. I can replace the $ImageNames variable with an actual string name and it finds them. So I know there is something wrong with the variable, but I can't pinpoint what it is. Here is a snip of the script.

I have tried to break the array into a foreach, but I get the same result with the single string.

Current output of $ImageNames from Write-Output of $imageNames
TEST_###_DPm.X.1.2.840.113681.2886735633.1532516094.5056.994912425400525861.dcm
TEST_###_DPm.X.1.2.840.113681.2886735633.1532516094.5056.996112425422850002.dcm
TEST_###_DPm.X.1.2.840.113681.2886735633.1532516094.5056.997312425470276903.dcm

Updated per advice, but still not working

foreach ($xmlFile in $sumReportArray)
    {

        $outputDirectory = $patientDir
        $subDirPerXML = Split-Path -Path $xmlFile -Leaf -Resolve
        $finalDir = $outputDirectory + '\' + $subDirPerXML
        [xml]$XmlDocument = Get-Content $xmlFile

        New-Item -ItemType Directory -Force -Path $finalDir | Out-Null
        $imageNames = $XmlDocument.VOLPARA_SERVER_INTERFACE.VolparaDicomSummaryReport.VolparaInputs.Image | Select-Object -ExpandProperty ImageFileName 
        Get-ChildItem -LiteralPath $volparaPath -include $imageNames -Recurse | Copy-Item -Destination $finalDir
    }

Here is the error I get...

Get-ChildItem : Illegal characters in path.
At C:\Users\AdamZenner\OneDrive - Volpara Health Technologies Limited\1 Volpara\Production Software\Script_VolparaServerSearch\VolparaServerSearch_1.0.ps1:90 char:13
+             Get-ChildItem -recurse -Path ($volparaPath) -filter ($ima ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Program File...araServer\DATA\:String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand


Solution

  • The output of ... | Select ImageFileName | Out-String contains unnecessary strings. (Header, Separator, etc.)

    So you should use Select-Object -ExpandProperty.

    $imageNames = … | Select-Object -ExpandProperty ImageFileName
    

    But in this case it is enough to use dot access.

    and if $imageNames is an array, use -Include parameter instead of -Filter parameter.

    $imageNames = $XmlDocument.VOLPARA_SERVER_INTERFACE.VolparaDicomSummaryReport.VolparaInputs.Image.ImageFileName
    Get-ChildItem -Path $volparaPath -Include $imageNames -Recurse | Copy-Item -Destination $finalDir