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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive

Status
Not open for further replies.

damann

Technical User
Jul 19, 2000
114
US
Hello all, <br><br>I'm trying to write a script that'll recursively print all the directories to the screen, then go inside those directories and do the same thing primarily, until it can't go any furthur.&nbsp;&nbsp;<br><br>This is what I have now, but his doesn't work at all, and as you can see it's not recursive, If I were to add another directory in at the very end where my opendir function ends, It would only print the directory but wouldn't list everything underneath it.&nbsp;&nbsp;So can someone help direct me or redirect me to in the right direction...<br><br>opendir (DIR, &quot;$dirname&quot;) or die &quot;Can't open: $!\n&quot;; <br>@alldir = sort grep(!/^\.¦\.[aA-zZ]/, readdir (DIR)); closedir(DIR); <br>foreach $dir(@alldir){ <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$dir&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opendir(SUB, &quot;$dir&quot;) or die &quot;Can't open: $!\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@subdir = sort grep(!/^\./, readdir (SUB)); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;closedir(SUB); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach $sub(@subdir) { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$sub&quot;; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}
 
If you are on a UNIX box, the <i>find</i> command will give you a good list of all sub dirs and files.<br><br><FONT FACE=monospace><br>$cmd = 'find /some/path/&nbsp;&nbsp;-name&nbsp;&nbsp;&quot;*&quot; ';<br>@files = system($cmd);<br>foreach $file (@files) { print &quot;$file\n&quot;; }<br></font><br><br>This will supply a list of all files and dirs found below /some/path/.<br><br>'hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Im not on a unix box.&nbsp;&nbsp;Im on an NT machine.&nbsp;&nbsp;So how do I go about doing htat now.<br><br><br>
 
There is a 'File::Recurse' module that does that.....from the ActiveState html version of the perl man pages.....<br>quote --<br>The function takes two parameters a function reference and a directory. The function referenced by the first parameter should expect to take one parameter: the full path to the file currently being operated on.&nbsp;&nbsp;This function is called once for every file and directory under the directory named by the second parameter.<br>For example:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recurse(\&func, &quot;/&quot;);<br><br>would start at the top level of the filesystem and call ``func'' for every file and directory found (notincluding ``/'').<br><br>--end quote<br><br>See the perl manpages for more details... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
try this:<br><br>sub recurse_dirs<br>{<br>&nbsp;&nbsp;my ($dir,$level) = @_;<br><br>&nbsp;&nbsp;opendir(DIR, &quot;$dir&quot;) or die &quot;Can't open: $!\n&quot;; <br>&nbsp;&nbsp;my @alldir=sort grep(!/^\.¦\.[aA-zZ]/,readdir(DIR));&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;closedir(DIR); <br><br>&nbsp;&nbsp;$str = &quot;% $levels&quot;;<br>&nbsp;&nbsp;$dir = sprintf($str,$dir);<br><br>&nbsp;&nbsp;$level++;<br>&nbsp;&nbsp;foreach $dir (@alldir) {recurse_dirs($dir,$level);}<br>}<br><br>recurse_dirs($dirname,0);<br><br>I'm not sure if that sprintf hack will work the way I want (print spaces before the directory in order to indent them correctly).&nbsp;&nbsp;But, you at least get the idea of how to write a recursive subroutine.<br><br>Sincerely,<br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top