2013-05-09 54 views
2

我有一個腳本來查找目錄和.tar.gz文件。無論什麼原因(這就是我希望你能幫助我)我的Perl Directory Finder不起作用?

if (-d $file) 

不返回,它是一個目錄!

我的腳本:

# specify the directory where you want to start the search 
my $directory = $ARGV[0]; 
my $directoryCount = 0; 

# Calling the Subroutine, which searches the File 
readDirectory($directory); 

sub readDirectory 
{ 
    my $directory = shift; 
    my $searchfile = shift; 
     my @directories; 
     my $tarOuput; 

    # a little bit output, in which directory the script 
    # is searching at the moment (the following line is not necessary) 
    print "Searching in $directory\n\n"; 

    # Open and close the directory 
    opendir(DIR, $directory) or die("ERROR: Couldn't open specified directory $!"); 
    my @files = readdir DIR; 
    closedir DIR; 

    shift(@files); 
    shift(@files); 

    print "------------------------------------------------------------------------ \n\n"; 

    foreach my $currentFile (@files) 
    { 
     print "Current File:", $currentFile, "\n\n"; 
     if (-d $currentFile) #THIS DOESN'T WORK?!?!?!?!? 
     { 
         print "Directory: ", $currentFile, "\n\n"; 
         #push (@directories, $currentFile); 
         #print "Found new directory:  $directories[$directoryCount]\n\nCurrent number = $directoryCount\n\n"; 
         #$directoryCount++; 
         #print "Directories: ", @directories, "\n\n"; 
         #next; 
         # The Subroutine is calling hisself with the new parameters 
         #readDirectory($currentFile); #recursive call through sub-directories 
     } 

     elsif ($currentFile =~ /\.tar.gz$/i || $currentFile =~ /\.tar$/i) 
     { 
         print "File: ", $currentFile, "\n\n"; 
         my $tarOutput = `tar -tvzf $currentFile`; 
         print $tarOutput, "\n"; 
     } 

     print "----------------------------------------------------------------------- \n\n"; 
    } 
} 

print語句將打印$ currentFile如果它是一個目錄從未打印...

輸出:

Searching in /home/gackerma/Logs 

------------------------------------------------------------------------ 

Current File:Dir1 

----------------------------------------------------------------------- 

Current File:file 

----------------------------------------------------------------------- 

Current File:configs.tar.gz 

File: configs.tar.gz 

tar (child): configs.tar.gz: Cannot open: No such file or directory 
tar (child): Error is not recoverable: exiting now 
tar: Child returned status 2 
tar: Error is not recoverable: exiting now 

----------------------------------------------------------------------- 

Current File:adstatlog.299 

----------------------------------------------------------------------- 

Current File:adstatlog.tgz 

----------------------------------------------------------------------- 

我已經做了完全相同的事情與一個超級簡單的腳本在同一目錄中,它的工作原理......但不,不在這裏。我不明白什麼是...?

請幫忙嗎?

回答

3

這是什麼看起來不對。請參見下面的diff

--- yours.pl  
+++ mine.pl 

    # Open and close the directory 
    opendir(DIR, $directory) or die("ERROR: Couldn't open specified directory $!"); 
- my @files = readdir DIR; 
+ my @files = grep { $_ !~ /^\.{1,2}$/ } readdir DIR; 
    closedir DIR; 

- shift(@files); 
- shift(@files); 

    foreach my $currentFile (@files) 
    { 
+  my $fullPath = "$directory/$currentFile"; 
     print "Current File:", $currentFile, "\n\n"; 
-  if (-d $currentFile) #THIS DOESN'T WORK?!?!?!?!? 
+  if (-d $fullPath) #THIS DOESN'T WORK?!?!?!?!? 
     { 
      print "Directory: ", $currentFile, "\n\n"; 
      #push (@directories, $currentFile); 
      #print "Directories: ", @directories, "\n\n"; 
      #next; 
      # The Subroutine is calling hisself with the new parameters 
-   #readDirectory($currentFile); #recursive call through sub-directories 
+   readDirectory($fullPath); #recursive call through sub-directories 
     } 

我猜雙shift的意思是推卸「」和「..」,這是不正確的。如果你做了sort @files,這可能會有效,但仍然是一個不好的方法。見grep

接下來的問題是,$currentFile需要在這裏完整的路徑。否則,它在尋找$currentFile在當前工作目錄

+0

是的,這就是班次用的。謝謝你! – user2340116 2013-05-09 20:31:31

+0

@ user2340116現在清楚了嗎?或者你讀這個'diff'輸出有問題嗎? – chrsblck 2013-05-09 20:43:08

+0

它工作了!啊,非常感謝你!完整目錄路徑是關鍵。 – user2340116 2013-05-09 20:47:15

0

嘗試:

if (-d $directory.'/'.$currentFile)