Friday, September 14, 2018

Powershell Bug in copy/move/rename when filename contains [square bracket characters]?

Please pardon a noob question, but I've been completely baffled by this one.



I have a "depositor" directory where user-submitted files arrive, and have no control over the incoming file names.



I created a parser in PS which quite successfully moves files (based on file name content) to an appropriate destination.



This works fine EXCEPT when a filename contains either "[" or "]".




Here is the "rename" pre-processor, which fails to actually rename a file containing either of the pesky bracket characters:



 cd $folderpath
foreach ($i in get-childitem $folderpath) {
if ($i.mode.substring(0,1) -ne “d”) {
$name = $i.name.replace("[","_")
$name = $name.replace("]","_")
Write-Host $i -foregroundcolor “blue”
Write-Host $name -foregroundcolor “green”


Rename-Item $i $name

}
}


This also fails for ren, copy, move and their cmdlet equivalents



Any insight you might be able to provide would be most welcome.




Thanks in advance . . .

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...