Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading folders and folders inside folders!

Status
Not open for further replies.

Zas

Programmer
Oct 4, 2002
211
US
Ok I've got my nice little piece of code that opens up a directory of course, and then reads the directory and searchs for all folders inside that directory and compiles a list. The only problem is it only finds . (this one) and .. (folder beneath). When I read for everything inside the directory and print them out, all the folders and files are definitely there. The code worked perfectly when I changed it to this directory, and not one up. So why/how is a better way to read directories above?

code:
opendir(USER, 'user');@READ = readdir(USER);print "@READ<br><br><br>";
foreach $f (@READ) { if(-d $f) { if(!($f=~m/\./i)) { push(@DIR, $f); } else {print "either . or .., not any of the others<br>";} } }
print "THIS BE DIR @DIR";
closedir(USER);


what returns is:
. .. achosenone agena ariale ayame aznrice B&P bear belforgus bookworm bowser brazer cacoon eitri fang_batosai gallen ghettygoku greywolf gw hiei hieidarksoul jeshua_asher jin_shinobi lamina likama lixilon ljiada83 luther luvs makaveli mindblast myattboy neo nurator oushi pleeee scott serpentmike databasep.txt databasen.txt databasel.txt shadow smorgen talic teleri email.txt databasea.txt dead.txt thaiclo toxic tusuami vauntx whitewolf ip.txt ipu.txt woodsorrel news.txt xadow zan_killer zas zealot9090 oldemail.txt soulip.txt soulnumlog.txt soulnumlog2.txt tdead.txt tkill.txt turn.txt uip.txt computer30 computer020 computer0989 computer0 computer90 computer6968 computer4865 computer923 computer77 computer8568 computer70 computer08 computer529 computer01 computer7965 computer23 computer981 computer820 computer6 computer3002 computer82 computer10 computer36 computer7 computer983 computer9031 computer21 computer530 computer25 computer3 computer631 computer7981 computer841 computer86 computer89 computer52 computer94 computer9 computer374 computer685 computer2574 computer09 computer3248 computer049 computer4344 computer56 computer67 computer54 computer1403 computer05 computer4113 computer8587 computer4 computer2118 computer0871 computer997 computer8 computer40 computer11 computer6343 computer138 computer185 computer696 computer99 computer6125 computer41 computer4119 computer697 computer7585 computer161 computer715 computer234 computer432 computer0522 computer985 computer3779 computer59 computer177 computer81 computer7498 computer7871 computer95 computer409 computer92 computer4404 computer96 computer5970 computer463 computer9494 computer5308 computer1833 computer4638 computer0360 computer5206 computer371 computer334 computer4266 computer74


either . or .., not any of the others
either . or .., not any of the others
THIS BE DIR


thanks for the help in advance.

Happieness is like peeing your pants.
Everyone can see it, but only you can feel the warmth.
 
Have a look at File::Find, I think its part of the main perl distro

HTH
--Paul
 
The problem with your code above is that the current directory isn't prefixed to the directory in that root

Code:
$dir="user";
opendir(USER, "$dir");
@READ = readdir(USER);
foreach(@READ) {print "$_\n<br>";}
foreach $f (@READ) {
  if( -d "$dir/$f") { 
    print "[$f]\n";
    if( $f ne ".") {
      print "its not '.'\n";
      if ($f ne "..")  { 
         print "its not '..'\n";
         push(@DIR, $f); 
         print "pushed $F\n";
       } else {
         print "[$f]either . or .., not any of the others<br>\n";
       }
    }
  }
}
print "THIS BE DIR<br>\n\n";
foreach(@DIR) {
  print "$_\n<br>";
}
closedir(USER);

But have a look at File::Find anyway its a lot neater than this

--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top