Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: File::Find stat question

by igoryonya (Pilgrim)
on Nov 24, 2014 at 06:31 UTC ( [id://1108226]=note: print w/replies, xml ) Need Help??


in reply to File::Find stat question

IMHO in your code:
#Replace / with \ (my $fn = $File::Find::name) =~ tr#/#\\#; #Replace gpfs_data with nas\rdds $fn =~ s/gpfs_data/nas\\rdds/; #Push into the @allpathlisting array any paths matching the patter +n. push @allpathlisting, $fn if /[IPDLMY]\d{8}$/; # How big is it? my $fsize = (stat($fn))[7];
You modify the original file name twice, and only then, you stat the modified file name.
so, if your original path /some/path/gpfs_data/some_file.ext
you stat: \some\path\nas\rdds\some_file.ext
That's a copletley different file, that probably doesn't exist
Shouldn't you stat the file before modifying?

Replies are listed 'Best First'.
Re^2: File::Find stat question
by Anonymous Monk on Nov 27, 2014 at 06:45 UTC

    Hi There

    I moved the stat up before I've modified the path names but I'm still getting a 0 for directory size, and I've found by printing $_ I'm not actually getting the required pattern match IPDLMY\d{8}$/ at times either... I think I'm going to have to resort to not using File::Find in this instance...

    sub all { #Unless directory skip. return unless -d; # Don't recurse past folders matching the pattern. $File::Find::prune = 1 if /[IPDLMY]\d{8}$/; #How big is it? my $fsize = (stat($_))[7]; print "$fsize\n"; push @sfiles, $fsize; #Replace / with \ (my $fn = $File::Find::name) =~ tr#/#\\#; #Replace gpfs_data with nas\rdds $fn =~ s/gpfs_data/nas\\rdds/; #Push into the @allpathlisting array any paths matching the patter +n. push @allpathlisting, $fn if /[IPDLMY]\d{8}$/; #print results to the screen, comment this out in production. print "$fn\n" if /[IPDLMY]\d{8}$/;
      I'm still getting a 0 for directory size

      Does your script run on a Windows Perl (Strawberry, ActiveState or the like)?

      Windows does not have a stat() system call. Perl emulates it, but not perfectly. I assume - without looking at the perl sources - that the struct stat is simply zeroed out and then filled depending on what the Windows API functions return. For directories, size is simply not touched and stays 0.

      Note that FAT and NTFS are very different from filesystems found on Unix systems, so having a "size" field for a directory may not make any sense at all.

      Also note that the "size" field is not at all related to the size of the files in the directory. On Unix systems, there is -- depending on the filesystem type -- a relation to the highest number of files in the directory over the lifetime of the directory, and maybe the length of the filenames. If you want the sum of the file sizes, calculate it by calling stat for each file in the directory.

      POSIX leaves the meaning of the st_size field undefined for directories. It is defined for regular files, symlinks, shared memory objects, and typed memory objects, but not for directories. ("For other file types, the use of this field is unspecified.") So, depending on your operating system, stat() may legally return 0 or just garbage for the size of a directory.

      My result for stat on a directory on Win7 NTFS and FAT, Strawberry Perl 5.14.2 is 0, too.

      On Linux 3.10.17, perl 5.18.1, ext3, ext2, procfs, sysfs, tmpfs, the results vary from 0 (procfs, sysfs) to 12288 (heavily used directory on ext3), matching the results from ls. The directory size of 0 on procfs is documented in stat(2), sysfs behaves in the same way.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Thanks Alexander for your reply, I'm using the following version of Activestate Perl. This is perl 5, version 16, subversion 2 (v5.16.2) Unfortunately I'm limited to what the I.T Dept installs and limited to what modules they have installed. I'll give your idea a go and add up all the file sizes in the directories, I assume this will be slower than a stat on a dir?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1108226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found