2009-09-08 52 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);  
}