Subject: [netatalk-admins] Script for renaming files for Mac to PC transfer

From: Jonathan Benson <[email-masked]>
Date: Mon, 12 Apr 1999 17:28:56 +1000

I've just been switching a number of users over from Mac to PC.  As part
of that process I've been copying their files (Word and Excel mainly) on
to the server (via netatalk) which is the same partition shared to the
PC users via Samba.

When trying to copy the folder on to their new PC I discovered numerous
problems with filenames that were valid on Mac but not on PC.  As I've
had this problem in the past I thought I'd write a little Perl script to
ease the renaming of files.

So I've attached it to this message.

I take NO RESPONSIBILITY for what this might do to your system, although
since it only renames files it should be fairly safe.

The only problem I can forsee is if you rename a Mac program
(executable) whilst it renames the resource file it doesn't edit the
resource file to reflect the new name and hence the program may not run
anymore.  Although that said, I just did a brief test by allowing it to
rename a program and the program still ran from the Mac afterwards.

Anwyay, I hope this is of some help to all.

Oh, to use it, just cd to the parent of the directories you want to
rename files under and run it.  Sorry for the lack of documentation, etc
but this is just something I wrote for myself and thought others might
be able to use.

Interesting note.  There is one type of filename that gave me a problem
that I didn't suspect would.  Specifically one of the form "filename."
Ie. ending in a period "."

This is a valid PC filename but under the version of netatalk/Samba I'm
running a filename of that type created on a Mac could not be read on a
PC.  Quite strange.  I don't have the time or inclination to investigate
further but if you want to remove that type of file it's the last patter
in that badpats array.

Regards

Jonathan

--
Jonathan Benson B.Eng. (Soft Sys Hon) <[email-masked]>
Systems Administrator, Phoenix Magazines, Phone: +61 3 9696 7200
http://www.phoenixmags.com.au/

#!/usr/bin/perl

#
# Perl program to fix Mac filenames to suit PCs
#
# By Jonathan Benson <[email-masked]> 12/4/99
#

if ( $ARGV[0] ) {
	print ("Usage: mactopc\n");
	print ("IE. No arguments are needed.\n\n");
	print ("This will recurse the directory structure from your current directory\n");
	print ("and rename any filenames that are not PC compatible.\n");
	exit (0);
}

use File::Find;

# Perl patterns to match and replace with an underscore "_"
@badpats = ("\\?", "\\\\", "\\/", "\\<", "\\>", "\\*", "\\|", "\\\"", ":..", "^ ", " \$", "\\.\$");

# This recursively steps through directories from the current one
finddepth(\&fixdir, '.');
print ("Done.\n");
exit (1);

# Renames files as needed
sub fixdir {
	foreach $badpat ( @badpats ) {
		opendir(DIR, ".") ||
			die ("Unable to open directory");
		@files = readdir(DIR);
		closedir(DIR);

		foreach $file ( @files ) {
			$file_fix = $file;
			$file_fix =~ s/$badpat/_/g;
			if ( $file ne $file_fix && $file ne "." && $file ne "..") {
				rename ($file, $file_fix);
				print ("Renaming $file to $file_fix\n");
			}
		}
	}
}