我尝试以管理员身份使用 Powershell 执行以下命令:
Get-ChildItem -Recurse | Where-Object { $_.Name -match " ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)" } | Rename-Item -NewName { $_.Name -replace " ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)", ""}
我收到很多错误:
Get-ChildItem : L'accès au chemin d'accès 'C:\Windows\system32\LogFiles\WMI\RtBackup' est refusé.
Au caractère Ligne:1 : 1
+ Get-ChildItem -Recurse | Where-Object { $_.Name -match " ?\(\d\d\d\d_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\syst...es\WMI\RtBackup:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
我必须做什么?
答案1
除非该位置受到仅限管理员访问的保护,或者您创建了文件或具有执行所做操作的正确权限,否则不需要管理员帐户。
永远不要在未先检查结果的情况下执行破坏性代码(创建/修改/更新/删除/移动/重命名)。你会发现自己处于非常不利的境地。 因此,在所有情况下都应使用 -WhatIf 或 Confirm。
Powershell 最佳实践#8:使用 WhatIf 并确认
Rename-Item -NewName ($_.Name -replace " ?\(\d\d\d\d_\d\d_\d\d \d\d_\d\d_\d\d UTC\)", "") -WhatIf
至于错误,你把这个问题复杂化了。可以简化为:
# Test create some test files
0..2 |
ForEach-Object {New-Item -Path "D:\Temp\SomeFileName$PSItem(2020_11_23 09_41_34 UTC).txt" -WhatIf}
# Results
<#
What if: Performing the operation "Create File" on target "Destination: D:\Temp\SomeFileName0(2020_11_23 09_41_34 UTC).txt".
What if: Performing the operation "Create File" on target "Destination: D:\Temp\SomeFileName1(2020_11_23 09_41_34 UTC).txt".
What if: Performing the operation "Create File" on target "Destination: D:\Temp\SomeFileName2(2020_11_23 09_41_34 UTC).txt".
#>
# Create the test files
0..2 |
ForEach-Object {New-Item -Path "D:\Temp\SomeFileName$PSItem(2020_11_23 09_41_34 UTC).txt"}
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 01-Dec-20 13:11 0 SomeFileName0(2020_11_23 09_41_34 UTC).txt
-a---- 01-Dec-20 13:11 0 SomeFileName1(2020_11_23 09_41_34 UTC).txt
-a---- 01-Dec-20 13:11 0 SomeFileName2(2020_11_23 09_41_34 UTC).txt
#>
# Get files in path
(Get-ChildItem -Path 'D:\Temp' -Filter '*(2020_11_23 09_41_34 UTC)*').FullName
# Results
<#
D:\Temp\SomeFileName0(2020_11_23 09_41_34 UTC).txt
D:\Temp\SomeFileName1(2020_11_23 09_41_34 UTC).txt
D:\Temp\SomeFileName2(2020_11_23 09_41_34 UTC).txt
#>
# Delete the parens and all text between them
(Get-ChildItem -Path 'D:\Temp' -Filter '*(*').FullName |
ForEach-Object {Rename-Item -Path $PSItem -NewName $($PSItem -replace '\(.*\)','') -WhatIf}
# Results
<#
What if: Performing the operation "Rename File" on target "Item: D:\Temp\SomeFileName0(2020_11_23 09_41_34 UTC).txt Destination: D:\Temp\SomeFileName0.txt".
What if: Performing the operation "Rename File" on target "Item: D:\Temp\SomeFileName1(2020_11_23 09_41_34 UTC).txt Destination: D:\Temp\SomeFileName1.txt".
What if: Performing the operation "Rename File" on target "Item: D:\Temp\SomeFileName2(2020_11_23 09_41_34 UTC).txt Destination: D:\Temp\SomeFileName2.txt".
#>
如果您确实需要日期字符串的正则表达式,那么您可以这样做:
(Get-ChildItem -Path 'D:\Temp' -Filter '*(*').FullName |
ForEach-Object {Rename-Item -Path $PSItem -NewName $($PSItem -replace '\(\d{4}|\d{2}.|_|UTC\)','') -WhatIf}
删除 -WhatIf 以完成任务并序列化更改。