readdir in subdirs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • über
    New Member
    • Sep 2007
    • 31

    readdir in subdirs

    Hello
    Code:
    opendir(DIR, ".");
    	@FILENAME = readdir(DIR);
    	closedir(DIR);
    Now here u see a script that gets file names in dir the script is in, but how i could also readdir in every subdir the script directory has?
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Check out File::Find.

    --Kevin

    Comment

    • über
      New Member
      • Sep 2007
      • 31

      #3
      But can you do that without the File::Find?

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by über
        But can you do that without the File::Find?
        File::Find would be the easiest way. If you are unable to install the module, you can still download the code and see how the module does it and incorporate that code into yours. Either way, its easier.

        Regards,

        Jeff

        Comment

        • über
          New Member
          • Sep 2007
          • 31

          #5
          Ok now i have File::Find and found this script somewhere
          Code:
          use strict;
          use File::Find;
          @ARGV = '.' unless @ARGV;
          
          find sub {
              print "got one!\n" if m/\.mp3$/;
          }, @ARGV;
          How i could make this script work like the one i used before? Like how i could get the files found to go to @FILENAME

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by über
            Ok now i have File::Find and found this script somewhere
            Code:
            use strict;
            use File::Find;
            @ARGV = '.' unless @ARGV;
            
            find sub {
                print "got one!\n" if m/\.mp3$/;
            }, @ARGV;
            How i could make this script work like the one i used before? Like how i could get the files found to go to @FILENAME
            Well, instead of doing the print, you could modify it to do a push() to the array. Look up the push function on perldoc and you will see how to do it.

            Regards,

            Jeff

            Comment

            • über
              New Member
              • Sep 2007
              • 31

              #7
              kk thanks, i will use this now, but i would still like to know how u would do it without File::Find

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by über
                kk thanks, i will use this now, but i would still like to know how u would do it without File::Find
                Write a recursive function. Google would have found you a number of examples. Learn how to use search engines for your pleasure and benefit.

                Dir function can be used to list all the files in a given folder recursively.

                Comment

                • eWish
                  Recognized Expert Contributor
                  • Jul 2007
                  • 973

                  #9
                  After reading the article that Kevin linked to File::Find is looking even better. I personally like File::Find::Wan ted. However, File::Find is standard and File::Find::Wan ted is not.

                  --Kevin

                  Comment

                  Working...