mtime - search directory for modified time

M

Mmcolli00 Mom

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
 
S

Stefan Lang

2008/12/18 Mmcolli00 Mom said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top