use strict; use File::Copy; # Setup source and target directories my $source_dir = "c:\\perl_stuff\\appendix"; my $target_dir = "c:\\perl_stuff\\test"; # Setup dir commands my $cmd1 = "dir \/b \/a:-d ".$source_dir; my $cmd2 = "dir \/b \/a:-d ".$target_dir; # Get file list from dir command for source directory my $list1 = `$cmd1`; # Strip out blank lines $list1 =~ s/^\s*$//gm; # Get file list from dir command for target directory my $list2 = `$cmd2`; # Strip out blank lines $list2 =~ s/^\s*$//gm; # Dump the dir command outputs into arrays (my @files1) = (split /\n/,$list1); (my @files2) = (split /\n/,$list2); # Loop through array of file lists from source directory foreach my $file1 (@files1) { # If current file is not in target directory, copy it if (!(grep $_ eq $file1, @files2)) { # Setup variables will full path for the file to be copied my $file2 = $target_dir."\\".$file1; $file1 = $source_dir."\\".$file1; # Do the copy copy($file1,$file2) || die "Unable to copy '$file1' to '$file2': $!\n"; } }