在Windows的整个子文件夹中替换文件名中的所有# [英] Replacing all # in filenames throughout subfolders in Windows
问题描述
将大约200,000个文件迁移到OneDrive进行商业活动,发现其中有一些他们不喜欢的字符-最大的冒犯者是#
.我有大约3,000个带有散列的文件,我想仅用No.
替换它们.例如,旧:File#3.txt
新:File No.3.txt
Migrating about 200,000 files to OneDrive for business, and discovered that there are a few characters they don't like - the biggest offender is #
. I have about 3,000 files with a hash, and I'd like to replace them all with just No.
. For example, old: File#3.txt
new: File No.3.txt
我尝试使用PowerShell脚本,但是它也不喜欢#
:
I tried using a PowerShell script, but it doesn't like #
either:
Get-ChildItem -Filter "*#*" -Recurse |
Rename-Item -NewName { $_.name -replace '#',' No. ' }
我不太了解保留字符的语法-我尝试了\#
,#\
,'*#*'
,没有运气.
I'm not having much luck figuring out the syntax for reserved characters - I tried \#
, #\
, '*#*'
, no luck.
任何人都可以阐明这一点或提供一种快速方法来递归替换所有这些哈希标签吗?
Can anyone shed light on this or provide a quick method to recursively replace all of these hash tags?
谢谢.
推荐答案
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使用反引号(`)作为转义字符,并用双引号评估内容:
PowerShell uses the Backtick (`) as an escape character, and double quotes to evaluate content:
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解析器足够聪明,可以弄清楚您的意图.
Because the PowerShell parser is smart enough to figure out your intent.
这篇关于在Windows的整个子文件夹中替换文件名中的所有#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!