Stat() function fails?

Discussion in 'Perl' started by Joe Cosby, May 27, 2004.

  1. Joe Cosby

    Joe Cosby Guest

    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?

    --
    Joe Cosby
    http://joecosby.com/
    I made macaroni!
    - Meryl Burbank
    Joe Cosby, May 27, 2004
    #1
    1. Advertising

  2. Joe Cosby wrote:
    > [...] , 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ürgen Exner, May 27, 2004
    #2
    1. Advertising

  3. Joe Cosby

    Joe Smith Guest

    Joe Cosby wrote:

    > 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
    Joe Smith, May 27, 2004
    #3
  4. Joe Cosby

    ! aaa Guest

    "Joe Cosby" <http://joecosby.com/code/mail.pl> wrote in message
    news:...
    >
    > 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;
    }
    ! aaa, May 27, 2004
    #4
  5. Joe Cosby

    Joe Smith Guest

    ! aaa wrote:

    > "Joe Cosby" <http://joecosby.com/code/mail.pl> wrote in message
    > news:...
    >
    >>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.


    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
    Joe Smith, May 27, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,690
    Pete Becker
    Jun 2, 2005
  2. Patrick Useldinger

    os.stat('<filename>')[stat.ST_INO] on Windows

    Patrick Useldinger, Feb 27, 2005, in forum: Python
    Replies:
    6
    Views:
    1,152
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 3, 2005
  3. Magesh
    Replies:
    3
    Views:
    428
    Gordon Burditt
    Oct 5, 2007
  4. Rolf Krüger
    Replies:
    2
    Views:
    605
    Ian Collins
    Mar 12, 2008
  5. ruck
    Replies:
    10
    Views:
    1,255
Loading...

Share This Page