in reply to Recursive Directory Copying to Single Target Directory

What was the question? Your approach seems right, but your code looks a little mangled around the copy() call. Make sure the stuff inside the sub { ... } is what you want to happen for each file found and the arguments to find are correct (compare to examples in File::Find, with your sub { ... } in place of \&wanted).
  • Comment on Re: Recursive Directory Copying to Single Target Directory

Replies are listed 'Best First'.
Re^2: Recursive Directory Copying to Single Target Directory
by Knoperl (Acolyte) on Jan 28, 2009 at 06:40 UTC
    The problem is that my program does not work. I took you advice and cleaned it as shown below:
    #!/usr/local/bin/perl -w use strict; use File::Find; use File::Copy; my $sourcedirectory = $ARGV[0]; my $targetdirectory = $ARGV[1]; copyfiles(); sub copyfiles() { #the $sourcedirectory below tells the find where to start #looking for files. The .TXT tells it only to return the files #with the path if the file is a .TXT file. It then uses the copy #of File::Copy to copy the entire path\filename to a SINGLE #directory target. find(sub { copy(({win_path{$File::Find::name$/"if(/\.TXT$/)}", "$sourcedirectory"),"$targetdirectory"); } ) } # ruzam figured out the following line on # how to convert the resulting string to MS-DOS complaint format sub win_path { (my $path = shift) =~ s![\\/]+!\\!g; return $path; }

    But I get the following error during when I try to run it:

    Scalar found where operator expected at notworking.pl line 21, near "$ +File::Find ::name$/" (Missing operator before $/?) String found where operator expected at notworking.pl line 21, near "$ +/"if(/\.TX T$/)}"" (Missing operator before "if(/\.TXT$/)}"?) syntax error at notworking.pl line 21, near "$File::Find::name$/" syntax error at notworking.pl line 23, near ") " Illegal declaration of subroutine main::win_path at notworking.pl line + 27.
      That stuff in there is still nothing that makes any sense. You're going to need to decide what has to happen and write the code to do it, not just throw in stuff until it stops complaining.

      A few hints: the directory to look in should be passed to find(), not copy(). copy() should be passed a filename (presumably returned from win_path) and the target directory. You'll need an if statement around the copy() call to make it only called for .TXT files.