Welcome!

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

SignUp Now!

Multi-file renaming puzzle

Feb
8
0
Hi all. I'm trying to rename a small group of files using wildcards, but I don't seem to be having much success at getting the results I need. The group of files are named:

kb67x l33brm prc base 001.kl1
kb67x l33brm prc base 002.kl1
kb67x l33brm prc base 003.kl1

They are the only files in their directory with the combination "l33brm". And I would like them to end up as:

kb67x lot33 prc base 001.kl1
kb67x lot33 prc base 002.kl1
kb67x lot33 prc base 003.kl1

I tried "ren *l33brm* *lot33*" but this merely changed all the file extensions to ".kl1lot33". Not very helpful. I'm reluctant to try too many experiments, because manually changing the filenames back after a mistake is a bit of a chore!
Can anyone suggest a method which will work? Can I just use the regular "*?" wildcards, or will it take some kind of regex wizardry to get what I want?

Thanks in advance for any help offered.
 
You've posted this in TCC/LE Support, and I'm not sure that TCC/LE supports regex. If you have a version that supports regex then this will do it:

Code:
c:\temp>dir /b
kb67x l33brm prc base 001.kl1
kb67x l33brm prc base 002.kl1
kb67x l33brm prc base 003.kl1

c:\temp>rename ::"(kb67x )l33brm(.*)" ::"\1lot33\2"
C:\temp\kb67x l33brm prc base 001.kl1 -> C:\temp\kb67x lot33 prc base 001.kl1
C:\temp\kb67x l33brm prc base 002.kl1 -> C:\temp\kb67x lot33 prc base 002.kl1
C:\temp\kb67x l33brm prc base 003.kl1 -> C:\temp\kb67x lot33 prc base 003.kl1
     3 files renamed

c:\temp>dir /b
kb67x lot33 prc base 001.kl1
kb67x lot33 prc base 002.kl1
kb67x lot33 prc base 003.kl1
 
Nice one @RogerB! Or @REPLACE[] and a loop.

Code:
v:\test> do f in "kb67x l33brm prc base *.kl1" (ren "%f" "%@replace[l33brm,lot33,%f]")

V:\test\kb67x l33brm prc base 001.kl1 -> V:\test\kb67x lot33 prc base 001.kl1
     1 file renamed
V:\test\kb67x l33brm prc base 002.kl1 -> V:\test\kb67x lot33 prc base 002.kl1
     1 file renamed
V:\test\kb67x l33brm prc base 003.kl1 -> V:\test\kb67x lot33 prc base 003.kl1
     1 file renamed
 
Thank you both for your replies and suggestions. I got some interesting results!

@RogerB according to the help file included with TCC/LE, in the section "Comparing TCC and TCC/LE", it says:

"TCC/LE does include the full command line editing, alias, filename completion, directory navigation, file selection, and regular expression support in TCC."

However, it also states that TCC/LE does not support Perl, Python, REXX, Ruby or Tcl/tk, so it's a little unclear exactly which regex syntax it does support!
When I tried your regex suggestion, I got this result:

Code:
D:\TempWork\tcc>dir /b
kb67x eagt1g prc base 001.kg1
kb67x eagt1g prc base 002.kg1
kb67x eagt1g prc base 003.kg1
kb67x eagt1g prc base 004.kg1
kb67x eagt1g prc base 005.kg1
kb67x l33brm prc base 001.kl1
kb67x l33brm prc base 002.kl1
kb67x l33brm prc base 003.kl1

D:\TempWork\tcc>rename ::"(kb67x )l33brm(.*)" ::"\1lot33\2"
D:\TempWork\tcc\kb67x l33brm prc base 001.kl1 -> D:\TempWork\tcc\lot33kb67x
D:\TempWork\tcc\kb67x l33brm prc base 002.kl1 -> D:\TempWork\tcc\lot33kb67x
TCC: (Sys) Cannot create a file when that file already exists.
 "D:\TempWork\tcc\kb67x l33brm prc base 002.kl1"
D:\TempWork\tcc\kb67x l33brm prc base 003.kl1 -> D:\TempWork\tcc\lot33kb67x
TCC: (Sys) Cannot create a file when that file already exists.
 "D:\TempWork\tcc\kb67x l33brm prc base 003.kl1"
     1 file renamed       2 failed

D:\TempWork\tcc>dir /b
lot33kb67x
kb67x eagt1g prc base 001.kg1
kb67x eagt1g prc base 002.kg1
kb67x eagt1g prc base 003.kg1
kb67x eagt1g prc base 004.kg1
kb67x eagt1g prc base 005.kg1
kb67x l33brm prc base 002.kl1
kb67x l33brm prc base 003.kl1

So it seems that there might be a problem with back references in TCC/LE.

@vefatica your solution worked perfectly! I checked in the help file that the @replace function is included in TCC/LE, and it is. So I tried it:

Code:
D:\TempWork\tcc>dir /b
kb67x eagt1g prc base 001.kg1
kb67x eagt1g prc base 002.kg1
kb67x eagt1g prc base 003.kg1
kb67x eagt1g prc base 004.kg1
kb67x eagt1g prc base 005.kg1
kb67x l33brm prc base 001.kl1
kb67x l33brm prc base 002.kl1
kb67x l33brm prc base 003.kl1

D:\TempWork\tcc>do f in "kb67x l33brm prc base *.kl1" (ren "%f" "%@replace[l33brm,lot33,%f]")
D:\TempWork\tcc\kb67x l33brm prc base 001.kl1 -> D:\TempWork\tcc\kb67x lot33 prc base 001.kl1
     1 file renamed
D:\TempWork\tcc\kb67x l33brm prc base 002.kl1 -> D:\TempWork\tcc\kb67x lot33 prc base 002.kl1
     1 file renamed
D:\TempWork\tcc\kb67x l33brm prc base 003.kl1 -> D:\TempWork\tcc\kb67x lot33 prc base 003.kl1
     1 file renamed

D:\TempWork\tcc>dir /b
kb67x eagt1g prc base 001.kg1
kb67x eagt1g prc base 002.kg1
kb67x eagt1g prc base 003.kg1
kb67x eagt1g prc base 004.kg1
kb67x eagt1g prc base 005.kg1
kb67x lot33 prc base 001.kl1
kb67x lot33 prc base 002.kl1
kb67x lot33 prc base 003.kl1

If I had a large chocolate chip cookie to hand out as a prize, it would be on its way right now, but sadly my cupboard is empty! :smile:
 
However, it also states that TCC/LE does not support Perl, Python, REXX, Ruby or Tcl/tk, so it's a little unclear exactly which regex syntax it does support!
Even without scripting languages, TCC (full version) has regex support. It's supplied by "Oniguruma" and configurable. My TCMD.INI includes the directive RegularExpressions=Perl.
 
Here's a one-liner approach:

Code:
ren *l33brm* "%%@replace[l33brm,lot33,*]"

help delayedvariableexpansion for more info on this trick.
 
I'm reluctant to try too many experiments, because manually changing the filenames back after a mistake is a bit of a chore!
Take note of the rename command's /N option, which allows you to see what would happen if the command ran without that option. It can save a lot of trouble when you're not sure that you have the right command. (I/m assuming the TCC/LE has that option.)
 

Similar threads

Back
Top