Question:

How do I search for a file using perl?

by  |  earlier

0 LIKES UnLike

I am trying to get this to work....it first uses FSUTIL to get the

current drives, then what king of drives, they are, if they are read/

write drives I want to search for a specific file and delete it.. This

is what I have so far..

open(FSUTIL,"fsutil fsinfo drives | ");

while (<FSUTIL>) {

$DRIVES=$_;

}

close(FSUTIL);

chop $DRIVES;

chop $DRIVES;

$DRIVES=~ s/Drives://;

#print "DRIVES:[$DRIVES]";

@drive=split(/\\/,$DRIVES);

foreach $letter (@drive) {

print "Searching drive:$letter\n";

open(FSUTIL,"fsutil fsinfo drivetype $letter|");

while (<FSUTIL>) {

$DRIVETYPE= $_;

}

close(FSUTIL);

print "DRIVETYPE:$DRIVETYPE\n";

if ( grep("Fixed Drive",$DRIVETYPE) != "" ) {

print "HIT\n"; ("this is just a test to see if it worked)

}

then from here I need to search said drive(s) for a specific file and delete that file...Im stuck

 Tags:

   Report

1 ANSWERS


  1. Well, I haven&#039;t actually tried this in Perl for Windows, although I assume you can feed the drive designation to the opendir command.  So you ought to be able to do something like:

    foreach $letter (@drive) {

    ...

    @files = find_file(&quot;$letter:&quot;,&quot;my_filename.txt&quot;);

    ...

    }

    sub find_file {

        my($dir,$name) = @_;

        return undef unless(-d $dir);

        return undef unless(opendir(XML_DIR,$dir));

        my(@files) = ();

        foreach my $file (readdir(XML_DIR)) {

            next if($file =~ /^\.\.?$/);

            if(-d &quot;$dir\\$file&quot;) {

                push @files,find_file(&quot;$dir\\$file&quot;,$name);

            } elsif(-f &quot;$dir\\$file&quot; &amp;&amp; $file =~ /^\Q$name\E$/) {

                push @files, &quot;$dir\\$file&quot;;

            }

        }

        return @files;

    }

    DaveE

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.