Powershell:在具有chars []的文件名时移动项目不起作用(Powershell: moving items not working when filenames that have ch

编程入门 行业动态 更新时间:2024-10-10 23:24:09
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 } }

更多推荐

本文发布于:2023-08-03 18:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   不起作用   项目   chars   Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!