Welcome!

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

SignUp Now!

Renaming files with regex.

Jul
33
0
Hi,
I need to rename about 11,000 files. All of them have this format:
er13se01.116
I need to change the dot to a hyphen, then put an extension of .eps on it. So, it should look like this:
er13se01-116.eps
I can't do with a wildcard rename. So, in 4nt and TC, I've tried this:
ren :: (.*)\.([0-9]{3}) \1-\2\.eps
Doesn't work. Can someone help me?
Thanks,
Peter

Please note: I had to put a space after the :: above, because, this forum automatically creates a smiley face when you type a colon followed immediately by an open paren. Drove me nuts.
 
Hi,
I need to rename about 11,000 files. All of them have this format:
er13se01.116
I need to change the dot to a hyphen, then put an extension of .eps on it. So, it should look like this:
er13se01-116.eps
I can't do with a wildcard rename. So, in 4nt and TC, I've tried this:
ren :: (.*)\.([0-9]{3}) \1-\2\.eps
Doesn't work. Can someone help me?

Are you renaming all the files in the directory? You may not need regexps at all; delayed expansion may be adequate:

Code:
ren /[!*.eps] /n * "%%@replace[.,-,*].eps"

(The exclusion range is to prevent any file from being renamed twice....)
 
I would use delayed variable expansion.

function newname=`%@name[%$]-%@ext[%$].eps`
ren * %%@newname
[*]

I had to use a user function to get that to work. I couldn't figure out how to get it to work otherwise.

i.e. This did NOT work:
ren * %%@name
[*]-%%@ext
[*].eps

-Scott
 
Are you renaming all the files in the directory? You may not need regexps at all; delayed expansion may be adequate:

Code:
ren /[!*.eps] /n * "%%@replace[.,-,*].eps"
(The exclusion range is to prevent any file from being renamed twice....)

Thanks. I'll need to look closely at your code. I don't recognize a lot of stuff in there. I'll try it, though.
 
I would use delayed variable expansion.

function newname=`%@name[%$]-%@ext[%$].eps`
ren * %%@newname
[*]

I had to use a user function to get that to work. I couldn't figure out how to get it to work otherwise.

i.e. This did NOT work:
ren * %%@name
[*]-%%@ext
[*].eps

-Scott

Thank you, Scott. Never done a "function" like that before. Pretty cool. I'll try it.
 
Hi,
I need to rename about 11,000 files. All of them have this format:
er13se01.116
I need to change the dot to a hyphen, then put an extension of .eps on it. So, it should look like this:
er13se01-116.eps
I can't do with a wildcard rename. So, in 4nt and TC, I've tried this:
ren :: (.*)\.([0-9]{3}) \1-\2\.eps
Doesn't work. Can someone help me?

That's a good idea for v11 ... regex matching in REN. Also good would be regexes in FOR's "set"

@XREPLACE in my 4UTILS plugin could help here:

Code:
v:\> dir /k /m [a-z][a-z][0-9][0-9]*.*
2009-04-22  20:09               0  ab23cd45.678
2009-04-22  20:09               0  ef34gh56.789

v:\> for %f in (*) if %@regex[.*\.[0-9]{3},%f] gt 0 ren %f
%@xreplace[(.*)\.([0-9]{3}),\1-\2.eps,%f]
V:\ab23cd45.678 -> V:\ab23cd45-678.eps
     1 file renamed
V:\ef34gh56.789 -> V:\ef34gh56-789.eps
     1 file renamed
 
vefatica wrote:
| ---Quote (Originally by pb4072)---
| Hi,
| I need to rename about 11,000 files. All of them have this format:
| er13se01.116
| I need to change the dot to a hyphen, then put an extension of .eps
| on it. So, it should look like this: er13se01-116.eps
| I can't do with a wildcard rename. So, in 4nt and TC, I've tried this:
| ren :: (.*)\.([0-9]{3}) \1-\2\.eps
| Doesn't work. Can someone help me?
| ---End Quote---
| That's a good idea for v11 ... regex matching in REN. Also good
| would be regexes in FOR's "set"
|
| @XREPLACE in my 4UTILS plugin could help here:
|
|
| Code:
| ---------
| v:\> dir /k /m [a-z][a-z][0-9][0-9]*.*
| 2009-04-22 20:09 0 ab23cd45.678
| 2009-04-22 20:09 0 ef34gh56.789
|
| v:\> for %f in (*) if %@regex[.*\.[0-9]{3},%f] gt 0 ren %f
| %@xreplace[(.*)\.([0-9]{3}),\1-\2.eps,%f]
| V:\ab23cd45.678 -> V:\ab23cd45-678.eps
| 1 file renamed
| V:\ef34gh56.789 -> V:\ef34gh56-789.eps
| 1 file renamed

Twice this morning I tried to post the response below to the OP, but was
rejected both times by the forum "thread is not currently available to be
posted to":

| I'd do something like this (UNTESTED!):
| for %x in (*) ren %x %@replace[.,-,%x].eps
| where you can replace the * with a filter if necessary.
|
| BTW, I avoid regular expressions when simpler methods suffice.

Charles' warning to exclude the newly renamed files would make my
suggestion:
for /[!*.eps] %x in (*) ren %x %@replace[.,-,%x].eps
which I think is much simpler than your regex-based suggestion.
--
Steve
 

Similar threads

Back
Top