By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,455 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

readdir in subdirs

P: 31
Hello
Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, ".");
  2.     @FILENAME = readdir(DIR);
  3.     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?
Oct 11 '08 #1
Share this Question
Share on Google+
8 Replies


eWish
Expert 100+
P: 971
Check out File::Find.

--Kevin
Oct 11 '08 #2

P: 31
But can you do that without the File::Find?
Oct 11 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Oct 11 '08 #4

P: 31
Ok now i have File::Find and found this script somewhere
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use File::Find;
  3. @ARGV = '.' unless @ARGV;
  4.  
  5. find sub {
  6.     print "got one!\n" if m/\.mp3$/;
  7. }, @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
Oct 11 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok now i have File::Find and found this script somewhere
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use File::Find;
  3. @ARGV = '.' unless @ARGV;
  4.  
  5. find sub {
  6.     print "got one!\n" if m/\.mp3$/;
  7. }, @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
Oct 11 '08 #6

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

KevinADC
Expert 2.5K+
P: 4,059
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.

http://www.bin-co.com/perl/scripts/dir.php
Oct 11 '08 #8

eWish
Expert 100+
P: 971
After reading the article that Kevin linked to File::Find is looking even better. I personally like File::Find::Wanted. However, File::Find is standard and File::Find::Wanted is not.

--Kevin
Oct 12 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.