[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] MacPerl batch file renaming?



On Thu, 30 Mar 2000 15:39:47 -0700, John McDermon wrote:

>I have what I think should be a relatively simple task 
>that I hope MacPerl will be able to handle. I have literally hundreds 
>of files with goofed up names that I would like to rename. The 
>actions fall into several broad categories. So I'll describe each by 
>and example.
>
>1) filename 1  --> filename1
>2) filename 1  --> filename_1
>3) filename.ext --> filename
>4) filename  --> filename.ext

Yes, using the File::Find module, that's relatively simple. It will:

 - read the names of the files in a directory before you can do anything
to a file, so changing a file name won't goof up your file list
 - chdir to the directory your file is in, so that can make life a bit
easier (not really in my final code example)
 - store the bare filename in $_ before calling your sub, and the
complete path in $File::Find::name, i.e. $name of the module's package.

>I think I could handle the first one by passing each filename to a 
>regular expression searching for the first blank something like "(*) 
>([1-9]+)" and then doing a rename to $1$2.

Almost. You forgot a dot just in front of your "*". And putting a
question mark right after the star is often a good idea, so that the
second part matches as much as possible, not the first part. Oh, and
anchor your regexes. Otherwise, you may match only part of your
filename. Like:

	/^(.*?) ([0-9]+)$/
or
	s/^(.*?) ([0-9]+)$/$1$2/;

Shorter version will work too.

Note that in using File::Find, you may assume $_ contains the bare
filename, not the full path.

>The second example is very simple (I think)
>	s/\s/_/;

Indeed. Or a variation thereof.

>The third would be
>	"(*).*" renaming to $1

Again, ".*?" instead of "*". And you must put a backslash in front of
that dot.

	s/(.*)\..*/$1/;

You don't needthe anchoring for this case.

>The fourth would be
>	"(*)" renaming to $1.ext

Again, ".*" not "*". And just a simple

	s/$/.ext"/;

which replaces the end of a string (no characters) with ".ext".

>I see in the book the reserved work "rename(OLDNAME,NEWNAME) which 
>looks like it'll do the trick.

Indeed. Note that by giving full paths to a file for replacement can
actually move the file to a different folder, provided it's on the same
disk. If you give no path for the replacement name, it is renamed in
place.

So here is an example using File::Find:

	use File::Find;
	find sub { -f or return; # only plain files
	     s/ +(\d+)$/$1/ and rename $File::Find::name, $_;
	}, @ARGV;

That is the complete script. Save as droplet, and drop away, both plain
files and folders! BTW this example will remove all spacces before
filenames ending with digits. Replace the s/// expression for other
results. Or did you actually want to combine the lot?

   HTH,
   Bart.

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org