[vox-tech] Another Perl Question: File::Find module not working as expected

Ken Herron vox-tech@lists.lugod.org
Tue, 01 Jul 2003 16:26:33 -0700


--On Tuesday, July 01, 2003 15:30:49 -0700 Richard Crawford 
<rscrawford@mossroot.com> wrote:

> The following script addresses the problem I had earlier of moving files
> from a Windows computer to a Unix server when the directory structures
> are different.  Within each subfolder on the Unix server there is a
> directory called "messages".  For some reason, it seems that File::Find
> is not traversing the directory tree properly and going into the
> messages folder at all.

It looks like you expect move() to be called once per directory. In fact 
it will be called for every file and directory encountered under 
$start_dir. For example, if $start_dir contains five files and a 
subdirectory with three more files, then find is going to call move() 
nine times; six times in $start_dir and three in the subdirectory.

I suspect the immediate problem is that you're creating and deleting 
files within the directories you're traversing. This may be causing some 
reorganization within the directory, leading to readdir() skipping some 
files.

Let me suggest the function called by find() should just store each name 
of interest in a global array or hash. Once the find() is finished you 
can iterate through the array. Assuming you only want to call move once 
per directory, something like the following would get you started:

	my @dirs;
	sub wanted { push @dirs, $File::Find::Name if (-d $_) }
	find \&wanted, $start_dir;
	move($_) foreach (@dirs);

-- 
Kenneth Herron  Kherron@newsguy.com     916-366-7338