0

フォルダとそのサブフォルダ内のファイルのリストを検索し、結果を別のフォルダにコピーしたいと思います。

私は現在使用しています:

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

「フォトライブラリ」にサブフォルダがあり、コマンドラインで「listimagescopy.txt」にリストされているファイルのサブフォルダも検索する必要があります。

また、サブフォルダー内に同じ名前のファイルが2つある場合があります。リストからファイルを探すときに、各ファイルの新しいバージョンを返すように指定できる必要があります(または、これが複雑な場合は、 cmdを使用して、ファイルをコピーするときに別の名前が付けられていても問題ありませんex file1 file2)

4

1 に答える 1

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.";
      }
    }
  }
}
于 2013-01-02T19:54:32.060 に答える