# copyNewOnly.pl script copies files from source to destination folder # only if they don't already exist on destination. # # written 26-feb-04 by fgb. # # NB: args must use forward slashes (unix style)! require "ctime.pl"; #use File::Basename; use File::Copy; use File::Find; use Getopt::Long; $bDebug = 0; my $sAppName = "copyNewOnly"; $nCopyTotal = 0; $sDest = ""; my $sNow = &ctime(time); $sSource = ""; my $bVerbose = 1; my $sVersion = "0.1"; _main: $result = GetOptions('/|-','help|?'=>\$bHelp); if ($bHelp) { help(); exit; } if ($bVerbose) { print "$sAppName $sVersion startup at $sNow"; } if ($#ARGV+1) { $sSource = shift @ARGV; } if ($#ARGV+1) { $sDest = shift @ARGV; } if ($bDebug) { print "dbg: src=$sSource dest=$sDest\n"; } if (length($sSource) < 1) { errMsg("invalid source folder"); exit; } if (length($sDest) < 2) { errMsg("invalid destination folder"); exit; } my @dirlist = $sSource; find(\&procFile,@dirlist); print "\nCopied $nCopyTotal files; $sAppName complete.\n"; ### subroutines: sub errMsg { my $s = "$sAppName error: " . @_[0]; print "$s\n"; } sub errorReport { print Win32::FormatMessage(Win32::GetLastError()); } sub help { print "$sAppName $sVersion - copy only files that don't exist on target\n"; print "\nusage: $sAppName sourceFolder destinationFolder\n"; print "\nNB1: args must use forward slashes (unix style)!\n"; print "NB2: run from an administrator account if possible.\n"; } sub procFile { my $sCmd = ""; my $sOrigSpec = $File::Find::name; my $sDestSpec = $sOrigSpec; if ($bDebug) { print "dbg: src='$sSource' dest='$sDest'\n"; } $sDestSpec =~ s/$sSource/$sDest/; ### doesn't work unless '/' path delimiter used $sDestSpec =~ s/^\\\\\\/\\\\/; if (-e $sDestSpec) { if ($bVerbose) { print "$sDestSpec exists; skipping\n"; } } else { if ($bDebug) { print "copy $sOrigSpec $sDestSpec...\n"; } else { copy($sOrigSpec,$sDestSpec); print '.'; ++$nCopyTotal; } } } __END__