# SUBROUTINE: get_sub_dirs
Call this function when you want to return an array of folders which exists within a certain path.
All you have to do is pass this function the path you want to scan for sub directories. If any subdirectories are found, it will return an array of the directory names. This array will be unsorted; so if you want it sorted, just do that after you have the array returned. If no sub-dirs are found in the path you specified, it will return a '0' (zero). This can be easily customized to return a string instead.

EXAMPLE: Specify the path you want $path = "C:\\Bible\\Ephesians\\Chapter2\\Verses8_9";

Call the function @dir_listing = &get_sub_dirs($path);

NOTE: This function will change the program's 'present working directory' when it executes, so you may want to change it back to whatever 'pwd' you need after its done executing.
ALSO NOTE: This function will DIE if you pass it an invalid path.
sub get_sub_dirs { #Made by: cynix #Local Variable for Path Passed to This Sub local($tmp_path) = @_; #<------ OPEN THE REQUESTED DIR PATH AND GET A FILE LISTING ------ opendir(TMP_DIR, $tmp_path) || die "Cannot Open $tmp_path"; local(@tmp_dir_listing) = readdir(TMP_DIR); closedir(TMP_DIR); #----------------------------------------------------------------- #NOTE THE CODE: local($nmbr_items) = @some_array; #DOESNT WORK SO ALWAYS DECLARE IT FIRST. local($nmbr_items); $nmbr_items = @tmp_dir_listing; #Variable to Hold the "Clean" Directory Listing local(@tmp_clean_listing); #Variable to Hold Index Count for "Clean" Listing local($nmbr_clean_items) = 0; #INIT THE "Clean" DIR listing to "empty" #YOU CAN MODIFY THIS VARIABLE TO RETURN WHATEVER YOU #WANT IN CASES WHERE THERE WAS NO DIRECTORY FOUND $tmp_clean_listing[0] = 0; #Variable to Hold a Directory Index String local($item); #Change the Working Directory, so PERL can do File Test Operations chdir $tmp_path; #Scan Through Array, Excluding the Notorious '.' & '..' for ($i=2;$i<$nmbr_items;$i++) { $item = $tmp_dir_listing[$i]; #Test $item and See if it Really is a Dir if (-d $item) { #ITEM IS A DIR -> SO EXTRACT IT TO THE CLEAN ARRAY $tmp_clean_listing[$nmbr_clean_items] = $tmp_dir_listing[$i]; $nmbr_clean_items = $nmbr_clean_items + 1; } else { #DO NOTHING::ITEM IS NOT A DIR } } return(@tmp_clean_listing); }

In reply to Return an Array of Sub-Dir Names by cynix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.