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
299
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
299
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,411
99
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.
 
Similar threads
Thread starter Title Forum Replies Date
T DEL disregards switch when using regular expressions Support 2
R WAD Unusable state when using Chinese characters Support 3
Joe Caverly Using TYPE with non-English text Support 22
L Using TCC.exe through an SSH connection Support 3
Joe Caverly Using @PSHELL from @EVAL Support 8
Jay Sage Command Will Not Run Using Short Name of Path Support 5
Jay Sage Cannot Postion Cursor in Command Line Using Mouse Support 7
MikeBaas Using the debugger Support 2
Joe Caverly Multiple Text Searches at once using FFIND or TPIPE Support 4
Dick Johnson Using the @instr function Support 5
Joe Caverly Using this CMD technique from TCC Support 17
Joe Caverly What version and Windows OS are you using? Support 3
Craig Fitzgerald Problems using tcstart.btm Support 3
R How to? Append files in multiple subfolders using copy? Support 8
M Ctrl-C when using command line history does nothing Support 2
Joe Caverly Using a Directory Alias with @iniwrite fails Support 14
C French accents using msgbox Support 4
C "Failed to update the system registry. Please try using REGEDIT" Support 3
x13 Problem listing repository files using DIR http(s)://... Support 8
R Regex using ^ Support 2
cxxl WAD Mouse movement sluggish when using TCC list Support 3
jfalch WAD internal "which" should check for FILEEXIST when using AppPaths entry Support 8
D How to? Scale font in TCC using wheel mouse or other means Support 2
fpefpe Documentation copy/move using /c or /u Support 2
gschizas Fixed Using codepage 65001 (UTF-8) breaks non-ASCII characters Support 8
D How to? Use typed envars using regex. Support 3
epement Using "everything" Support 10
U Installing the Compaq Visual Fortran 6.0 compiler using Take Command Support 8
Alexander WAD The number of files and dirs are multiplied when using multiple wildcards Support 6
T using bottom line for status bar Support 3
A How to? How do you launch TCC/LE using the already-open instance? Support 5
rps Fixed Using last argument variable ! Support 5
M Incorrect ARGV1 when using linux-style paths Support 4
D How to? Using a custom ini file with TCC from Take Command Support 7
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
D Documentation Using "send all input to other tabs" Support 11
Joe Caverly Word Count using TPIPE Support 10
Phileosophos Fixed Option Dialog Hangs when Using Help Support 7
MickeyF problem using COM object in VBScript from v16 x64 TCC but not from v15 32-bit TCC Support 4
S How to? How do I distinguish missing source file from download error when using HTTP? Support 6
Jay Sage Using EVERYTHING Command in TCMD15 Support 5
Charles Dye Odd behavior after using OPTION "Advanced" tab Support 2
T How to? Using FOLDERMONITOR 1 event causes multiple trigger(ing)s Support 8
samintz WAD FILEWRITE using SMOPEN Support 1
ehab aboudaya How to? 2 GUI questions and return HWND using TakeCommandIPC Support 3
R Fixed Using %~dp1 for paths with unavailable drives Support 6
C Using dif. sending addresses with sendmail Support 11
R TC 13 / Using TCDIALOG to exit commands Support 4
epement Video tutorials of using Take Command Support 2
electrotype Error using TCC: "Label not found" Support 6

Similar threads