joshboski has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get this to work....it first uses FSUTIL to get the current drives, then what kind 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

Replies are listed 'Best First'.
Re: find and delete file in Drive
by grep (Monsignor) on Jul 30, 2008 at 21:03 UTC
    You can use File::Find to find the file and unlink to delete it.

    There are several examples in both sets of documentation that will get you there.

    UPDATE
    Other Notes: Use strictures and warnings.
    Indent your code (at least for us)
    Don't use chop. Use chomp
    Use a real array and push

    # ex my @drives; # you want an array foreach (<FSUTIL>) { chomp; # don't use chop push(@drives, $_); } close(FSUTIL);
    grep
    One dead unjugged rabbit fish later...
Re: find and delete file in Drive
by betterworld (Curate) on Jul 30, 2008 at 22:24 UTC

    I don't know fsutil, but I think grep is right: you should use File::Find.

    However, some more remarks:

    • eq compares strings, != compares numbers.
    • grep is only for lists; use eq or $str =~ /foo/ to match strings.
    • It's always helpful to to check the return values of open and (especially when dealing with command pipes) close
    • for errors.
    • Rather than embedding variables in commands, I prefer to use the several-argument form of open:
      open my $handle, '-|', 'fsutil', 'fsinfo', 'drivetype', $letter or die $!;

    Hmm, it seems kind of funny that I have two links with the same title. In case you did not notice, one is for the user and one is for the function ;-)

Re: find and delete file in Drive
by poolpi (Hermit) on Jul 31, 2008 at 09:13 UTC