I am trying to create a directory structure if it doesn't exist in another location. It works, but I get an error on any directory with a bracket in the name. I assume I have to escape that somehow, but don't know how.
Script code:
$source = "c:\data"
$destination = "c:\scrap\data"
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object {
if (!(Test-Path -path $_ )) { $null = New-Item -ItemType Container -Path $_ }
}
-ErrorAction SilentlyContinueto the cmdlet which is throwing the un-useful error.$error[0].exception.tostring()to your post.New-Item : Item with specified name C:\scrap\data\Evernote\backup\Untitled note [2]_files already exists. At C:\data\PowerShell\Untitled5.ps1:9 char:67 + ForEach-Object {if (!(Test-Path -path $_ )) { $null = New-Item <<<< -ItemType Container -Path $_ }} + CategoryInfo : ResourceExists: (C:\scrap\data\E... note [2]_files:String) [New-Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommandTest-Pathbefore trying to create the directory. What do you get when you manually runTest-PathandNew-Itemwith the path that raises the error?