Is it possible to use multiple delims based on character position rather than character value in Windows 10 Command Prompt to rename files?

The problem and question in a nutshell before the verbose details of my quest below:

In command prompt, I need a single ren command to rename all files in the following format:vfx_444xx.png where xx is a two-digit number in order from 00 to 93

To this result: Target format: vfx_444_xx.png

The change being simply to add an underscore before the two numbers matched by "xx" above.

Here is the complete list of Google, windowscentral.com, and SE searches that I did: [NOTE: I'm sorry SE is not observing the format of these entries. They are each on a separate line. Since this is more evidence that I did the most research possible before coming here instead of contributing info, ok.]

5:31 PM Where to ask DOS operating system question about the command prompt?
- Meta Stack Exchange 5:31 PM stackexchange dos - Google Search
5:29 PM dos rename "insert character" -unix -sed - Google Search
5:29 PM dos rename "insert character" - Google Search
5:27 PM "command prompt" rename files "insert character" - Google Search
5:26 PM "windows 10" "command prompt" rename files "insert character"- Google Search
5:26 PM "windows 10" "command prompt" rename files "insert character" - Google Search

5:25 PM "windows 10" "command prompt" rename multiple files "insert character" - Google Search 5:24 PM dos rename multiple files "insert character" - Google Search 5:08 PM How to batch rename multiple files on Windows 10 | Windows Central 5:08 PM How to batch rename multiple files on Windows 10 | Windows Central 5:07 PM How to batch rename multiple files on Windows 10 | Windows Central 4:58 PM How to batch rename multiple files on Windows 10 | Windows Central 4:47 PM dos rename multiple files - Google Search

I read all the SE posting recommended by commenters on an unrelated DOS question: "How does the Windows RENAME command interpret wildcards?"

Lots of information but I could not find any guidance on how to insert a character(s) in the target Mask that is/are not in the source Mask.

Below are the various terms I tried in the SuperUser Search field: windows rename insert character in target mask windows rename insert character windows rename insert windows rename target insert

Again, this is the source format:vfx_444xx.png where xx is a two-digit number in order from 00 to 93target format: vfx_444_xx.png

I decided to try some commands on a temp directory of files:ren vfx_44400.png ???????_??.png --> vfx_444_0.png
This failed because the underscore in target picked up the first 0 in the source

ren vfx_444*.png vfx_444_*.png ---> first ten files were renamed the same as the attempt above, and then threw out an error that file was not found for the remaining files.

Next I did some reading about batch commands. I found a person who needed something close to what I need (Command Prompt being Illogical? Want to add numbers using Rename)

for /f "tokens=1,2 delims=_" %a in ('dir /b img_*.jpg') do ren "%a_%b" "%a_1%b"

As you can tell, this inserts a 1 after the underscore. I need to define two different delimiters both based on character positions, not a specific character. Something like:

for /f "tokens=1,2,3 delims=(substring{0,7), substring(7))" %a in .....

I suspect "substring" would have to be coded as a subroutine in the batch file = writing code. Ugh.

At this point (4 hours) I am just mentally exhausted by the whole thing and could really use some outsides help from you geniuses to get me pointed in the right direction. Thanks in advance for reading my trek and maybe a solution you can offer.

4 Answers

You can use powershell for easy bulk rename tasks:

$i = 0; Get-ChildItem "Filepath" | Foreach { $i++; rename-item -Path $_.Fullname -newname { $_.basename.substring(0,$_.name.length-2) + "_$($i)" + $_.extension}}

After 4 more hours, I found it. First run:

cmd /v

Then, inside this terminal, go to your folder and run:

@for %i in (vfx_444*.png) do @(set "line=%i" && echo !line:~0,-6!_!line:~-6,7!)

You could call cmd /v inside the loop, in this way:

@for %i in (vfx_444*.png) do @cmd /v /c "set line=%i && echo !line:~0,-7!_!line:~-7,7!"

I haven't tested this in Win10, though.

0
for /l %i in (44400 1 44493)do cmd/v/c "set "_xx=%i" & ren vfx_%i.png vfx_444_!_xx:~-2!.png"

enter image description here

You do not need for /f with multiple delimiters, just numbers in the range 44400-44493, so use a for /l that gives you the current number in the loop and define a substring with the last 02 digits preceded with _ to compose the destination name for rename command:

 Numbers in loop: %i == 44400-44493Orinal name in loop: vfx_%i.png == vfx_44400.png - vfx_44493.png
Target name in loop: vfx_444_!_xx:~-2!.png == vfx_444_00.png - vfx_444_93.png

[√] For Loop

[√] For /L Loop

[√] For /F Loop

[√] Strings Manipulation (Substrings)

Another PowerShell method. There is no reason to use batch with Windows 10.

Verbose:

Get-ChildItem "vfx_444??.png" | Rename-Item -NewName { $_.Name -replace 'vfx_444', 'vfx_444_' }

KeyBanger:

gci "vfx_444??.png" | ren -New { $_.Name -replace 'vfx_444', 'vfx_444_' }

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like