2017-07-16 39 views
-3

即時通訊嘗試檢查是否存在比X minuts更早的文件(我不在乎文件夾)。不幸的是,我可以;不知道我的代碼在哪裏。 我將所有並欣賞幫助:)Perl:找到超過X分鐘數的文件的最快方法,將最舊到最新排序

1.找到比分鐘

#!/usr/bin/perl 

my $maindir = "C:\\Users\\Dor\\Desktop\\aba"; 
my $minutesold = 60; 
my $now = time; 
my $filedisc; 

# Declare arrays 
my @xmlfiles; 
my @qulfiedfiles; 

# Declare a Dictionary 
my %filedisc; 

opendir(my $dh, $maindir) or die "opendir($maindir): $!"; 

# Read all the files 
while (my $de = readdir($dh)) 
{ 
    # get the Full path of the file 
    my $f = $maindir . $de; 

    if (-f $f) 
    { 

     push (@xmlfiles, $f); 

    } 
} 
    closedir($dh); 


# For every file in directory 
for my $file (@xmlfiles) { 

    # Get stats about a file 
    my @stats = stat($file); 

    # If time stamp is older than minutes provided 
    if ($stats[9] >= ($now - (($minutesold * 60)))){ 

     # Put the File and Time stamp in the dictionary 
     print($stats[9] ."   .|   " .$file ."\n\n"); 

    } 

    #print($now ."\n") 
    #print($now - ($minutesold * 60) ."\n"); 
} 
+2

當你運行它什麼不順心?你看過@xmlfiles並驗證了正確的文件名被添加到它嗎? – ysth

+3

像'#Declare arrays'這樣的東西很荒謬。如果你看不到接下來的內容,那麼你就沒有理解代碼和意見的希望。已經過了20年或更長的時間,只是爲了說明而留下您的代碼,最好使用合理的標識符來表達您的想法。只有當代碼模糊或者沒有經驗(可能是由於優化),或者當代碼不清楚時,您才需要註釋。既不適用於此代碼中的任何評論,所以它們僅用於掩蓋它。 '#讀取所有文件'完全錯誤。 – Borodin

回答

-1

解決這個實物的功能編程風格是走在這裏我想順便說一句:

my $dir = shift() || $ENV{HOME}; #command line arg or else home dir 
my $minutesold = 60; #1h                                                
opendir my $dh, $dir or die "ERR: opendir($dir) $!\n"; 

print 
map  "$$_{timestamp}   .|   $$_{file}\n", 
#sort { $$a{timestamp} <=> $$b{timestamp} } # sort by age 
#sort { $$a{file}  cmp $$b{file}  } # sort by name 
grep $^T-$$_{timestamp} >= 60*$minutesold, # $^T is program startup time() 
map  {{timestamp=>(stat($_))[9], file=>$_}} 
grep -f $_, 
map  "$dir/$_", 
readdir $dh; 

closedir $dh; 
+0

hm,爲什麼downvote沒有評論: - / –

-1

的X數的文件你已經錯過了一個簡單的方法來獲取文件的修改時間在Perl:在 - M開關。

my $modifiedTimeinDays = -M "$file"; 

    my $modifiedTimeinSec = $modifiedTimeinDays*60*60*24; 

    if($modifiedTimeinSec > 60) 
    { 
     # file older than 60 sec 
    } 

就這麼簡單。

請參閱perldoc -f -X瞭解所有文件測試。

+0

即時獲取空白輸出。我試了一個文件夾中的文件: '#!/ usr/bin/perl my $ maindir =「C:\\ Users \\ Dor \\ Desktop \\ aba \\ a.txt」; my $ filename =「a.txt」; my $ age = -M $ maindir; print $ age; ' – lol

+0

@lol在腳本的頂部添加'use warnings'和'strict strict;'。它會告訴你'$ age'是否是未定義的,在這種情況下這可能意味着該文件不存在。 – stevieb

+0

Re *「在這種情況下可能意味着文件不存在*」,確切的錯誤將在'$!'中找到 – ikegami

1

路徑和文件不正確。

my $f = $maindir . $de; 

應該

my $f = "$maindir/$de"; 
4

通常最好使用glob而非opendir/readdir,以避免「重建」的完整路徑(路徑和文件之間添加斜線)文件爲每個結果

您可能會想在Windows上啓用:bsd_glob選項,以便正確處理具有空格的路徑(如C:\Program Files

use strict; 
use warnings 'all'; 

use File::Glob ':bsd_glob'; # Provide for spaces in path 

my $root  = 'C:\Users\Dor\Desktop\aba'; 
my $minutesold = 60; 

my @old_files = grep { -f and -M * 24 * 60 > $minutes_old } glob "$root\\*.*"; 
+0

這是我得到的輸出:參數「* main :: 24」在C:\ Users \ Dor \ Desktop \ folderTest.pl第27行的乘法(*)中不是數字。 – lol

相關問題