2013-12-18 43 views
0

我正在創建一個Perl腳本來部署webcode(Windows 2008 Server)。我需要首先複製目標文件夾中的所有舊文件,併爲存檔目錄名稱(arc_dir.20131217)上的時間戳結尾創建文件的存檔目錄。將文件移入存檔。然後我需要將源代碼中的代碼複製到目標文件夾中。然而,它根本不起作用,而且我爲什麼無能爲力。Perl webcode部署腳本

兩件事情,我很熟悉Perl,很快就會看到,我不想讓別人爲我做代碼。它有點挫敗學習的目的。指導和對話會很棒。我是一個退伍軍人的夢想,願意學習,我希望只寫清潔的代碼。

use strict; 
use warnings; 
use autodie; 
use File::Copy; #Gives you access to the "move" command 
use File::Path; #Copies data recursively from one dir and creates a new dir 
use POSIX; 



#Set Directories 
use constant { 
    TIMESTAMP  => strftime("%Y%M%d%H%M%S", localtime); 
    Source_Dir  => "C:\Users\Documents\Source_Dir", 
    destination_Dir => "C:\Users\Documents\Destination_Dir", 
    ARCHIVE   => "C:\Users\Documents\arc_dir.TIMESTAMP", 
}; 
#Creates new directory to archive old files 
make_path('C:\Users\Shaun\Documents\arc_dir.TIMESTAMP'); 


#Need to copy destination dir, create archive dir and paste data to it 
#Opens destination_Dir, so I can read from it 
opendir my $dir, destination_Dir; 

# Loop through directory and grab all of the files and store in var 
while (my $file = readdir $dir) { 
    my $destination_Dir = destination_Dir . "/" . "$file"; 
    move $destination_Dir, ARCHIVE; 

#Loop through directory and copy all webcode to destination_Dir 
opendir my $dir, Source_Dir; 

while (my $file = readdir $dir) { 
my $Source_Dir = Source_Dir . "/" . "$file"; 
move $Source_Dir, destination_Dir; 

回答

1

腳本中有一些語法錯誤。我會在命令行上使用-c來檢查腳本的語法(如perl -c)。請確保您的()和{}匹配。

另一項是反斜槓(\)需要在雙引號(「)中用反斜槓(\)轉義,否則它只是轉義字符串中的下一個字符(並且可能不是你需要一個路徑名)在雙引號中的字符串在被處理之前被內插,其中單引號不是(對sinqle引號與雙引號之間差異的很好解釋:http://www.perlmonks.org/?node_id=401006)。你可能想要改變雙引號,引用在你的makepath調用中,否則TIMESTAMP將不會被改變,目錄名將會被命名爲TIMESTAMP

我也建議在一些print語句中指出正在做什麼,並給出反饋意見,表示項目正在進行。作爲打印「移動$ destination_Dir到ARCHIVE」和「移動$ Source_Dir到destination_Dir」會讓你知道文件正在移動。