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

batch renames

Discussion in 'Support' started by drrob1, Oct 29, 2009.

  1. drrob1

    drrob1 Guest

    I know that this has been covered before many many times, but I cannot find it now that I want it.

    I want to batch rename a bunch of files
    ren 123.txt abc123.txt

    To prevent files from being renamed twice or more, I did this:
    dir *.txt >filelist.txt

    for %x in (@filelist.txt) ren %x abc-%x

    but of course this does not work.

    Thanks for your help
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    drrob1 wrote:
    | I know that this has been covered before many many times, but I
    | cannot find it now that I want it.
    |
    | I want to batch rename a bunch of files
    | ren 123.txt abc123.txt
    |
    | To prevent files from being renamed twice or more, I did this:
    | dir *.txt >filelist.txt
    |
    | for %x in (@filelist.txt) ren %x abc-%x
    |
    | but of course this does not work.

    Try this (UNTESTED!):

    *dir/b/a-d/ne *.txt > filelist.txt
    for %x in (@filelist.txt) ren %x abc-%x

    --
    Steve
    <><
     
  3. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,304
    Likes Received:
    39
    Works for me, with a couple of minor tweaks:

    Code:
    dir /b *.txt > file.lst
    for %x in ( @file.lst ) ren %x abc-%x
    
    You could also use an exclusion range instead of a file list:

    Code:
    for /[!abc-*] %f in ( *.txt ) ren %f abc-%f
    
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,856
    Likes Received:
    83
    The new in v11 /O:... option will save the file list before performing the
    operation, so there isn't a need for the FOR.
     
  5. drrob106

    Joined:
    Jan 24, 2009
    Messages:
    36
    Likes Received:
    0
    On Thu, 2009-10-29 at 18:57 -0500, rconn wrote:

    Rex, I could not get that to work. I tried this:
    ren /o:n *.txt abc_*.txt

    This did not work. It only renamed the 1st file it found.

    The file list did work, when I removed all the junk in the redirected
    directory listing (forgot about that).

    So I got it to work w/ the for option, not the new in v11 option.
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,856
    Likes Received:
    83
    Works perfectly here, though of course with your syntax it won't rename it
    the way you expect. The way REN works (thanks to CMD) is to replace the
    first four characters of the filename with "abc_" rather than prefixing
    "abc_" onto the existing name. (So if your filenames are <= 4 chars, it'll
    only rename one.)

    Also, note that FOR and DO also support the /O:... syntax, so even if you
    need to use one of those commands (i.e., if your target expansion is too
    complex) you still don't need to use a file list.
     
  7. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,949
    Likes Received:
    30
    On Thu, 29 Oct 2009 20:00:09 -0500, rconn <> wrote:

    This topic has come up with some regularity over a long time.

    Suggestion/request: How about the next version's REN having an /X, say, option
    to use regular expressions with captures [ \(...\) ] to identify that target
    files and expressions involving references to the captures [ \1, \2, ... ] to
    specify the new name? That would allow the user to pick the old name apart and
    put the pieces back together as desired.

    Perhaps the OP could kludge something together using an alias and 4UTILS's
    @XREPLACE[] ... (untested)

    alias xren `for %f in ( "::%1" ) ren %f %@xreplace[%1,%2,%f]`
    --
    - Vince
     
  8. Howard Goldstein

    Joined:
    Jun 1, 2008
    Messages:
    111
    Likes Received:
    1
    Drrob106 wrote:

    I think that what Rex meant was that you could use delayed variable
    expansion along with the new /O: option. Try:

    ren /o:n *.txt abc_%%@name[*].txt

    --
    Howard
     
  9. Péter Köves

    Joined:
    Jun 1, 2008
    Messages:
    58
    Likes Received:
    0
    I agree that this feature has been missing from TCC. My solution is the attached perl script. I've been using it for several years, though the current incarnation requires perl 5.10.

    The OP could then use

    C:\...\test>dir /hkm
    2009-10-30 12:47 0 1.txt
    2009-10-30 12:47 0 2.txt
    2009-10-30 12:47 0 12.txt
    2009-10-30 12:47 0 1234.txt
    2009-10-30 12:47 0 abc.tst

    C:\...\test>patren /(.*\.txt)$/abc-$1/
    C:\CMD\test
    1.txt -> abc-1.txt
    12.txt -> abc-12.txt
    1234.txt -> abc-1234.txt
    2.txt -> abc-2.txt
    4 files renamed

    (Note that I have SET .pl=perl -S, hence patren can be used as a command).
     

    Attached Files:

  10. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Pter Kve wrote:
    | ---Quote (Originally by vefatica)---
    || Suggestion/request: How about the next version's REN having an /X,
    || say, option
    || to use regular expressions with captures [ \(...\) ] to identify
    || that target
    || files and expressions involving references to the captures [ \1, \2,
    || ... ] to
    || specify the new name? That would allow the user to pick the old
    || name apart and
    || put the pieces back together as desired.
    ||
    || Perhaps the OP could kludge something together using an alias and
    || 4UTILS's @XREPLACE[] ... (untested)
    ||
    || alias xren `for %f in ( "::%1" ) ren %f %@xreplace[%1,%2,%f]`
    || --
    || - Vince
    || ---End Quote---
    | I agree that this feature has been missing from TCC. My solution is
    | the attached perl script. I've been using it for several years,
    | though the current incarnation requires perl 5.10.

    No attachment was delivered to my mailbox. I don't know whether or not it is
    visible via browser.

    | The OP could then use
    |
    | C:\...\test>*dir /hkm*
    ...

    Is that command a typo, which should be "*dir /hkm *"?

    Your solution may be perfect for you, and probably includes protection about
    multiple renaming, but I prefer doing it without invoking an external, when
    it can be done without such.
    --
    Steve
     
  11. drrob106

    Joined:
    Jan 24, 2009
    Messages:
    36
    Likes Received:
    0
    On Thu, 2009-10-29 at 21:05 -0500, Howard Goldstein wrote:

    I needed a full example, not just a feature suggestion.

    Thanks.
     
  12. Péter Köves

    Joined:
    Jun 1, 2008
    Messages:
    58
    Likes Received:
    0
    Its just "dir /hkm". I bolded it for the post, perhaps that's where the *-s come from (bug in forum software?). The attachment is there in the web interface, but I missed it in the first version of the post and added it later.

    I mostly agree with your preference, though on today's machines I don't see too much of a performance hit for invoking an external process (may be significant when doing it in a loop of course). My normal modus operandi is to do as much as possible in TCC or TCC batches/aliases and switch to perl for really complex tasks. Of course, I am very familiar with perl (means I probably managed to learn about 60% of it in the last 15 years :)).
     
  13. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,856
    Likes Received:
    83
    ?? That IS a full example - try it!
     
  14. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    rconn wrote:
    | The new in v11 /O:... option will save the file list before
    | performing the operation, so there isn't a need for the FOR.

    In other words, the command
    *ren /o:u * xyz*
    would rename each file exactly once with minimal penalty? That's a very nice
    new feature, and ought to be explicitly mentioned in the documentation. If
    COPY, DO and FOR behave the same way, e.g.,
    *copy /o:u * dup*
    creates exactly one duplicate of each file originally present, their help
    topics ought to be similarly improved.
    --
    Steve
     
  15. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,189
    Likes Received:
    11
    Delayed expansion is a very useful tool for this sort of thing. However,
    I have always had trouble getting it to work with multiple functions.

    For example:

    ren *.txt abc_%%@right[3,%%@name[*]].%%@ext[*]

    -Scott

    vefatica <> wrote on 10/29/2009 09:25:58 PM:


    option

    target

    ] to

    nameapart and

    4UTILS's

     
  16. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,189
    Likes Received:
    11
    Is this behavior documented anywhere? It's not in the CMD help for REN
    and it's not in the TCC help for REN as far I could tell.

    -Scott

    rconn <> wrote on 10/29/2009 08:59:54 PM:


    it

    it'll

     
  17. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,304
    Likes Received:
    39
    I don't think it's meant to work with multiple functions. What you can do is create a user-defined function -- delayed expansion does work with UDFs:

    Code:
    function newname=`abc_%@right[3,%@name[%$]].%@ext[%$]`
    ren /o:u *.txt %%@newname
    [*]
    
     
  18. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,189
    Likes Received:
    11
    Yes. That's usually what I end up resorting to.

    -Scott

    Charles Dye <> wrote on 10/30/2009 11:50:48 AM:


    functions.


     
  19. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,856
    Likes Received:
    83
    I suspect it'll be useful to about 5 people, which is why it is not
    prominently featured in the docs.
     
  20. Jim Cook

    Joined:
    May 20, 2008
    Messages:
    604
    Likes Received:
    0
    I've had to work around the ren problem myself, but did not grok that
    using /O: would affect the issue. I'm quite happy to learn about that
    usage of it.

    On Fri, Oct 30, 2009 at 6:44 PM, rconn <> wrote:



    --
    Jim Cook
    2009 Saturdays: 4/4, 6/6, 8/8, 10/10, 12/12 and 5/9, 9/5, 7/11, 11/7.
    Next year they're Sunday.
     

Share This Page