Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

-d file test seems to be lying

Status
Not open for further replies.

Mthales

Technical User
Feb 27, 2004
1,181
GB
I know this sounds odd but it's got me really stuck. I have the following code in a subroutine called remove_CVS_copies. It's meant to progress recursively down a directory structure and delete all the directories it finds called "CVS."
But it does not seem to be detecting directories correctly.

Code:
 opendir(DIR, $start) or die "can't opendir for $start: $!\n";

   while (defined($file = readdir(DIR)))
   {
        # check all  directories
        if( -d $file ){
                if( $file eq "CVS" ){
                  rmtree($start ."/".$file) or die "unable to rmtree $start/$file: $!\n";
                }
                elsif($file ne "." && $file ne ".."){
                        print "# recurse down into the directory $file\n";
                        remove_CVS_copies($start ."/". $file);
                }
        }
        elsif( $file eq "CVS" ){
                print "it thinks CVS is a file!\n";
                rmtree($start ."/". $file) or die "unable to rmtree $start/$file: $!\n";
        }
   }
   closedir(DIR);
I get the message "it thinks CVS is a file" a lot and I never get the message "# recurse down into the directory $file".
Can anyone please offer any advice?

--------------------------------------
My doctor says that I have a malformed public-duty gland and that I am therefore excused from saving universes.
 
You're misinterpreting what's happening there. It's not telling you that CVS is a file (you'd need the -f file test for that). Rather, it's just telling you that CVS is not a directory.

The reason is because you're reading the names of the contents of the directory whose name is in $start. Then you're looking for those filenames in the current directory, rather than $start.

You want: if ( -d "$start/$file" ) { ... }

Or even better, use File::Spec to join the filename to the path.
 
D'oh! thanks very much ishnid I can't believe I made that dumb mistake [blush]

--------------------------------------
My doctor says that I have a malformed public-duty gland and that I am therefore excused from saving universes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top