smanicka has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a sync program between some folders.The requirement was that one folder needs to contain just the files from the directories and sub directories present in the three input folders and needn't carry over the directory structure.

For ease I decided to just copy the files recursively using file::find.Here is the program.

#!usr/bin/perl use strict; use File::Find; use File::Copy; my @location=("\\\\network1\\pics","\\\\network1\\fonts","\\\\network1 +\\forms"); my $new_location="\\\\network1\\Sol"; foreach my $location(@location){ find(\&force_move,$location); } sub force_move(){ my $file=$_; print "copying $File::Find::file from $File::Find::dir\n"; copy($file,$new_location) or warn "$!" ; } print "Hey , I am done!!!!"; sleep(2);
My problem is that this code works beautifully when i try using local folders.But if it is folders on the network, it doesn't give me any output as in it compiles, executes without any problems and shows the prompt again, but no files are copied. Could any one tell me what I can do to rectify this? Thanks Sandhya

UPDATE

It works now. I have no clue what changed the behavior.And it didn't work when i had $file::Find::file .I instead just used $file that is assigned t the current value of $_. I used just the mapped network drive in the path and am printing out the file names so that i can figure out if it works or not.

thanks

Sandhya

Replies are listed 'Best First'.
Re: file::find problems accessing files on a network
by shmem (Chancellor) on Jan 05, 2009 at 16:18 UTC

    Read File::Find again. $_ only holds the file name (as in basename $path) - you want

    copy($File::Find::name, $new_location) or warn "$!" ;
      Thanks to your post.I tried changing that.Still no files are being copied.It won't even print out the file names using that print statement in force_move function when I try for network files

        I wonder why you get the file names printed out using

        print "copying $File::Find::file from $File::Find::dir\n";

        since there's no variable $File::Find::file in File::Find - at least in my version (1.10). What version do you have (print $File::Find::VERSION) ?

        What happens if you change

        my @location=("\\\\network1\\pics","\\\\network1\\fonts","\\\\network1 +\\forms");

        to

        my @location = ("//network1/pics", "//network1/fonts", "//network1/for +ms");

        - does $File::Find::name hold the full path, then?

Re: file::find problems accessing files on a network
by zentara (Cardinal) on Jan 05, 2009 at 16:04 UTC
    I'm fairly certain that File::Find only works on the local filesystem. You will need scp, sftp or something similar to get over a network.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      the network drive has been mapped locally .would this overcome the problem if i use the local mapped path?
Re: file::find problems accessing files on a network
by runrig (Abbot) on Jan 05, 2009 at 17:07 UTC

    I don't see how your code works at all, whether local or network directories. File::Copy requires two file arguments, it does not accept a directory as the second argument like command line copy. And FYI, find() accepts a list of directories, so there's no need to use a for loop to iterate through your list of source directories.

    Also, I once wrote a file sync utility for Windows. You may have to tweak it for your specific purpose if you decide to use it, though it may be overkill for your purposes.

    Update: Hmm, I just tested File::Find::copy on Windows copying a file to a directory, and it works, so nevermind about that. I wasn't aware that it accepted a directory as the second argument (though it does not work that way on Unix).