批量解决方案:
您可以使用FOR /R列出所有子文件夹:
FOR /R "V:\Photo Library" %G in (.) do @echo %G
并为每个文件夹启动稍作修改的命令:
FOR /R "V:\Photo Library" %G in (.) do for /F "delims=" %i in (listimagescopy.txt) do xcopy "%G\%i.jpg" "V:\Current Library\Work Zone" /D /Y
xcopy /D 将仅复制较新的文件, /Y 将覆盖而无需确认。在批处理文件中,检查源是否存在,您可以使用:
@echo off
FOR /R "V:\Photo Library" %%G in (.) do (
for /F "delims=" %%i in (listimagescopy.txt) do (
if exist "%%G\%%i.jpg" xcopy "%%G\%%i.jpg" "V:\Current Library\Work Zone" /D /Y
)
)
Perl 解决方案:
use strict;
use warnings;
use File::Find;
use File::Copy;
#filenames to match
my $filenames = join '|', map "\Q$_\E", split "\n", <<END;
filename1
otherfilename
another_one
etc
END
my $src_path = "V:\\Photo Library";
my $dst_path = "V:\\Current Library\\Work Zone";
find({ wanted => \&process_file, no_chdir => 1 }, $src_path);
sub process_file {
if (-f $_) {
# it's a file
if (/\/($filenames).jpg$/) {
# it matches one of the rows
if ((stat($_))[9] > ((stat("$dst_path/$1.jpg"))[9] // 0)) {
# it's newer than the file in the destination
# or destination file does't exist
print "copying $_ ...\n";
copy($_, $dst_path) or die "File $_ cannot be copied.";
}
}
}
}