【问题标题】:How can I copy files from subfolders? (list of filenames is in a text file) using the command line and perl [closed]如何从子文件夹中复制文件? (文件名列表在文本文件中)使用命令行和 perl [关闭]
【发布时间】:2012-12-17 03:38:14
【问题描述】:

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

我目前正在使用:

for /F "delims==" %i in (listimagescopy.txt) do copy "V:\Photo Library\%i.jpg" "V:\Current Library\Work Zone"

“照片库”中有子文件夹,我需要命令行也应该在子文件夹中查找“listimagescopy.txt”中列出的文件

在子文件夹中可能有 2 个同名文件 - 我需要能够指定在文件夹中查找列表中的文件时,它应该返回较新版本的每个文件(或者如果这对 cmd 来说很复杂,如果它复制文件时它们的名称不同,例如 file1 file2 也可以)

【问题讨论】:

  • 您的问题是什么?您列出的 sn-p 将已经复制 .txt 文件中列出的文件。您是否需要一个脚本来获取搜索令牌并复制与该令牌匹配的文件?
  • 直到现在“照片库”只是一个文件夹,现在我已经在“照片库”中添加了子文件夹,我需要能够执行相同的命令,它还应该在其中查找文件子文件夹
  • 应该在 serverfault.com 上询问

标签: perl batch-file copy cmd subdirectory


【解决方案1】:

批量解决方案:

您可以使用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.";
      }
    }
  }
}

【讨论】:

  • 我应该把它粘贴进去吗?
  • 当我把它粘贴进去时——我明白了——@echo off FOR /R "V:\Photo Library" %%G in (.) do ( %%G 是意外的在这个时候。 for /F "delims=" %%i in (listimagescopy.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$_\E", split "\n", &lt;&lt;END;,然后删除两个替换,以防文件名中存在可能影响正则表达式匹配的元字符。
猜你喜欢
  • 2014-12-04
  • 2015-02-25
  • 2020-08-05
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 2012-01-02
相关资源
最近更新 更多