Forum: Ruby mtime - search directory for modified time

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2008-12-18 16:28
I am trying to extract an exact filename so that I can use mtime method.
It appears to be needing an exact filename and maybe I am getting other
artifacts when iterating through this directory. Would you know how to
fix? Please help me with this. Thanks. MC

require "fileutils"
require 'find'
require 'ftools'

Dir.entries("C:/New").each do |filename|
 if File.extname(filename) == ".txt" then
 filename.to_s
 puts File.mtime(filename) #<--gets error 'No such file or directory'
ENOENT
 puts File.mtime("C:/New/2343434.txt")#<--works by outputting modified
time
 end
end
40613e55d7082e5f08429dfb50d0680e?d=identicon&s=25 Stefan Lang (Guest)
on 2008-12-18 21:26
(Received via mailing list)
2008/12/18 Mmcolli00 Mom <mmc_collins@yahoo.com>:
> I am trying to extract an exact filename so that I can use mtime method.
> It appears to be needing an exact filename and maybe I am getting other
> artifacts when iterating through this directory. Would you know how to
> fix? Please help me with this. Thanks. MC
>
> require "fileutils"
> require 'find'
> require 'ftools'

You aren't using anything from the above three libraries,
so you can remove the requires.

> Dir.entries("C:/New").each do |filename|

filename is a plain filename without directory part.
That's why it's not found.

>  if File.extname(filename) == ".txt" then
>  filename.to_s
>  puts File.mtime(filename) #<--gets error 'No such file or directory'
> ENOENT
>  puts File.mtime("C:/New/2343434.txt")#<--works by outputting modified
> time
>  end
> end

This would be a simpler way:

    Dir["C:/New/*.txt"].each { |path|
      puts File.mtime(path)
    }

Stefan
This topic is locked and can not be replied to.