Reputation: 10113
I have a list of 15.000 documents where I need to remove a certain piece of text from the name.
The name structure is always: [xxx]_(xxx)_xxx.extension
I am trying to remove the (xxx)_
part of the filename. The length of xxx is variable.
I've only found ways to replace certain pieces of text or remove fixed pieces of text. Any help or guidelines are appreciated.
Upvotes: 1
Views: 2762
Reputation: 9
If you want to trim multiple file names, you can try this using PowerShell. 1. Navigate to the directory where the files to rename are. 2. Excecute the following command.
dir | Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-N) + $_.Extension}
where N in "length-N" is the number of characters you want to trim, from the last one, without counting the extension file.
Example:
I restored my files after a backup in Windows 10 and every document appeared with some extra characters, showing the day and time when I made the back up.
being FileName (2020_02_13 12_33_22 UTC).extension, I needed to trim everything between the parenthesis and take FileName.extension
So I have to trim 26 characters in every filename. Then N = 26
1) I open PowerShell and navigate to the folder where I will excecute the script. 2) I excecute the following script
dir | Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-26) + $_.Extension}
Take care using this command, because it doesn't follow any parameters except to trim a given number of characters.
Upvotes: 0
Reputation: 130819
I would use JREN.BAT - a regular expression renaming utility. JREN.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
Assuming you have JREN.BAT in a folder that resides within your PATH, then all you need is the following command from the command line:
jren "^(\[.*?\]_)\(.*?\)_" "$1"
The command looks for file names in the current directory that begin with [*]_(*)_
and removes the (*)_
portion.
Use CALL JREN
if you put the command in a batch script.
You can add the /P
option to specify a path different than the current directory, and the /S
option to iterate all sub-directories.
You can use jren /??
to access the extensive documentation. Press <Enter>
to advance the help one line at a time, or <space>
to advance one screen page at a time. Or use jren /?
to get the entire help dumped to screen immediately.
Upvotes: 2
Reputation: 56155
that's a job for for
:
for /f "tokens=1,2,* delims=_" %%a in ('dir /b "[*]_(*)_*.extension"') do @echo %%a_%%c [removed "%%b"]
How it works:
it disassembles your filenames into three parts, based on a delimiter (_
here): firstpart_secondpart_thirdpart
, then reassembling it by mounting the first and third part together.
If you want to rename them (for example), instead of just echo
ing, you can do: ren "%%a_%%b_%%c" "%%a_%%c"
Upvotes: 2