[英]Rename all files on directory with pattern [Powershell]
我在使用 Windows OneDrive 時遇到了一些問題,我的操作系統上的很多文件都重命名為:
index.js
至:
index-DESKTOP-9T6I5F5.js
如何使用幫助 cmd/powershell 從目錄中的所有文件中刪除該模式?
param(
$targetPath = "d:\tmp",
$pattern = "-DESKTOP-9T6I5F5"
)
Get-ChildItem $targetPath -Recurse -File | ForEach-Object {
Rename-Item $_.FullName -NewName ($_ -replace $pattern) -WhatIf
}
測試它,然后刪除 -WhatIf
當然,這取決於您要刪除的文件名的哪一部分,但是如果我正確理解了這個問題,您希望刪除第一個連字符之后的所有內容。 你可以這樣做:
Get-ChildItem -Path '<root folder where the files are>' -Filter '*.js' -File -Recurse |
Rename-Item -NewName { $_.BaseName.Split("-")[0], $_.Extension } -WhatIf
如果您對控制台中顯示的內容正確感到滿意,請移除-WhatIf
安全開關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.