Hello all,
I have a directory structure with lots of text files in it. I want to have a script that basically takes in the top level directory as a parameter, and it will read through all the directories and subdirectories and retrieve all the filenames.
I thought that the best way to tackle this is to use a recursive function to do the work of stepping through the directory structure. I'm not sure if there is a built in function that can do this sort of thing.
Here is what I have done so far:
As of now, this sort of works. What it does is it reads the root directory, and then it will do recursion only for the first directory it finds, and it will ignore the rest. For example, say we have the following:
C:\ (this is passed as a parameter)
C:\abc
C:\Program Files
C:\ZXY
The script will be able to read those 3 directories, but it will only do the recursion on C:\abc and skip the other two. Within the recursive call of C:\abc, it will do the same thing (take first entry and skip the rest).
Can someone help me in finding the error? I tried to comment it as much as possible but I'm not sure if I was descriptive enough.
Cheers.
I have a directory structure with lots of text files in it. I want to have a script that basically takes in the top level directory as a parameter, and it will read through all the directories and subdirectories and retrieve all the filenames.
I thought that the best way to tackle this is to use a recursive function to do the work of stepping through the directory structure. I'm not sure if there is a built in function that can do this sort of thing.
Here is what I have done so far:
Code:
sub checkForDir
{
#grab directory as an input
$directory = shift;
#open the directory, and get all the content except ., .., and .svn (in case this is based on an svn checkout)
opendir( DIR, $directory );
@contents = grep !/(^\.\.?$)|(^\.svn$)/, readdir DIR;
closedir( DIR );
#now that we have the contents of the directory in @contents
foreach( @contents )
{
print( $directory . '\\' . $_ . "\n");
#attempt to check if the content is a directory or a file by trying to open it
if( opendir( SubDir, $directory . '\\' . $_ ) )
{
#if we can open it, it's a directory
#push the directory into an array for later usage;
push( @directories, $directory . '\\' . $_ );
closedir( SubDir );
#try to do recursion
checkForDir( $directory . '\\' . $_ );
}
#otherwise, opendir will fail, which means it is most likely a file or something went horribly wrong
}
foreach( @directories )
{
print( $_ . "\n" );
}
}
As of now, this sort of works. What it does is it reads the root directory, and then it will do recursion only for the first directory it finds, and it will ignore the rest. For example, say we have the following:
C:\ (this is passed as a parameter)
C:\abc
C:\Program Files
C:\ZXY
The script will be able to read those 3 directories, but it will only do the recursion on C:\abc and skip the other two. Within the recursive call of C:\abc, it will do the same thing (take first entry and skip the rest).
Can someone help me in finding the error? I tried to comment it as much as possible but I'm not sure if I was descriptive enough.
Cheers.