Powershell:在具有chars []的文件名时移动项目不起作用(Powershell: moving items not working when filenames that have chars [ ])
关于使用PowerShell移动项目的快速问题:有没有人知道为什么当文件名上有[或]字符时,以下脚本不起作用? (例如: file1 [VT] .txt )
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if ($destination -ne $null) { mi $_.PSPath $destination.PSPath -Verbose -WhatIf } }例如,它将移动文件,如果它被称为file1.txt,但它将忽略名为file1 [VT] .txt的文件 。 我假设当它的名字上有chars [或]时,它找不到文件的路径。 有任何想法吗?
Quick question about moving items with PowerShell: does anyone know why the following script does not work when the filename has the [ or ] chars on it? (ex.: file1[VT].txt)
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if ($destination -ne $null) { mi $_.PSPath $destination.PSPath -Verbose -WhatIf } }For instance, it will move the file if it's called file1.txt but it will simply ignore files named file1[VT].txt. I'm under the assumption that it's not finding the path to the file when it has chars [ or ] on its name. Any ideas?
最满意答案
只需为move-item使用-literalpath参数
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if( $destination -ne $null){ mi -literalpath $_.PSPath $destination.PSPath -Verbose -WhatIf } }Just using -literalpath parameter for move-item
ls j:\ | foreach { $itemName = $_.Name.Replace('.', ' ') $destination = ls | where { $itemName -match $_.Name } | select -First 1 if( $destination -ne $null){ mi -literalpath $_.PSPath $destination.PSPath -Verbose -WhatIf } }更多推荐
发布评论