batch renames

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
D

drrob1

Guest
#1
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
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
<><
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,383
39
Albuquerque, NM
prospero.unm.edu
#3
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.
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
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#4
> 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.
The new in v11 /O:... option will save the file list before performing the
operation, so there isn't a need for the FOR.
 
Jan 24, 2009
36
0
#5
On Thu, 2009-10-29 at 18:57 -0500, rconn wrote:

> ---Quote---
> > 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.
> ---End Quote---
> The new in v11 /O:... option will save the file list before performing the
> operation, so there isn't a need for the FOR.
>
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.
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#6
> > The new in v11 /O:... option will save the file list before
> performing the
> > operation, so there isn't a need for the FOR.
> >
> ---End Quote---
> 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.
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
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
Drrob106 wrote:

>
> On Thu, 2009-10-29 at 18:57 -0500, rconn wrote:
>
>
> ---Quote---
>> ---Quote---
>> > 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.
>> ---End Quote---
>> The new in v11 /O:... option will save the file list before performing
>> the
>> operation, so there isn't a need for the FOR.
>>
> ---End Quote---
> 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.
>
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
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
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).
 

Attachments

#10
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
 
Jan 24, 2009
36
0
#11
On Thu, 2009-10-29 at 21:05 -0500, Howard Goldstein wrote:

> >> > 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.
> >> ---End Quote---
> >> The new in v11 /O:... option will save the file list before performing
> >> the
> >> operation, so there isn't a need for the FOR.
> >>
> > ---End Quote---
> > 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.
> >
> ---End Quote---
> 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
I needed a full example, not just a feature suggestion.

Thanks.
 
#12
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
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 :)).
 
#14
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
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#15
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:


> 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
nameapart 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
>
>
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#16
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:


> 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.)
>
>
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,383
39
Albuquerque, NM
prospero.unm.edu
#17
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.
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
[*]
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#18
Yes. That's usually what I end up resorting to.

-Scott

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


> Quote:
>
> Originally Posted by samintz [image removed]
> 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.

>
> 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[*]
>
>
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#19
> 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.
I suspect it'll be useful to about 5 people, which is why it is not
prominently featured in the docs.
 
#20
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:

> ---Quote---
>> 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.
> ---End Quote---
> I suspect it'll be useful to about 5 people, which is why it is not
> prominently featured in the docs.
>
>
>
>
>


--
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.