Stat() function fails?

J

Joe Cosby

I'm trying to write a script which converts a directory to html, so I
use

opendir()
readdir()
closedir()

to open a directory, read a list of file names, use the stat()
function on the file name to determine if it is a subdirectory, and
generate HTML links based on that.

I chdir to the directory I'm reading and this is succesful, and the
readdir() is pulling the correct list of file names, but when I try to
stat($filename) it fails.

There's no error message or anything, but when it executes

$st = stat($filename) or die "Can't stat file $filename\n";

it dies.

I am running Windows, I tried this on both a FAT32 directory and an
NTFS directory with the same result.

I tried CD'ing to the target directory before running the script, in
addiition to the chdir within the perl script, but no go.

Does anybody have any idea why this might be failing?
 
J

Jürgen Exner

Joe said:
[...] , use the stat()
function on the file name to determine if it is a subdirectory,

Is there anything wrong with using "-d" to test for a directory?

jue
 
J

Joe Smith

Joe said:
I chdir to the directory I'm reading and this is succesful, and the
readdir() is pulling the correct list of file names, but when I try to
stat($filename) it fails.

Works for me.

C:\Documents and Settings\jms>perl -le "opendir DIR,'.';for (readdir
DIR){$st=stat $_; print 'dir: ',$_ if -d _}"
dir: .
dir: ..
dir: .java
dir: .javaws
dir: .jpi_cache
dir: Application Data
dir: Cookies
dir: Desktop
dir: Favorites
dir: Local Settings
dir: My Documents
dir: NetHood
dir: PrintHood
dir: Recent
dir: SendTo
dir: Start Menu
dir: Templates
dir: UserData
dir: WINDOWS

C:\Documents and Settings\jms>

Reduce you problem to a short test program (24 lines or less) and post it
to comp.lang.perl.misc instead of this newsgroup.
-Joe
 
A

! aaa

Joe Cosby said:
I'm trying to write a script which converts a directory to html, so I
use

opendir()
readdir()
closedir()

to open a directory, read a list of file names, use the stat()
function on the file name to determine if it is a subdirectory, and
generate HTML links based on that.

I chdir to the directory I'm reading and this is succesful, and the
readdir() is pulling the correct list of file names, but when I try to
stat($filename) it fails.

I'd guess the chdir is the culprit. Try eliminating that step (add the
directory name onto the individual file names instead)

Below is a chunk of code which works on windows and unix
and does file and directory stuff, FYI (it's my code which
traverses a directory tree)
There's no error message or anything, but when it executes

$st = stat($filename) or die "Can't stat file $filename\n";

it dies.

I am running Windows, I tried this on both a FAT32 directory and an
NTFS directory with the same result.

I tried CD'ing to the target directory before running the script, in
addiition to the chdir within the perl script, but no go.

Does anybody have any idea why this might be failing?

--
Joe Cosby
http://joecosby.com/
I made macaroni!
- Meryl Burbank








#
# Get list of files to work on
#

my @allfiles=($switch{2});

if(-d $switch{2}) { # Is this a directory?
@allfiles=AddDir($switch{2},1); # ,1 means die if we have an opening
problem.
} # Was a directory

if($#allfiles<0) {
print STDERR qq ~Can't find any files in or matching "$switch{2}"\n~;
exit 2;
}


#
# Go through the files.
#

while($#allfiles>=0) {
my $f=shift @allfiles;
print STDERR "Processing $f\n" if($switch{'debug'});
if(-d $f) { # Directory
if(($switch{'d'})||($switch{'r'})) { # Are we recursing?
push @allfiles,&AddDir($f,0); # Yes - get all the files to
do from here as well
}
} else { # regular file - "grep" it...
&DoGrep($switch{1},$f);
}
}



#
# Return a list of all files in a given subdirectory
#
sub AddDir {
my($dir,$stoponerror)=@_;
my @ret;

chop $dir while ($dir =~ /$switch{'DirSepQ'}$/);

if(!(my $rc=opendir(DIR,$dir))) {
if($stoponerror) {
print STDERR qq ~Can't open file/directory "$dir" $!\n~;
exit 2;
}
} else {
while(my $f=readdir(DIR)) {
push @ret,$dir . $switch{'DirSep'} . $f unless(($f eq '.') || ($f eq
'..'));
}
# @allfiles=readdir(DIR);
closedir(DIR);
}
return @ret;
}
 
J

Joe Smith

! aaa said:
I'd guess the chdir is the culprit.

Nope. Failing to chdir() to the directory being processed is the culprit.
The contents of readdir() are usable as is only if the opendir() was done
on ".". For any other argument to opendir(), that argument needs to be
prepended to all of the results from readdir().

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top