1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Renaming files with regex.

Discussion in 'Support' started by pb4072, Apr 22, 2009.

  1. pb4072

    Joined:
    Jul 25, 2008
    Messages:
    33
    Likes Received:
    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.
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    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....)
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,190
    Likes Received:
    11
    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
     
  4. pb4072

    Joined:
    Jul 25, 2008
    Messages:
    33
    Likes Received:
    0
    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.
     
  5. pb4072

    Joined:
    Jul 25, 2008
    Messages:
    33
    Likes Received:
    0
    Thank you, Scott. Never done a "function" like that before. Pretty cool. I'll try it.
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,952
    Likes Received:
    30
    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
    
    
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    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
     

Share This Page