-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemovePrefix&Suffix.ps1
36 lines (32 loc) · 1 KB
/
RemovePrefix&Suffix.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$Folder = 'C:\Example'
$Prefix = 'ABC'
$PrfLen = $Prefix.Length
$Suffix = '123'
$SufLen = $Suffix.Length
Clear-Host
Function FilePrefixLoop {
$PrefixFiles = Get-ChildItem -Path $Folder -Recurse | Where-Object {$_.BaseName.StartsWith("$Prefix")}
Write-Host "Changed Prefix:"
Write-Host ""
$PrefixFiles| ForEach-Object {
$RF = $_.BaseName[($PrfLen)..$_.BaseName.Length] -join ""
Write-Host $_.BaseName -> $RF
Rename-Item -Path $_.FullName -NewName ($RF + $_.Extension)
Write-Host
}
Write-Host ""
}
Function FileSuffixLoop {
$SuffixFiles = Get-ChildItem -Path $Folder -Recurse | Where-Object {$_.BaseName.EndsWith("$Suffix")}
Write-Host "Changed Suffix:"
Write-Host ""
$SuffixFiles| ForEach-Object {
$Diff = $_.BaseName.Length - $SufLen - 1
$RF = $_.BaseName[0..$Diff] -join ""
Write-Host $_.BaseName -> $RF
Rename-Item -Path $_.FullName -NewName ($RF + $_.Extension)
}
Write-Host ""
}
FilePrefixLoop
FileSuffixLoop