Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Q regarding glob

by arunagiri (Initiate)
on May 16, 2013 at 17:55 UTC ( [id://1033881]=perlquestion: print w/replies, xml ) Need Help??

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

I am a basic user of perl to sort files etc: I am using glob in the following context foreach $element (glob "../../dir1/dir2/*/dir3/filename"){ print "$element\n"; } I am getting the Complete path. How can I get only the folder names inside dir2? Is there any easy way to get it?

Replies are listed 'Best First'.
Re: Q regarding glob
by stephen (Priest) on May 17, 2013 at 00:21 UTC

    Easiest way is probably to use File::Slurp.

    Here's an untested example:

    #!env perl use strict; use warnings; use File::Slurp qw/read_dir/; my $Dir_Prefix = '../../dir1/dir2'; my $File_Path = 'dir3/filename'; MAIN: { my @files = read_dir($Dir_Prefix); foreach my $file (@files) { -d $file or next; if (-e "$Dir_Prefix/$file/$File_Path") { print "$file\n"; } } }

    Update:Updated from a File::Find example.

    stephen

      That's Informative and usefull. Thanks
Re: Q regarding glob
by 2teez (Vicar) on May 16, 2013 at 19:07 UTC

    Hi arunagiri,
    "..How can I get only the folder names inside dir2? Is there any easy way to get it?.."

    You could use basename subroutine from the module File::Basename.

    Update:

    basename("/foo/bar"); # this return "bar" #OR for my $ele (glob(...)){ print basename($ele), $/; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: A regarding glob
by LanX (Saint) on May 16, 2013 at 18:26 UTC
    please use <code> tags around your code.

    > Is there any easy way to get it?

    No, not what you intend with "easy"!

    Use a regex or split or substr to isolate what you wanted from the full path

    edit

    DB<106> $head="/tmp/";$tail="/foo/file" => "/foo/file" DB<107> $path= <$head*$tail> => "/tmp/bla/foo/file" DB<108> ($middle) = $path =~ /^\Q$head\E(.*)\Q$tail\E$/ => "bla"

    Easy enough! (?)

    update

    FWIW a one liner:

    DB<109> ($middle) = <$head*$tail> =~ /^\Q$head\E(.*)\Q$tail\E$/ => "bla"

    update
    added quotemeta

    update

    Not sure if I misunderstood your question or if you phrasing is just fuzzy. What are folder names inside dir2 ?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Sorry if I didn't mention it clearly. There are many folders inside dir2. Eg: A1, B1, C1 etc: are the folder inside dir2. only A1 and B1 folders have a file names "hello.txt" Hence, I want only the names of A1 and B1. If I do glob from some other folder like glob ("../../dir1/dir2/*/hello.txt") How can I get the only folders A1 and B1. I am going through the various suggestions that you guys gave. I will try them out. Thanks
        > Sorry if I didn't mention it clearly.

        Sorry still not clear! (And unfortunately you didn't update your posts with code-tags either).

        2 possibilities:

        • If you want the whole sub-path which fills into the wild-card, then the demonstrated code is already perfect. Try it out.

        • If you're only interested in the highest level folder do a @folders = split '/', $path for every path found by your glob and just take $folder[$index], where index is the number of folders in the "head" part.

          E.g 4 for "../../dir1/dir2/" will give you the next directory after "dir2".

        HTH! =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Q regarding glob
by TomDLux (Vicar) on May 16, 2013 at 19:05 UTC
    use feature 'say'; use Cwd; my $origdir = cwd(); chdir '../../dir1/dir2'; for my $element( glob '*/dir3/filename' ) { say $element; }

    Alternately, can you do ...?

    my $suffix = '/dir3/filename'; for my $element( glob '*' ) { say $element . $suffix; }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Q regarding glob
by hdb (Monsignor) on May 17, 2013 at 09:34 UTC

    So you are looking for the names of directories in dir2 that have subdirectories called dir3 and contain files called filename? Here it is:

    use strict; use warnings; foreach my $element (glob "dir1/dir2/*/dir3/filename"){ print "$element: "; $element =~ m!dir1/dir2/(.*)/dir3/filename!; print "$1\n"; }

    Comment: I left the ../../ out as it would not fit my sample dir structure I had to create. Without testing it, I would still say that you do NOT need to change the regex to make it work for your situation.

      Works like a charm. Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found