Reputation: 171
I have some troubles with Windows OneDrive and very much files on my OS renamed like from:
index.js
to:
index-DESKTOP-9T6I5F5.js
How can i remove that pattern from all files in directory with help cmd/powershell?
Upvotes: 1
Views: 1341
Reputation: 61028
Depending on what part of the filename you want to remove of course, but if I understand the question properly, you want everything after the first hyphen to be removed. You can do this like:
Get-ChildItem -Path '<root folder where the files are>' -Filter '*.js' -File -Recurse |
Rename-Item -NewName { $_.BaseName.Split("-")[0], $_.Extension } -WhatIf
Remove the -WhatIf
safety switch if you are satisfied with what is displayed in the console is correct.
Upvotes: 0
Reputation: 856
param(
$targetPath = "d:\tmp",
$pattern = "-DESKTOP-9T6I5F5"
)
Get-ChildItem $targetPath -Recurse -File | ForEach-Object {
Rename-Item $_.FullName -NewName ($_ -replace $pattern) -WhatIf
}
test it and than remove -WhatIf
Upvotes: 2