How can I batch rename windows files where the % is a delimiter?
If you're on Windows 10, you should be learning & using Powershell for administration & automation. It's very powerful and a lot of fun.
With your sample names and desired result, I opted to split on '%5C' and use an index of -1, which gets the last element in an array regarless of length.
Verbose:
$path = 'c:\temp'
Get-ChildItem $path | Rename-Item -NewName { ($_ -split '%5C')[-1] }
At the console:
gci c:\temp | ren -NewName { ($_ -split '%5C')[-1] }
Get-ChildItem
Rename-Item
about split
String manipuation demo:
PS C:\...\keith>$oldnames
114902_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489879.pdf
114904_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1489880.pdf
114906_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491058.pdf
114916_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491059.pdf
114918_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491452.pdf
114920_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491453.pdf
114923_T%3A%5CAccounting%5CPayables%5CAPA%5CBoshart Apr 25%5CBII_inv_1491454.pdf
115513_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039292647.pdf
115515_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039295860.pdf
115517_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039296218.pdf
115523_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308592.pdf
115525_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039308593.pdf
120342_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039328905.pdf
120345_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337204.pdf
120348_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337221.pdf
120351_T%3A%5CAccounting%5CPayables%5CAPA%5CABB MAY 15%5C0039337461.pdf
120639_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168731.pdf
120640_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168988.pdf
120642_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63168993.pdf
120644_T%3A%5CAccounting%5CPayables%5CAPA%5CAFA MAY 15%5CINV#63169027.pdf
PS C:\...\keith>$oldnames | ForEach{ ($_ -split '%5C')[-1] }
BII_inv_1489879.pdf
BII_inv_1489880.pdf
BII_inv_1491058.pdf
BII_inv_1491059.pdf
BII_inv_1491452.pdf
BII_inv_1491453.pdf
BII_inv_1491454.pdf
0039292647.pdf
0039295860.pdf
0039296218.pdf
0039308592.pdf
0039308593.pdf
0039328905.pdf
0039337204.pdf
0039337221.pdf
0039337461.pdf
INV#63168731.pdf
INV#63168988.pdf
INV#63168993.pdf
INV#63169027.pdf
I would write a PowerShell script that iterates over the file names and rewrites them with the following:
$dir = Get-ChildItem -Path "PUTFULLPATHHERETODIRECTORY"
foreach($P in $dir) {
Rename-Item -path $P -newname $P.FullName.Substring($P.FullName.lastindexof("%5C")+3)
}
First, we get the contents of the target folder. Then, for each item in the directory, rename it to the name starting at the last index of the "%5C" pattern and add 3 spaces to account for removing it.
Of course, put the full path where I put PUTFULLPATHHERETODIRECTORY.
Use *5c
as a delimiter and obtain target name substrings by the set
command, and this will get the last part of the file name without worrying if it is token 5, 6, 7, etc, because that always get the strings after the last delimiter...
Split string into substrings based on delimiter by Sponge Belly / DosTips.com
set "_trick=%_name:*5c=" & set "_name=%"
@echo off && setlocal enabledelayedexpansion
for %%i in (*.pdf)do call :_ren_: "%%~ni" && ren "%%~fi" "!_name!%%~xi"
:_ren_:
set "_name=%~1" <nul || endlocal && goto :EOF
set "_trick_=%_name:*5c=" & set "_name=%" && exit /b
- The same in Powershell
gci C:\temp\*.pdf|Ren -New {$_ -replace '.*%5C',''}