tags:

views:

138

answers:

4

Do you guys have an idea on how to search or list down .exe files on the server I am currently using. (or maybe place it in an array)? I will use this command in my Perl program. Assuming that my program is also located on the said server. My OS is Linux - Ubuntu if that even matters, just in case. working in CLI here. =)

+1  A: 

Perl to find every file under a specified directory that has a .exe suffix:

#!/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";
}
chaos
Thanks! I'll have to try this one, but needs to install the modules first. =)
Suezy
You could take a coderef as the second param of process_dir so that you can reuse the process_dir.
Geo
@Suezy - This program uses only modules from Perl's core distribution, meaning if you have Perl you should have those two modules (and more) installed.
Chris Lutz
+1  A: 

Have a look at File::Find.

Alternatively, if you can come up with a command line to the *nix file command, you can use find2perl to convert that command line to a Perl snippet.

innaM
+4  A: 

As mentioned, It is not clear whether you want '*.exe' files, or executable files. You can use File::Find::Rule to find all executable files.

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

If you are looking for executable files, you (the user running the script) need to be able to execute the file, so you probably need to run the script as root.

It might take a long time to run to.

If you are looking for .exe files, chances are that your disk is already indexed by locate. So this would be much faster:

  my @exe= `locate \.exe | grep '\.exe$'`
mirod
I think i need the latter. thanks! ;)
Suezy
A: 

I'll probably be shot down for suggesting this, but you don't have to use modules for a simple task. For example:

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

Of course, it will need to have some tweaking for your particular choice of starting directory (here, I used ~ for the home directory)

EDIT: Maybe I should have said until you get the modules installed

pavium
nice..this is what I'm "looking" for..lol. just a simple command.. just like mirod's.. =)
Suezy
oh...thanks anyway! =) next time I'll figure it out. *a newbie* ;D
Suezy
Thanks Suezy. I'm poking my tongue out at whoever downvoted me.
pavium
Just got downvoted again, several months after the question was posted. It is still the accepted answer, see Suezy's comment above.
pavium