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

[MacPerl] batch change file names



Here's a beginner's script that batch change file names. Would anyone care to show me a better code? I think this script can probably be made into 2 lines or so.

The script traverse a specified folder and change each file to a new name. The name is pulled from the first line of the file that contains a date.

------------------
#!perl -w
use File::Find;
use English;
use strict;

find(\&fileFilter,'APS600:No need to backup:weblogs:');


sub fileFilter {
	if ($File::Find::name =~ m@[^:]$@) {
	renameFile($File::Find::name)
	};
};

# renameFile ("filePath") will rename the file by
# a date pulled from the first line of the file.
# If the line contains e.g. [17/Nov/1997:...
# the name of the file is then 1997.Nov.17
sub renameFile {
	my($inFile,$line,@a);

	$inFile = $_[0];

	open(WEBLOG,"<$inFile") || die("Error while open file: $!");
	$line = <WEBLOG>;
	$line =~ m@\[(\d\d/\w+/1997)@; # e.g. [17/Nov/1997:01:37:00]
	$line = join ( '.', reverse(split m'/', $1));
	
	@a = split(m':',$inFile); pop @a;
	$line = join(':', @a, $line);
	
	rename($inFile, $line) or die("Error in renaming file: $!");
	
	close (WEBLOG);
};

------------------

 Xah
 xah@best.com
 http://www.best.com/~xah/SpecialPlaneCurves_dir/specialPlaneCurves.html
 Mountain View, CA, USA



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch