Renaming files with regex.

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.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,462
88
Albuquerque, NM
prospero.unm.edu
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....)
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
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.
 
May 20, 2008
11,402
99
Syracuse, NY, USA
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
 
May 20, 2008
3,515
4
Elkridge, MD, USA
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
Thread starter Title Forum Replies Date
M Another problem with renaming files... Support 3
gunkelkarl renaming files Support 14
JohnQSmith Regex renaming Support 2
P Renaming with a RegEx Support 1
J Extended directory search - cure by renaming tcmd.ini Support 10
nickles Renaming bug? Support 9
S GPF after successfully renaming directories on an ftp server Support 2
M More file-renaming problems involving commas... Support 0
MickeyF TCC crashing when copying multiple files (now resolved) Support 6
fpefpe How to? shortcuts and batch files Support 4
C v28 key files Support 2
J ffind does not find files Support 4
C duplicate files in multiple plugin sites Support 9
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
S How to? Deinstalling TC/reinstating execution of bat files by cmd in Win7 Support 31
vefatica FFIND, temp files? ... not deleted? Support 0
R FUNCTION to count NUMBER of files matching a pattern and specific length/not length Support 4
vefatica Tmp files from in-process pipes not deleted Support 6
vefatica Finding files with alternate data streams? Support 4
rps Multi-column DIR /v not displaying all files. Support 5
D How to skip files in a fileset? Support 6
vefatica PIPEVIEW leaves TMP files Support 18
K "copy /z /w" command not deleting read-only files in destination Support 10
Jesse Heines "Taken" date for JPG and PNG files Support 4
O I cannot use links to .btm files Support 5
C How to? rename files with spaces Support 8
R How to? Append files in multiple subfolders using copy? Support 8
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
vefatica Find Files/Text dialog: does it work? Support 0
Joe Caverly UNZIPping older .ZIP files Support 3
C Documentation default location for LOG files Support 21
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
M ANSI is used to show files instead of OEM in version 17 to 19 ! Support 5
C 7Z files on forum? Support 0
x13 Problem listing repository files using DIR http(s)://... Support 8
B Can copy command concatenate multiple mp3 files Support 2
redwdc Copying files with spaces, commas, etc. Support 5
J New lexer for TCC batch files. Support 0
M How to? want to download a file from a website enumarate the links and download the files Support 2
keithg1964 TCC RT Install Files Support 1
P Fixed Rexx .cmd files no longer work in TCC 20 Support 3
thorntonpg Handling files with embedded spaces Support 1
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2
P Files older - exist Support 1
D safe delete files command Support 10
P Copying files in subdirectories Support 9
C WAD VIEW print multiple files - switches fail Support 10
MikeBaas How to? SELECT: exclude files / display prompt Support 2

Similar threads