http://www.perlmonks.org?node_id=755132

BJ_Covert_Action has asked for the wisdom of the Perl Monks concerning the following question:

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