2014-09-29 192 views
1

我試圖編寫一個腳本,該文件將基於文件名(類似)將文件從一個文件夾複製到另一個文件夾。因爲我在文件夾中有幾千個文本文件。但我試圖從數千個文件中找出幾百個文件。搜索它需要很長時間。基於類似的文件名將文件從一個文件夾複製到另一個文件夾

Copy似乎是一個好主意,可以在這裏使用,然後用於遍歷我試圖從數千中找出的文件列表。但Copy需要指定的名稱。問題是我只有部分文件名。文件名的

ABCDEF-A01

ADEWSD-B03

ABDDER-C23

例:文件列表(文本文件的內容)的

GGI_1409506_ABCDEF-A01.txtGGI_ADEWSD-B03.txtDA_ABDDER-C23_12304.txt

我只得到ABCDEF-A01而不是完整的文件名。

預期的結果:

能夠通過文件夾進行搜索和文件複製到匹配根據文件列表(一個文本文件),另一個位置。

任何你可以分享的東西?信息/ ans /相關帖子?非常感謝!

+0

這是在Windows上運行?是使用PowerShell的解決方案好嗎? – 2014-09-29 03:28:42

+0

@RobertKelly yes是Windows。 Powershell是好的。因爲我也瞭解shell的基本知識。謝謝! – 2014-09-29 03:30:18

回答

1

嘗試在Perl下面的代碼。運行程序時,將源目錄路徑和目標目錄路徑的參數連同需要搜索的文件名列表一起傳遞。如果目標目錄不存在,將通過程序自動創建一個文件夾如下圖所示:

代碼:

use strict; 
use warnings; 
use File::Copy; 
my $source = $ARGV[0]; 
my $destination = $ARGV[1]; 
my $listFiles = $ARGV[2]; 

if(-f $destination) 
{ 
     print "Already unknown extension of file exists with the same name of directory. So rename the file and run the program"; 
     exit 0; 
    } 

if(-d "$destination") 
{ 
     print "Directory where files need to be copied: $destination\n"; 
    } 
else  
{ 
     print "No Directory found and hence created the directory $destination\n"; 
     mkdir("$destination"); 
     } 

opendir DIR, $source or die "cant open dir"; 
my @files = grep /(.*?)(\.txt)$/,(readdir DIR); 
open my $fh, '<', "$listFiles" or die "Cannot open the file names to search $listFiles - $!"; 
open my $out,'>', "$ARGV[1]\\NoMatch.txt" or die "Cannot write to the file NoMatch.txt - $!"; 
my @listFileNames = <$fh>; 
my @listFiles =(); 
foreach my $InputFiles (@files) 
{ 
     chomp($InputFiles); 
    foreach my $list(@listFileNames) 
    { 
    chomp($list); 
    if($InputFiles =~ /$list/isg) 
     { 
     print "Files : $InputFiles copying\t"; 
     copy("$InputFiles","$destination"); 
     print "Files : $InputFiles copied\n"; 
     push(@listFiles,$list);  
      } 
     } 
     } 

my %seen =(); 
my $count = 0; 
foreach my $list (@listFiles) 
{ 
    $seen{lc($list)} = 1; 
    #print $list . "\n"; 
     } 

foreach my $listnames (@listFileNames) 
{ 

    if($seen{lc($listnames)}) 
     { 
     } 
    else 
     { 
     if($count ==0) 
     { 
     print "\nFilenames that did not match the text files are present in the destination folder : NoMatch.txt file " . "\n"; 
      } 
     print $out "$listnames\n"; 
     $count++; 
     } 
    } 
close($out);  
close($fh);  
closedir(DIR); 
+0

然後代碼的哪一部分將讀取包含所有相似文件名的「文件列表」? – 2014-09-29 06:05:28

+0

您向我們提供了需要複製到目標文件夾中的文件名的模式。那麼你的意思是相似的文件名?你能通過提供輸入和預期輸出來具體嗎? – Praveen 2014-09-29 07:02:23

+0

這裏所有與您提供的文本文件類似模式的文件列表都被放入數組@files中,並將這些文件複製到目標目錄中。 – Praveen 2014-09-29 07:09:00

0

創建一個批處理文件,並將其放入源文件夾中,並將其與要複製的文件列表放在一起。

for /f %%f in (list.txt) do robocopy c:\source d:\dest %%f 
0

希望這有助於

#!/usr/bin/perl -w 
    use strict; 
    use File::Copy; 

    my $sorce_direcrtory = qq{}; 
    my $new_directory = ""; 

    opendir(my $dh, $sorce_direcrtory) || die; 
    while(readdir $dh) { 
     if($_ =~ /[A..Z]+\-[A..Z]\d+/){ 
      move("$sorce_direcrtory/$_", "$new_directory/$_"); 
     } 
    } 
    closedir $dh; 
+0

hmm,'$ sorce_directory'我應該在這裏放什麼?把我的'文件列表(.txt)'或所有文件所在的路徑?哪部分代碼調用文件列表(.txt)' – 2014-09-29 05:33:36

+0

路徑到您的目錄 – Noble 2014-09-29 08:53:35

相關問題