【问题标题】:Replacing all # in filenames throughout subfolders in Windows在 Windows 的子文件夹中替换所有 # in 文件名
【发布时间】:2014-12-26 04:41:11
【问题描述】:

将大约 200,000 个文件迁移到 OneDrive for business,并发现有一些他们不喜欢的字符 - 最大的违规者是 #。我有大约 3,000 个带有哈希的文件,我想将它们全部替换为 No.。例如旧的:File#3.txt 新的:File No.3.txt

我尝试使用 PowerShell 脚本,但它也不喜欢 #

Get-ChildItem -Filter "*#*" -Recurse |
  Rename-Item -NewName { $_.name -replace '#',' No. ' }

我在搞清楚保留字符的语法时运气不佳 - 我试过 \##\'*#*',但没有运气。

谁能阐明这一点或提供一种快速的方法来递归替换所有这些哈希标签?

谢谢。

【问题讨论】:

  • 如果您在标签或标题中指定您工作的环境,您可能会得到更多答案

标签: windows powershell replace find


【解决方案1】:
Mode                LastWriteTime     Length Name       
----                -------------     ------ ----      
-a---        30.10.2014     14:58          0 file#1.txt
-a---        30.10.2014     14:58          0 file#2.txt

PowerShell 使用反引号 (`) 作为转义字符,并使用双引号来评估内容:

Get-ChildItem -Filter "*`#*" -Recurse |
Rename-Item -NewName {$_.name -replace '#','No.' } -Verbose

Get-ChildItem -Filter "*$([char]35)*" -Recurse | 
Rename-Item -NewName {$_.name -replace "$([char]35)","No." } -Verbose

两者都可以。

Get-ChildItem -Filter "*`#*" -Recurse | 
           Rename-Item -NewName {$_.name -replace "`#","No." } -Verbose

VERBOSE: Performing the operation "Rename File" on target 
"Item: D:\tmp\file#1.txt Destination: D:\tmp\fileNo.1.txt".
VERBOSE: Performing the operation "Rename File" on target 
"Item: D:\tmp\file#2.txt Destination: D:\tmp\fileNo.2.txt".

这也可以,

Get-ChildItem -Filter '*#*' -Recurse |
Rename-Item -NewName {$_.name -replace '#', 'No.'} -Verbose

VERBOSE: Performing the operation "Rename File" on target 
"Item: D:\tmp\file#1.txt Destination: D:\tmp\fileNo.1.txt".
VERBOSE: Performing the operation "Rename File" on target
"Item: D:\tmp\file#2.txt Destination: D:\tmp\fileNo.2.txt".

因为 PowerShell 解析器足够聪明,可以找出您的意图。

【讨论】:

猜你喜欢
  • 2015-10-07
  • 2017-04-20
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多