On 7/14/08, Rob Dixon <[EMAIL PROTECTED]> wrote: > > > jet speed wrote: > > > > I put togather few lines of code, I am looking to achieve the below > > > > dir1 with file1, file2 > > dir2 with file1, file2 > > > > i want to copy the files from each of the directory to a third directory > > dir3 as file1, file2 from dir1 and rename the file1 as file3 and file2 as > > file4 from the dir2. > > > > dir3 with file1,file2,file3,file4 > > > > Please let me know how to modify the below to achieve this. Thanks in > > advance > > > > > > #!/usr/bin/perl -w > > > > # dir1 with file1, file2 > > # dir2 with file1, file2 > > # copy inot dir3 list of files file1, file2, file3, file4 > > > > use strict; > > use warnings; > > > > > > my @files; > > my $file; > > my $dir = "/work/dir1"; > > opendir (DIR, $dir); > > @files = readdir(DIR); > > closedir(DIR); > > print "@files \n"; > > foreach $file (@files) { > > print "$file \n"; > > } > > > > > > my @files1; > > my $file1; > > my $num1=1; > > my $dir1 = "/work/dir2"; > > opendir (DIR, $dir1); > > @files1 = readdir(DIR); > > #closedir(DIR); > > > > foreach $file1 (@files1) { > > print "$file1.$num1 \n"; > > system "(mv $file1 $file1.$num1"); > > } > > > > closedir(DIR); > > This seems to do the job. > > HTH, > > Rob > > > use strict; > use warnings; > > use File::Spec; > use File::Path; > use File::Copy; > > my @files = (files_in('/work/dir1'), files_in('/work/dir2')); > print "$_\n" foreach @files; > > foreach my $file (@files) { > next unless $file =~ /\d+$/; > copy_next($file, '/work/dir3'); > } > > > > sub files_in { > my $dir = shift; > opendir my $dh, $dir or do { > warn $!; > return (); > }; > my @files = grep not(/^\.\.?$/), readdir $dh; > map File::Spec->catfile($dir, $_), @files; > } > > sub copy_next { > > my ($from_file, $to_dir) = @_; > my $name = (File::Spec->splitpath($from_file))[-1]; > > mkpath $to_dir; > > my $to_file = File::Spec->catfile($to_dir, $name); > > while (-e $to_file) { > $to_file =~ s/(\d+$)/$1+1/e; > } > > copy $from_file, $to_file or warn $!; > } >
Hi Rob, Thanks for the update, I have tested it & works perfect. I am still getting use to File:Spec, good learning. Thanks again, Much appreciated. Js