Using Regular Expressions with the REN commanc

Aug 16, 2016
31
2
I have a folder containing files generated by screen captures. The files are named ImageN.jpg and imageNN.jpg, where N is of course a digit. I want to rename all the ImageN.jpg files to Image0N.jpg and leave the ImageNN.jpg files alone. I have tried and tried to do this with no success. However I structure the regexes, I either get no rename or I get all the files renamed. Here are examples.

Code:
> ren "::image(\d).jpg" ::"image0\1.jpg"
Image1.jpg -> Image1.jpg
Image2.jpg -> Image2.jpg
Image3.jpg -> Image3.jpg
Image5.jpg -> Image5.jpg
Image7.jpg -> Image7.jpg
Image8.jpg -> Image8.jpg
Image9.jpg -> Image9.jpg
     7 files renamed
    
> ren ::image(\d{1}).jpg ::image0\1.jpg
Image1.jpg -> Image1.jpg
Image2.jpg -> Image2.jpg
Image3.jpg -> Image3.jpg
Image5.jpg -> Image5.jpg
Image7.jpg -> Image7.jpg
Image8.jpg -> Image8.jpg
Image9.jpg -> Image9.jpg
     7 files renamed
    
> ren "::^(.*)(\d{1})\.jpg" ::"\{1}0\2\.jpg"
Image1.jpg -> Image01.jpg
Image10.jpg -> Image100.jpg
Image11.jpg -> Image101.jpg
Image12.jpg -> Image102.jpg
Image13.jpg -> Image103.jpg
Image2.jpg -> Image02.jpg
Image3.jpg -> Image03.jpg
Image5.jpg -> Image05.jpg
Image7.jpg -> Image07.jpg
Image8.jpg -> Image08.jpg
Image9.jpg -> Image09.jpg
    11 files renamed

Note that if I use the first regex in a dir command, I get only the 1-digit files:

Code:
> dir/b ::image(\d).jpg
Image9.jpg
Image8.jpg
Image7.jpg
Image5.jpg
Image1.jpg
Image2.jpg
Image3.jpg

If someone could please tell me what I'm doing wrong I would greatly appreciate it. It's probably something simple, but I just can't see it. Thank you.
 
Aug 16, 2016
31
2
By the way, the following accomplishes my goal, but I would still like to know how to do this with regular expressions.
Code:
for %k in (image?.jpg) ren %k image0%@instr[5,1,%%k].jpg
Thank you again.
 
Apr 18, 2014
279
9
If you take the quotes out of your first example I think it does what you want:

Code:
d:\test>dir /b
image1.jpg
image2.jpg
image12.jpg

d:\test>rename ::image(\d)\.jpg ::image0\1.jpg
D:\test\image1.jpg -> D:\test\image01.jpg
D:\test\image2.jpg -> D:\test\image02.jpg
     2 files renamed

d:\test>dir /b
image01.jpg
image02.jpg
image12.jpg

Edited to add: I've looked again at your first example and it's not quite the same as mine, you need to escape the "." before "jpg" or it is interpreted as "any character" by the regex engine. So the regex to identify the files you want is ::image(\d)\.jpg
 
Aug 16, 2016
31
2
Thank you for your response, Roger, but I tried that and it simply doesn't work for me. Please see the following capture. TCC says that a rename occurred, but it didn't. It just renamed the file to the same name it had previously, without the added 0.

As you can see, I am running version 27.00.23, which I believe is the latest version.
Code:
> ver


TCC  27.01.23 x64   Windows 10 [Version 10.0.18363.1500]

> dir/b/o
Image1.jpg
Image3.jpg
Image5.jpg
Image7.jpg
Image10.jpg
Image11.jpg
Image12.jpg
Image13.jpg

> rename ::image(\d)\.jpg ::image0\1.jpg
Image1.jpg -> Image1.jpg
Image3.jpg -> Image3.jpg
Image5.jpg -> Image5.jpg
Image7.jpg -> Image7.jpg
     4 files renamed

> dir/b/o
Image1.jpg
Image3.jpg
Image5.jpg
Image7.jpg
Image10.jpg
Image11.jpg
Image12.jpg
Image13.jpg
 
Apr 18, 2014
279
9
There's a case sensitivity problem now Jesse, sorry that's my fault, I wasn't paying close enough attention to your original post. The regular expression is case sensitive, so try

Code:
rename ::Image(\d)\.jpg ::Image0\1.jpg

It's odd though that the lower case version seems to be matching some files (which it shouldn't) and then not renaming them (which is correct as they don't match the regex)
 
Aug 16, 2016
31
2
OMG. I feel like an idiot. How embarrassing. Yes, of course, regexes are case-sensitive. Sigh.

But in my defense, I can only plead that I didn't notice this because, as you wrote, "It's odd though that the lower case version seems to be matching some files (which it shouldn't) and then not renaming them."

Thank you for your help, Roger. It is very much appreciated.
 
May 20, 2008
11,185
94
Syracuse, NY, USA
Note that if I use the first regex in a dir command, I get only the 1-digit files:
"\d" means a single digit. You could try "\d+" (one or more digits) but you might wind up with files renamed more than once and even if not, 1-digit numbers, 2-digit numbers (...) will all get a single '0' prepended.

It's too bad you can't put "\1" inside a variable function; then you could compute the number of 0s (using @len[\1]) needed and use @repeat in the new name.
 
Aug 16, 2016
31
2
Thanks for your reply, Vince, but the \d wasn't the issue. The problem was case-sensitivity. However, there appears to be a bug in that the first regex is matching both "Image" and "image" yet the rename isn't occurring.

You are correct, BTW, that when I used \d+ all digits were prepended with 0. That certainly wasn't what I wanted.

See RogerB's post above time stamped 5:10 yesterday for the key to the solution to my problem.