renaming badly named files...

N

Naran Hirani

Hi All,

I am looking for a perl code fragment, or utility that will enable me to
rename badly named files and/or dir elements to such files, by replacing
the dubious character with an underscore. It would nice if such a
utility worked recursively through unix dirs and also had a mode where
by it just shows what would be renamed.

If any body knows of such a utility or supply me with a code fragment
that might do the above please could you e-mail me as well as posting here.

TIA.
Naran
 
T

Tad McClellan

I am looking for a perl code fragment, or utility that will enable me to
rename badly named files


To provide code that does that, you would need to define "badly named".

What counts as "badly named"?

and/or dir elements to such files, by replacing
the dubious character with an underscore.


What should it do when these 2 files are present in the same directory?

foo?bar
foo_bar

if we follow your specification, data will be lost.

Fix your specification.

It would nice if such a
utility worked recursively through unix dirs


use File::Find;

and also had a mode where
by it just shows what would be renamed.


That would be easy enough.

If any body knows of such a utility or supply me with a code fragment
that might do the above


Since you've left many things unspecified, that means we get to
pick whatever specs we want. Being Lazy, we are likely to pick
specs that make it easier on us, rather than what Does What You Need.

Poor specs lead to poor implementations, so take some care when
composing your spec.

---------------------------
# untested
use File::Find;

find \&unwanted, '.';

sub unwanted {
return unless /\W/; # only \w chars allowed

my $new = $_;
$new =~ s/\W/_/g; # or maybe: tr/a-zA-Z0-9_/_/cs

return if -e $new; # don't stomp over existing files

rename $_, $new or die "could not move '$_' $!";
}
---------------------------

please could you e-mail me as well as posting here.


Ask here, get the answer here (maybe).
 
N

Naran Hirani

Tad,

Thanks for your comments and code. I realize my spec was sub-optimal
and vague.
But this was deliberately so to some extent to allow for a highly
flexible, perhaps pre-existing
utility that meets my fuzzy requirements :)

Thanks also to all the other guys who responded atleast I now have a few
things to ponder upon.

Cheers.
Naran.
 
N

Naran Hirani

Tim,

Thanks for your suggestion. It sounds quite promising to me, but will it
handle the following scenario:

I/have a badly named sub_dir/with some more badly named files here.txt

I.e. will it deal with sub dirs in the path as well as the final files
under the sub dirs?

Of course, I am thinking of using rename in conjunction with the unix
find util.

Cheers.
Naran

Tim said:
Naran Hirani graced us by uttering:

I am looking for a perl code fragment, or utility that will
enable me to rename badly named files and/or dir elements to
such files, by replacing the dubious character with an
underscore. It would nice if such a utility worked recursively
through unix dirs and also had a mode where by it just shows
what would be renamed.

If any body knows of such a utility or supply me with a code
fragment that might do the above please could you e-mail me as
well as posting here.

You can find the rename.pl utility here:

http://borkware.com/quickies/files/rename.pl

Using this script, you can enter any expression as the first
argument and it will be executed on each filename. e.g.:

rename.pl 's/\.MP3$/mp3/;' *.MP3

In your case, try the tr/// operator:

rename.pl 'tr/bad_charset/_/;' [files to rename]

In this was, rename.pl has become one of the most flexible tools
in my ~/bin/.

HTH,
Tim Hammerquist
 
J

Jürgen Exner

[Please do not top post! Jeopardectomy performed]
[Please don't full-quote. Reduced to reasonable excerpt]

Naran said:
Tim said:
Naran Hirani graced us by uttering:
I am looking for a perl code fragment, or utility that will
enable me to rename badly named files and/or dir elements to
such files, by replacing the dubious character with an
underscore. [...]
You can find the rename.pl utility here:
http://borkware.com/quickies/files/rename.pl
[...]
Thanks for your suggestion. It sounds quite promising to me, but will
it handle the following scenario:

I/have a badly named sub_dir/with some more badly named files here.txt

I.e. will it deal with sub dirs in the path as well as the final files
under the sub dirs?

Did you actually check those 7 lines of code in the file? There is no piece
of code that would read a directory/change to a different directory/recurse
over a directory tree, so no, it won't.
Of course, I am thinking of using rename in conjunction with the unix
find util.

Why not using Perl Find::Find module?
Note: you should do a depth-first approach. I am not sure if Find::File
still recurses correctly when you change a directory name on it in the
middle of the operation.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,104
Messages
2,570,643
Members
47,247
Latest member
youngcoin

Latest Threads

Top