Powershell:当文件名包含char []时,移动项目不起作用 [英] Powershell: moving items not working when filenames that have chars [ ]

查看:75
本文介绍了Powershell:当文件名包含char []时,移动项目不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用PowerShell移动项目的快速问题:有人知道文件名上带有[或]字符时为什么以下脚本不起作用吗? (例如: file1 [VT] .txt )

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  
  } 
}

例如,如果文件名为 file1.txt ,它将移动文件,但只会忽略名为 file1 [VT] .txt 的文件.我假设它的名称上带有chars或[]时找不到文件的路径.有什么想法吗?

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?

推荐答案

只需将-literalpath参数用于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  
  } 
}

这篇关于Powershell:当文件名包含char []时,移动项目不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆