Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found