Search code examples
perlstat

Getting the DateModifed of a file using Stat() in Perl - Inappropriate I/O control operation


I'm attempting to read a file's datemodified value, but I've been unable to do so. I'm continually receiving "Inappropriate I/O control operation" errors. This is a windows directory structure that I'm attempting read. I've attempted to pass through the full file-path along with the file name ($outputFilePath."/".$files) to the stat() function ($! returns nothing in this case, program simply dies), as well as using a file-handle (below) with no results. Any help is appreciated.

chdir($outputFilePath);
opendir(my $dirHandle, $outputFilePath) or die "Cannot opendir $outputFilePath: $!";
my $files;
my $modTime; 
#print getcwd();
while($files = readdir($dirHandle)){
  if($files ne '.' && $files ne '..'){
    open(my $fileHandle, $files) or die "Cannot open $files: $!";
    $modTime = (stat($fileHandle))[9] or die "Cannot stat file $files: $!";
    print $files."-".$modTime."\n";
    close($fileHandle);
   }
}
closedir($dirHandle);

Solution

  • Perhaps the following, which uses the fileglob operator to get the list of files in a directory, will assist you:

    use strict;
    use warnings;
    use File::stat;
    
    my $outputFilePath = 'C:\Moodle\server\php';
    chdir $outputFilePath;
    
    print "$_-" . stat($_)->mtime . "\n" for <*>;
    

    Partial output:

    cfg-1292006858
    data-1324925198
    DB-1324925198
    debugclient-0.9.0.exe-1198234832
    ...
    tmp-1292006858
    www-1292006858
    xdebug.txt-1198234860
    zendOptimizer-1324925193
    

    The last line:

    print "$_-" . stat($_)->mtime . "\n" for <*>;
            ^       ^         ^              ^^
            |       |         |              ||
            |       |         |              |+ - All files ( use <*.txt> to get only text files )
            |       |         |              + - glob angle-bracket operator generates list of file names in dir
            |       |         + - Get modification time
            |       + - Stat on file
            + - File name
    

    Hope this helps!