Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Using Regular Expressions with the REN commanc

Aug
47
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.
 
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.
 
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
 
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
 
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)
 
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.
 
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.
 
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.
 
@AnrDaemon, your suggestion (to avoid case sensitivity) works just fine:
Code:
[H:\]dir /b
Image1.jpg
image2.jpg
Image10.jpg
image20.jpg

[H:\]ren /n ::(?i)image(\d)\.jpg ::image0\1\.jpg
H:\Image1.jpg -> H:\image01.jpg
H:\image2.jpg -> H:\image02.jpg
     2 files would be renamed
Getting a little more complicated, this will also work (and will maintain capitalization of the filename):
Code:
[H:\]ren /n ::(?i)(image)(\d)(\.jpg) ::\{1}0\2\3
H:\Image1.jpg -> H:\Image01.jpg
H:\image2.jpg -> H:\image02.jpg
     2 files would be renamed
But, as @RogerB said, it's odd that when ignore-case is not specified:
Code:
[H:\]ren /n ::image(\d)\.jpg ::image0\1\.jpg
H:\Image1.jpg -> H:\Image1.jpg
TCC: (Sys) Cannot create a file when that file already exists.
 "H:\Image1.jpg"
H:\image2.jpg -> H:\image02.jpg
     1 file would be renamed
  • The find portion of the rename matched both files; since case insensitivity was not specified, it should not have matched Image1.jpg.
  • The replace portion of the first rename skipped the character in the replacement string following "image".
In fact, the replace portion skips even more, and simply uses the original filename:
Code:
[H:\]ren /n ::image(\d)(\.jpg) ::foogexyz\1\2
H:\Image1.jpg -> H:\Image1.jpg
TCC: (Sys) Cannot create a file when that file already exists.
 "H:\Image1.jpg"
H:\image2.jpg -> H:\foogexyz2.jpg
     1 file would be renamed
You can put almost anything you want on the right side, as long as it starts with "::" and contains a regex construct, and you'll get the same result. Sometimes, e.g., in this example (bad replacement regex), both renames trigger the weird error:
Code:
[H:\]ren /n ::image(\d)\.jpg ::x\3\4fiiet
H:\Image1.jpg -> H:\Image1.jpg
TCC: (Sys) Cannot create a file when that file already exists.
 "H:\Image1.jpg"
Invalid back-ref
H:\image2.jpg -> H:\image2.jpg
TCC: (Sys) Cannot create a file when that file already exists.
 "H:\image2.jpg"
     0 files would be renamed
 

Similar threads

Back
Top