2009-09-08 142 views
-5

你們是否有一個想法如何搜索或列出服務器上的.exe文件 我目前正在使用(或可能將它放在一個數組中)?
我將在我的Perl程序中使用這個命令。假設我的程序也位於上述服務器上。 我的操作系統是Linux - Ubuntu,如果這很重要,以防萬一。在這裏使用CLI。 =)如何搜索.exe文件

+1

你能給的你真正想要實現的一個例子嗎? Linux不使用「.exe」文件,這是Windows的事情。 – 2009-09-08 06:49:29

+0

我的Perl程序旨在刪除一個特定的.exe文件..例如,所有「sampleFile.exe」不同版本的程序執行時都必須刪除,因爲我只需要最新版本。可能嗎? – Suezy 2009-09-08 06:55:13

+0

不確定在linux會話中運行時是否可以讀取Windows可執行文件的特定版本元數據。這個任務在Windows中不會更容易嗎? – benPearce 2009-09-08 06:57:12

回答

0

我可能會爲這個建議被擊落,但你不必使用模塊一個簡單的任務。例如:

#!/usr/bin/perl -w 
@array = `find ~ -name '*.exe' -print`; 
foreach (@array) { 
    print; 
} 

當然,這需要有一些調整的起始目錄中的特定選擇(這裏,我用〜主目錄)

編輯:也許我應該說直到你得到的模塊安裝

+0

nice..this是我正在「尋找」....ol。只是一個簡單的命令..就像mirod的.. =) – Suezy 2009-09-08 07:14:53

+0

哦......謝謝! =)下次我會弄清楚。 *一個新手*; D – Suezy 2009-09-08 07:16:47

+0

謝謝Suezy。我向任何低估我的人伸出舌頭。 – pavium 2009-09-08 07:18:24

1

Perl來尋找帶有.exe後綴指定目​​錄下的所有文件:

#!/usr/bin/perl 

use strict; 

use File::Spec; 
use IO::Handle; 

die "Usage: $0 startdir\n" 
    unless scalar @ARGV == 1; 
my $startdir = shift @ARGV; 

my @stack; 

sub process_file($) { 
    my $file = shift; 
    print $file 
     if $file =~ /\.exe$/io; 
} 

sub process_dir($) { 
    my $dir = shift; 
    my $dh = new IO::Handle; 
    opendir $dh, $dir or 
     die "Cannot open $dir: $!\n"; 
    while(defined(my $cont = readdir($dh))) { 
     next 
      if $cont eq '.' || $cont eq '..'; 
     my $fullpath = File::Spec->catfile($dir, $cont); 
     if(-d $fullpath) { 
      push @stack, $fullpath 
       if -r $fullpath; 
     } elsif(-f $fullpath) { 
      process_file($fullpath); 
     } 
    } 
    closedir($dh); 
} 

if(-f $startdir) { 
    process_file($startdir); 
} elsif(-d $startdir) { 
    @stack = ($startdir); 
    while(scalar(@stack)) { 
     process_dir(shift(@stack)); 
    } 
} else { 
    die "$startdir is not a file or directory\n"; 
} 
+0

謝謝!我將不得不嘗試這一個,但需要先安裝模塊。 =) – Suezy 2009-09-08 07:01:34

+0

您可以將coderef作爲process_dir的第二個參數,以便您可以重新使用process_dir。 – Geo 2009-09-08 07:03:17

+1

@Suezy - 這個程序只使用Perl核心發行版中的模塊,這意味着如果你有Perl,你應該安裝這兩個模塊(以及更多)。 – 2009-09-08 07:12:13

1

看一看File::Find

或者,如果您可以想出命令行命令,則可以使用find2perl將該命令行轉換爲Perl片段。

5

如上所述,您是否需要'* .exe'文件或可執行文件尚不清楚。 您可以使用File :: Find :: Rule查找所有可執行文件。

my @exe= File::Find::Rule->executable->in('/');  # all executable files 
    my @exe= File::Find::Rule->name('*.exe')->in('/'); # all .exe files 

如果您正在尋找可執行文件,你(運行腳本的用戶)需要能夠執行文件,所以你可能需要運行腳本根。

運行可能需要很長時間。

如果您正在尋找.exe文件,則可能是您的磁盤已被locate索引。因此,這將是更快:

my @exe= `locate \.exe | grep '\.exe$'` 
+0

我想我需要後者。謝謝! ;) – Suezy 2009-09-08 07:12:14

0

得到遞歸使用

use File::Find; 
##cal the function by sending your search dir and type of the file 
my @exe_files = &get_files("define root directory" , ".exe"); 

##now in @exe_files will have all .exe files 

sub get_files() { 
    my ($location,$type) = @_; 
    my @file_list; 
    if (defined $type) { 
     find (sub { my $str = $File::Find::name; 
      if($str =~ m/$type/g ) { 
       push @file_list, $File::Find::name ;    
      } 
     }, $location); 
    } else { 
     find (sub {push @file_list, $File::Find::name }, $location); 
    } 
    return (@file_list);  
}