Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hello Monks,

I am attempting to code a subroutine that allows me to scour through a given directory and all subdirectories. This is going to be a small piece of a much larger project so, even though what I am doing within those subdirectories seems trivial, it will be changed later. Anyways, here is the folder tree I am working in:

>Search_Dir.plx
>TryMe (directory)
->I_Am_$$
->Some$$
->Not_Found
->TryMe2 (dir)
-->Copy Of I_Am_$$
-->Copy Of Some$$
-->Copy Of Not_Found
->TryMe3 (dir)
-->Copy2 of I_Am_$$
-->Copy2 of Some$$
-->Copy of Not_Found


Each of the files with the (dir) following it is a subdirectory the (dir) is just a tag posted here, it is not in the actual names of the subdirectories.

I am trying to run Search_Dir.plx to go through this file tree and print to output.txt a list of all of the filenames that contain $$ (again, seems trivial, but this is because I am building up to stuff).

So here is what the contents of Search_Dir.plx look like now:

#!/usr/bin/perl # Search_Dir.plx use warnings; use strict; open OUTPUT, ">output.txt"; print "Please input the path to a directory you would like to search: +"; chomp(my $dir_name = <STDIN>); opendir DIR, $dir_name or die "Couldn't open the specified directory: +$!"; search_dir($dir_name); sub search_dir{ my $sub_name = shift; opendir SUB, $sub_name or die "Couldn't open the specified directo +ry: $!"; while(my $file = readdir(SUB)){ next if $file eq "." or $file eq ".."; next if $file eq "output.txt"; print $file, "\n"; if(-d $file){ search_dir($file); }elsif($file =~ /\$\$/){ print OUTPUT "Possible \$\$ file found: $file\n"; } } #close SUB; }


So when I run the script I tell it to search through the TryMe directory. It seems to find the first two $$ files successfully and print them to output.txt. However, it does not seem to get into the subdirectory logic at all. That is, when I feed it the logic:

if(-d $file){ search_dir($file); }elsif($file =~ /\$\$/){ print OUTPUT "Possible LIST\$\$ file found: $file\n"; }


...the -d $file sections seems to always return false. At first I thought that the problem came from calling the subroutine from within itself. However, to troubleshoot, I just put a generic print "Here\n"; statement within that same section of the if statement (-d $file) and nothing ever printed. So I guess my final question is, why isn't my subroutine detecting the subdirectories within the file tree. Or, for that matter, why isn't the -d test returning true?

Also, as a disclaimer, I know there is a module that handles this kind of thing called File::Find. However, part of the reason I am trying to code this is just as a learning exercise. Furthermore, I want some customize-ability for where I am going to use this subroutine later. So despite there being an existing module, I would still like to work this out using file and directory handles and the readdir command myself (same goes for grep function).

Let me know if there is anything else anyone needs to assess this problem. I am working on a Windows XP machine with the strawberry perl 5.10 distribution.

Thank you in advance,
Brady

In reply to SOLVED: Subroutine to Search Subdirectories Failing at Filehandle Test by BJ_Covert_Action

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2020-10-22 07:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (225 votes). Check out past polls.

      Notices?