Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从子文件夹复制文件?(文件名列表位于文本文件中)使用命令行和perl_Perl_Batch File_Copy_Cmd_Subdirectory - Fatal编程技术网

如何从子文件夹复制文件?(文件名列表位于文本文件中)使用命令行和perl

如何从子文件夹复制文件?(文件名列表位于文本文件中)使用命令行和perl,perl,batch-file,copy,cmd,subdirectory,Perl,Batch File,Copy,Cmd,Subdirectory,我想搜索文件夹及其子文件夹中的文件列表,并将结果复制到其他文件夹 我目前正在使用: for /F "delims==" %i in (listimagescopy.txt) do copy "V:\Photo Library\%i.jpg" "V:\Current Library\Work Zone" “photo library”中有子文件夹&我需要命令行也在子文件夹中查找“ListImageScope.txt”中列出的文件 此外,在子文件夹中可能有两个同名文件-我需要能够指定,在文件夹中查

我想搜索文件夹及其子文件夹中的文件列表,并将结果复制到其他文件夹

我目前正在使用:

for /F "delims==" %i in (listimagescopy.txt) do copy "V:\Photo Library\%i.jpg" "V:\Current Library\Work Zone"
“photo library”中有子文件夹&我需要命令行也在子文件夹中查找“ListImageScope.txt”中列出的文件


此外,在子文件夹中可能有两个同名文件-我需要能够指定,在文件夹中查找列表中的文件时,它应返回每个文件的较新版本(或者,如果使用cmd执行此操作非常复杂,则在复制文件时,如果它们的名称不同,例如file1 file2,也可以)批量解决方案:

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.";
      }
    }
  }
}
您可以使用列出所有子文件夹:

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.";
      }
    }
  }
}
使用严格;
使用警告;
使用File::Find;
使用文件::复制;
#要匹配的文件名
my$filenames=join'|',map“\Q$\ue”,split“\n”,1},$src\u路径);
子进程文件{
如果(-f$){
#这是一个文件
如果(/\/($filename).jpg$/){
#它匹配其中一行
if((stat($)[9]>((stat($dst_path/$1.jpg))[9]//0)){
#它比目标中的文件新
#或目标文件不存在
打印“复制$\…\n”;
复制($\$dst\\路径)或死“无法复制文件$”;
}
}
}
}

您的问题是什么?您列出的代码段已经复制了.txt文件中列出的文件。你想要一个脚本来获取搜索令牌并复制与令牌匹配的文件吗?到目前为止,“照片库”只是一个文件夹&现在我在“照片库”中添加了子文件夹,我需要能够执行相同的命令,只是它还应该查找子文件夹中的文件应该在serverfault.com上询问我是否应该粘贴它?当我刚刚粘贴它时-我得到这个--@echo off FOR/R“V:\Photo Library”%%G in(.)do(%%G此时是意外的。FOR/F“delims=“%%i in(listImageScope.txt)do(%%i此时是意外的。如果存在”%%G\%%i.jpg“xcopy”%%G\%%i.jpg“V:\Current Library\Work Zone”/D/Y))@june将最后一段代码粘贴到.CMD文件中,然后启动它。或者你可以粘贴第二行,以FOR开头。它试图复制每个文件,即使是不存在的文件,但是它只是为了兴趣而跳过它们-我只是看了你的个人资料&我看到你提到了perl我也对perl感兴趣,虽然我不是很了解它-如果它不是太复杂,我不介意知道如何做同样的事情,但在perl中我相信它会更灵活(如果我想调整脚本,我可以获得实时帮助)考虑
my$filenames=join'|',map“\Q$\ue”,split“\n”,