0
votes

I need to iterate over an array of folder names, passing them into GCI so I can run operations on their contents. No matter what I do, it keeps giving me this error:

gci : Illegal characters in path.
At C:\Scripts\Arvest_submission_monitoring.ps1:86 char:5
+     gci -path "D:\subftp\$_" -recurse {
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (D:\SUBFTP\folder1:String [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : 
    DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

I've created an array:

$folders = "folder1","folder2","folder3"

And then iterate over it:

$folders | % {
  gci -Path "D:\subftp\$_" -Recurse {
    #do stuff here
  }
}

I've tried many various options such as a regex replace to remove any possible illegal characters (although a $folder.Count shows me there are no invisible characters), I've tried turning $folders into a C# ArrayList but it's still a no go. I've even played around with the path variable itself concatenating it into a single string before use.

The weird thing is if I try and execute it from the command line it works fine. What gives? I'm running v5.

1
Have you tried -LiteralPath instead of -Path? - lit
Please show the actual paths. - Ansgar Wiechers
I'm trying to avoid identifying information, but they all are in this format: "D:\SUBFTP\58-BANK" And wouldn't -LiteralPath ignore the variable I'm trying to pass? - fudge
The variable inside the double-quotes will get expanded before the value is bound to the parameter - Mathias R. Jessen
$folder.Count is not going to show you anything useful - do $_.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) and observe if it returns any value other than -1. ie. "not?a?valid*filename".IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) will return 3 because of the ? at index 3 - Mathias R. Jessen

1 Answers

1
votes

It is caused by the curly brace right at the end of your command gci -Path "D:\subftp\$_" -Recurse {.

Probably you wanted to do this:

$folders | ForEach-Object {
  Get-ChildItem -Path "D:\subftp\$_" -Recurse | ForEach-Object {
    #do stuff here
  }
}