How to open a directory and print out dates of all the files?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • new bie
    New Member
    • Nov 2010
    • 1

    #1

    How to open a directory and print out dates of all the files?

    Need to write a perl script to open a dir and print out the date of all files last modified. This is what I have but keep getting error...

    use File::stat;
    open "D,"$ARGV[0]") or die "Can't open dir\n";

    while(defined($ file = readdir D))
    {
    next if ! -T $file;
    $last_mod = (stat($file))[9] or die "Can't stat\n";
    print"$file:\t$ last_mod\n";
    }

    Keep getting "Can't stat"!!! What did I do wrong??? Any help is appreciated....
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    new bie,

    If you read the File::stat module documentation, you'll see that there are problems with the -T operator. I don't know if this is what is affecting you or not.

    Are you, perhaps, running on a Windows box, rather than a Linux box?

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Curse, what am I saying. Even if -T fails, you're still failing to stat(), aren't you?

      Can you add the file name that's causing problems?
      Code:
      use File::stat;
      open "D,"$ARGV[0]") or die "Can't open dir\n";
      
      while(defined($file = readdir D))
      {
        next if ! -T $file;
        $last_mod = (stat($file))[9] or die "Can't stat '$file'\n";
        print"$file:\t$last_mod\n";
      }
      BTW, here is the man page from perl.org.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        The open function is used to open a file, not a directory. You need to use opendir.

        perldoc -f open


        perldoc -f opendir

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          @RonB - good catch, man! I think you just solved all his problems.

          Comment

          Working...