2014-03-04 33 views
0

我是新來的Perl,我想創建一個腳本,可以從不同的擴展名複製幾個文件從一個目錄到另一個。我試圖使用一個數組,但不知道如果這是可能的,但如果它更容易,我打開其他方式。使用不同的擴展名複製文件

我的代碼看起來像這樣;

my $locationone = "filepath" 
my $locationtwo = "filepath" 

my @files = ("test.txt", "test.xml", "test.html"); 

if (-e @files){ 
    rcopy($locationone, $locationtwo) 
} 

該代碼可能有點粗糙,因爲我要離開我的頭頂,我還是perl的新手。

我真的很感謝幫助。

Regards

回答

2

你最初的想法是對的,但它錯過了一些東西。

... 
use File::Copy; # you will use this for the copy! 
... 
my $dest_folder = "/path/to/dest/folder"; 
my @sources_filenames = ("test.txt", "test.xml", "test.html"); 
my $source_folder = "/path/to/source/folder"; 

我們設置了一些有用的變量:文件夾名稱和一組文件名。

foreach my $filename (@sources_filename) { 

我們碰到的文件名

my $source_fullpath = "$source_folder/$filename"; # you could use 
    my $dest_fullpath = "$dest_folder/$filename"; # File::Spec "catfile" too. 

然後我們建立(每個文件)的完整路徑開始名稱和完整路徑目標名稱。

copy($source_fullpath, $dest_fullpath) if -e $source_fullpath; 

最後我們複製只有當文件存在。

} 
+0

這個工作,非常感謝你對你的幫助:) – user2099445

0

你可以做這樣的事情:

foreach my $file (@files) 
{ 
    next unless (-e "$locationone/$file"); 
    `mv $locationone/$file $locationtwo`; 
} 
相關問題