Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

why not glob

by Boldra (Deacon)
on Aug 14, 2009 at 05:55 UTC ( [id://788527]=note: print w/replies, xml ) Need Help??


in reply to Quickest way to get a list of all folders in a directory

I don't know why I see glob so rarely recommended in the monastary. I understand earlier perl versions had some problems on some OSs, but it's fine now (isn't it?)
my @folders = glob('//pcname-vm/c$/program files/adobe/*');
Looks much cleaner, doesn't it? And probably you weren't interested in all of the files in the adobe directory anyway.

I do a bit of this sort of work, here are some other things that might be interesting:
  • 'c:/program files' is language dependant. eg here in Germany it's 'c:/programme'
  • Win32::TieRegistry works across networks, you might find it easier and quicker, particularly for finding versions of installed software.
  • This kind of task is called 'software inventory'. There are commercial products available.

UPdate: correction in code


- Boldra

Replies are listed 'Best First'.
Re: why not glob
by Anonymous Monk on Aug 14, 2009 at 07:00 UTC
    For directories only you want
    grep -d, glob ...
    I don't use glob because it has its own syntaxt and quirks, I find this much clearer to use File::Find::Rule with regex
    #!/usr/bin/perl -- use strict; use warnings; use File::Find::Rule; my $dir = '//pcnamevm/c$/program files/adobe/'; $dir = 'C:/program files/Mozilla Firefox/'; my @folders = File::Find::Rule->directory->maxdepth(1)->in($dir); use DDS; Dump \@folders; __END__ $ARRAY1 = [ 'C:/program files/Mozilla Firefox', 'C:/program files/Mozilla Firefox/chrome', 'C:/program files/Mozilla Firefox/components', 'C:/program files/Mozilla Firefox/defaults', 'C:/program files/Mozilla Firefox/dictionaries', 'C:/program files/Mozilla Firefox/extensions', 'C:/program files/Mozilla Firefox/greprefs', 'C:/program files/Mozilla Firefox/modules', 'C:/program files/Mozilla Firefox/plugins', 'C:/program files/Mozilla Firefox/res', 'C:/program files/Mozilla Firefox/searchplugins', 'C:/program files/Mozilla Firefox/uninstall' ];
      One difference between glob and File::Find::Rule is that the latter also returns the parent directory ('C:/program files/Mozilla Firefox', in your example). It seems the OP does not want that.
        I think that is a bug :D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-20 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found