gpt4 book ai didi

windows - 替换 Windows 中整个子文件夹中文件名中的所有 #

转载 作者:可可西里 更新时间:2023-11-01 09:59:00 25 4
gpt4 key购买 nike

将大约 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. ' }

我不太幸运地弄清楚保留字符的语法 - 我尝试了 \##\'*#*',运气不好。

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

谢谢。

最佳答案

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 解析器足够智能,可以弄清楚您的意图。

关于windows - 替换 Windows 中整个子文件夹中文件名中的所有 #,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com