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的解析器是足夠聰明,找出你的意圖。
如果您在標籤或標題中指定工作環境,可能會得到更多答案 – 4rlekin 2014-10-30 08:24:55