如何從檔案名稱中刪除 (2020_11_23 09_41_34 UTC)

如何從檔案名稱中刪除 (2020_11_23 09_41_34 UTC)

我嘗試以管理員身份使用 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 以使其完成任務並序列化變更。

相關內容