Windows bulk rename middle filename via CLI?
This PowerShell one liner will expand all numbers in the file name to two places .PadLeft(2,'0')
:
(and leave numbers with more places untouched)
Get-ChildItem *[0-9]*.txt|Rename-Item -NewName {([regex]::Replace($_.BaseName,'\d+',{$args[0].Value.PadLeft(2,'0')}))+$_.Extension}
to be on topic, wrapped in a cmd line/batch file:
Powershell -Nop -C "Get-ChildItem *[0-9]*.txt|Rename-Item -NewName {([regex]::Replace($_.BaseName,'\d+',{$args[0].Value.PadLeft(2,'0')}))+$_.Extension}"
To be on the safe side before executing the commands, append either
-Confirm
which asks before doing a rename-WhatIf
which lists all renames it would execute without the parameter
just in front of the last "
Would it be possible to bulk rename it with Windows ren or rename tool?
Yes, but it requires a batch file.
test.cmd:
@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2,3,4" %%i in ('dir /b *Example.txt') do (
rem pad 2nd token with leading zero
set _num=0%%j
set _num=!_num:~-2!
ren "%%i %%j %%k %%l" "%%i !_num! %%k %%l"
)
endlocal
example:
> dir *Example.txt
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test\test
03/01/2019 11:30 0 File 15 - Example.txt
03/01/2019 11:30 0 File 2 - Example.txt
03/01/2019 11:30 0 File 22 - Example.txt
03/01/2019 11:30 0 File 3 - Example.txt
03/01/2019 11:30 0 File 4 - Example.txt
03/01/2019 11:30 0 File 5 - Example.txt
6 File(s) 0 bytes
0 Dir(s) 1,075,134,230,528 bytes free
> ..\test
> dir
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test\test
03/01/2019 11:54 <DIR> .
03/01/2019 11:54 <DIR> ..
03/01/2019 11:30 0 File 02 - Example.txt
03/01/2019 11:30 0 File 03 - Example.txt
03/01/2019 11:30 0 File 04 - Example.txt
03/01/2019 11:30 0 File 05 - Example.txt
03/01/2019 11:30 0 File 15 - Example.txt
03/01/2019 11:30 0 File 22 - Example.txt
6 File(s) 0 bytes
2 Dir(s) 1,075,134,230,528 bytes free
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- Dir - list files and folders - Windows CMD - SS64.com
- EnableDelayedExpansion - Windows CMD - SS64.com
- For - Loop through command output - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
Edit: I've just noticed this question is specifically about renaming from the command line, so it does not answer the question directly... I'll keep it for now, hoping it will be useful for others.
The Total Commander file manager has an excellent bulk rename tool. It includes many features, including rename preview, different rename masks, regular expressions, renaming files in entire folder hierarcy, and much more. At the same time, it is quite easy to use.
Here's a screenshot for demonstrating its usage:
Step by step:
- Download and run Total Commander.
- Nativate to the folder with the files to rename.
- Mark the files to rename:
- Option 1 - Ctrl + A for marking all files in the folder.
- Option 2 - Mark files one by one, using the Space key or mouse right click.
- Option 3 - Open "Find Files" (Alt + F7), type
*.txt
in the "Search for" box, click "Start Search", press "Feed to listbox", then mark the files with Ctrl + A. Use this technique if you want to rename files also in inner folders.
- Press Ctrl + M to open the Multi-Rename tool.
- Set the desired values in "Search for" and "Replace with" boxes. If using a regular expression, check the RegEx box.
- Hit "Start!".