Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

File::Find

by Anonymous Monk
on Jun 07, 2002 at 17:52 UTC ( [id://172589]=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone tell me how to search from the root directory over multiple drives on a windows box using: 'File::Find' Current code:
use File::Find; find(\&handleFind, '../'); sub handleFind { my $foundFile = $File::Find::name if 'VCVARS32.BAT'; print "$foundFile\n"; }
This is just returning everything from one directory below my current dir

Replies are listed 'Best First'.
(ichi) Re: File::Find
by ichimunki (Priest) on Jun 07, 2002 at 18:18 UTC
    CPAN is your friend here. You want the Win32::DriveInfo module. Otherwise the root ('/' not '../', btw) of each drive volume is separate: C:, D:, etc. (enclosed sample code untested)
    use File::Find; use Win32::DriveInfo; for( Win32::DriveInfo::DrivesInUse ) { find( \&handleFind, "$_:" ); } sub handleFind {... same as before ...}
    UPDATE: Correct error in use of drive letter ':' is required

    Note: installing Win32::DriveInfo requires installing Win32::API. The former is available only from CPAN, the latter is available as a PPM. Fortunately Win32::Drive info is pure Perl and can simply be copied to C:/perl/lib/site/lib/win32 and used from there.

      I agree, Win32::DriveInfo is best.
      As we just discussed here, this code also works:
      use File::Find; use Win32API::File qw( :Func :DRIVE_ ); my @drives = map { tr{\\}{/}s; $_ } grep { GetDriveType($_) == DRIVE_FIXED } getLogicalDrives(); find( sub { $File::Find::prune = 1, return if $File::Find::name =~ m{^.:/System Volume Information$}; print "$_\n"; }, @drives );
      Perhaps someone will find it useful when Win32API::File is at hand, but Win32::DriveInfo is not.
Re: File::Find
by stajich (Chaplain) on Jun 07, 2002 at 18:19 UTC
    Works for me on linux. You might also try using, find2perl which helps you construct a script to do this sort of thing. Here is sample output from running

    % find2perl -name "*.txt"

    I edited the wanted function adding the && print $name,"\n";

    #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, '.'); exit; sub wanted { /^.*\.txt\z/s && print $name,"\n"; }
Re: File::Find
by Anonymous Monk on Jun 07, 2002 at 18:07 UTC
    I don't think windows OSes have "root" directories/folders.
    You might have to determine what drives are available
    and iterate over those ...
    @drives = &someSubThatReturnsListODrives(); foreach my $drive ( @drives ) { find(\&handleFind, $drive); } sub handleFind { my $foundFile = $File::Find::name if 'VCVARS32.BAT'; print "$foundFile\n"; }
    ... or something like that. I hope someone comes up with a better answer for you. :)
      That's wierd! I didn't type
      $File::Find::name if 'VCVARS32.BAT'
      

      I typed $drive

      what's up with that?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172589]
Approved by ichimunki
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found