2013-05-06 104 views
-1

我想要做的是在指定的位置創建一個文件夾,然後用該日期和用戶名首字母命名該文件夾。我希望用戶能夠在創建文件夾時輸入首字母縮寫。我已經想出瞭如何以正確的格式生成日期,但我需要弄清楚如何將用戶輸入$ initials添加到一起,以便文件夾名稱類似於「130506SS」。我無法弄清楚如何將這兩個變量連接在一起以獲得正確的文件夾名稱。任何人都可以幫我解決這個問題嗎?如何加入兩個變量來創建文件夾名稱?

use strict ; 
    use warnings ; 
    use POSIX qw(strftime); 
    my $mydate = strftime("%y%m%d",localtime(time)); #puts the year month date and time in the correct format for the folder name  
    print "Enter users initials: "; 
    my $initials = <STDIN>; # prompts for user input 

    #$mydate.= "SS"; #stores today's date and the initials 

    $mydate.= $initials; 


    sub capture { 

    my $directory = '/test/' . $mydate; 

     unless(mkdir($directory, 0777)) { 
      die "Unable to create $directory\n"; 

      } 

      }   

    capture(); #creates the capture folder 


    sub output { 

    my $directory = '/test2/' . $mydate; 

     unless(mkdir($directory, 0777)) { 
      die "Unable to create $directory\n"; 

      } 

      }  

    output(); #creates the output folder 

編輯:上述腳本的全部部分工作,除非我試圖加入兩個變量來創建文件夾名稱。 ($ mydate。= $ initials;)我用($ mydate。=「SS」;)測試了它,而腳本完美地工作。我可以設法加入變量$ mydate和一個字符串而不是$首字母。

+0

當你運行你的代碼時,你會得到錯誤消息嗎? – toolic 2013-05-06 19:01:27

回答

2

您尚未指出您認爲哪個位無法工作,但我懷疑這是因爲您已創建了文件夾/文件名中的嵌入換行符。

隨着下面你有$指明MyDate初始化爲一個日期字符串和$縮寫從標準輸入的一行:

my $mydate = strftime("%y%m%d",localtime(time)); 
my $initials = <STDIN>; 

這裏要注意的一點是,$縮寫有一個換行符在的結束輸入;在加入他們之前,你會想擺脫那個換行符。下面的代碼會做你想要什麼:

chomp ($initials); 
$mydate .= $initials; 
1

當我運行代碼,我得到一個錯誤:「無法創建/測試/ 130506SS」。

一個問題是mkdir無法遞歸創建目錄,但可以使用File::Path中的make_path

另一個問題是您應該輸入chomp

use strict; 
use warnings; 
use POSIX qw(strftime); 
use File::Path qw(make_path); 
my $mydate = strftime("%y%m%d", localtime(time)); #puts the year month date and time in the correct format for the folder name 
print "Enter users initials: "; 
my $initials = <STDIN>;        # prompts for user input 
chomp $initials; 
#$mydate.= "SS"; #stores today's date and the initials 

$mydate .= $initials; 

sub capture { 
    my $directory = '/test/' . $mydate; 
    unless (make_path($directory)) { 
     die "Unable to create $directory\n"; 
    } 
} 

capture(); #creates the capture folder 
相關問題