Hello:
I am a retired software developer just working on my music library.
My problem is with square brackets in file names which are valid names but cause problems in my scripts.
I wrote a simple script to illustrate. The directories in the working directory should be listed
with a count of the files in them. Each directory has 1 file. Filenames with matching square brackets
show zero files. Filenames with unmatched square brackets cause an error, indicating an invalid
wildcard pattern. I am not a “regular expression” guru but the filename seems to be being evaluated as such?
Here is the script and output"
PS C:\Users\jhibn_000\powershell\Recs\xx’ yy>
get-ChildItem
$parent = get-item .
$parentDirs = $parent.GetDirectories()
for($i=0; $i -lt $parentDirs.Count; $i++) {
$children = get-childitem $parentDirs[$i].FullName -file
$parentDirs[$i].FullName + " has " + $children.Count
}
get-host | Select-Object -property version
Directory: C:\Users\jhibn_000\powershell\Recs\xx’ yy
Mode LastWriteTime Length Name
d----- 4/21/2020 12:38 AM All Aboard!
d----- 9/18/2014 1:20 AM AnotherSide+Times{2_2}
d----- 9/18/2014 1:20 AM AnythingGoes+SweetCharity1
d----- 9/18/2014 1:20 AM BackRoads+[NashvilleSkyline
d----- 4/26/2020 6:40 PM xx=.-cc
d----- 4/26/2020 6:40 PM zz=.-cc
d----- 9/18/2014 1:20 AM _+PlanetWaves
C:\Users\jhibn_000\powershell\Recs\xx’ yy\All Aboard! has 1
C:\Users\jhibn_000\powershell\Recs\xx’ yy\AnotherSide+Times{2_2} has 1
C:\Users\jhibn_000\powershell\Recs\xx’ yy\AnythingGoes+SweetCharity1 has 0
Get-ChildItem : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not
valid: BackRoads+[NashvilleSkyline
At line:7 char:15
+ $children = get-childitem $parentDirs[$i].FullName -file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: ( [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetChildItemCommand
C:\Users\jhibn_000\powershell\Recs\xx’ yy\BackRoads+[NashvilleSkyline has 0
C:\Users\jhibn_000\powershell\Recs\xx’ yy\xx=.-cc has 1
C:\Users\jhibn_000\powershell\Recs\xx’ yy\zz=.-cc has 1
C:\Users\jhibn_000\powershell\Recs\xx’ yy_+PlanetWaves has 1
Version : 5.1.18362.752
I’m sure I can write something to change the offending names but would rather not. Is it possible to avoid this?
Thanks
Joe