CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
-d, -f directory, file checking problem

 



Newton
Novice

Sep 30, 2008, 2:01 PM

Post #1 of 5 (614 views)
-d, -f directory, file checking problem Can't Post

Hi,
I am attempting to batch access the contents of a group of directories (within a parent directory) but am having trouble getting the -d directory testing flag to work. A snippet of my code follows:

opendir(DIR,$DirName);
while(my $SubDir = readdir(DIR)) {
next if $SubDir =~ /^\./;
if (-d $SubDir) {
... work on contents of sub-directory
}
}

The problem is that my subdirectories in DIR are not being seen as directories and I can't go on. I have specified a full path to $DirName and the -d test still doesn't work.
Thanks in advance for any help,
N.


KevinR
Veteran


Sep 30, 2008, 3:18 PM

Post #2 of 5 (610 views)
Re: [Newton] -d, -f directory, file checking problem [In reply to] Can't Post

This should work:


Code
if (-d "$DirName/$SubDir") {


remove the / if its already on the end of $DirName.

But that will only work for one level of recursion into the parent directory. If you need to recurse all subdirectories you might want to look into File::Find.
-------------------------------------------------


shawnhcorey
Enthusiast


Sep 30, 2008, 3:21 PM

Post #3 of 5 (610 views)
Re: [Newton] -d, -f directory, file checking problem [In reply to] Can't Post


In Reply To
Hi,
I am attempting to batch access the contents of a group of directories (within a parent directory) but am having trouble getting the -d directory testing flag to work. A snippet of my code follows:

opendir(DIR,$DirName);
while(my $SubDir = readdir(DIR)) {
next if $SubDir =~ /^\./;
if (-d $SubDir) {
... work on contents of sub-directory
}
}

The problem is that my subdirectories in DIR are not being seen as directories and I can't go on. I have specified a full path to $DirName and the -d test still doesn't work.
Thanks in advance for any help,
N.


You specified the full path for $DirName but not $SubDir.

Code
opendir(DIR,$DirName); 
while(my $SubDir = readdir(DIR)) {
next if $SubDir =~ /^\./;
if (-d "$DirName/$SubDir") {
... work on contents of sub-directory
}
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Newton
Novice

Sep 30, 2008, 3:47 PM

Post #4 of 5 (606 views)
Re: [KevinR] -d, -f directory, file checking problem [In reply to] Can't Post

Hi,
Thanks alot for the feedback KevinR and shawnhcorey. Your suggestions worked. And thanks for the explanation.
N.


KevinR
Veteran


Sep 30, 2008, 5:31 PM

Post #5 of 5 (602 views)
Re: [Newton] -d, -f directory, file checking problem [In reply to] Can't Post

You're welcome
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives