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

Hi, I use the below code to move files from a specified directory as a command line argument to another directory, but this script fails when recursing into subdirectories. I guess we have tweak the $file =~ s/\//\\/; Can anybody hlp please? i have given the sample o/p below.

use strict;
use File::Find;
use warnings;
my @files = ();
my @dirs = @ARGV or die "Please supply a valid directory to search";
print "\n All contrib and keep Files within the specified directory will be moved recursively to D:\\jude-test \n"; find(\&move, @dirs);
print "\n Moving .contrib and .keep files to D:\\jude-test \n";
for my $file ( @files ) {
$file =~ s/\//\\/;
print "Moving $file to D:\\jude-test\n";
system("move $file D:\\jude-test");
}

print "\n";

sub move {
if(-f && m/\.(contrib|keep)$/) {
push @files, $File::Find::name;
}
}
O/P:
Moving f:\temp\ument.keep to D:\jude-test
1 file(s) moved.
Moving f:\temp\test/4Document.keep to D:\jude-test
The system cannot find the path specified.
  • Comment on Problem with file path structure in DOS

Replies are listed 'Best First'.
Re: Problem with file path structure in DOS
by Prof Vince (Friar) on Sep 06, 2007 at 11:43 UTC
    You're better off using File::Copy :
    #!/usr/bin/perl use strict; use warnings; use File::Copy qw/move/; use File::Find; use constant TARGET => 'D:\\jude-test'; find sub { move $File::Find::name, TARGET if -f && /\.(contrib|keep)$/ }, @ARGV;
    (Yes, this shebang is kinda ankward for a Windows script :)
Re: Problem with file path structure in DOS
by nimdokk (Vicar) on Sep 06, 2007 at 12:06 UTC
    You might also want to consider using File::Spec to help with formatting the paths properly. I noticed that in the output, the directory seperator in the filename that files is going the wrong way. While this could simply be a typo in posting, it is something to watch out for. Also, as others have noted, it would be better to use File::Copy instead of shelling out of Perl to use Windows 'move'.
      Thanks all for your comments , File::Copy with move combo works fine.
Re: Problem with file path structure in DOS
by girarde (Hermit) on Sep 06, 2007 at 13:28 UTC
    Your pattern needs the //g modifier so that it will replace all instances of /. Right now it stops after the first instance.
Re: Problem with file path structure in DOS
by Anonymous Monk on Sep 06, 2007 at 11:44 UTC
    use File::Copy