# Mtime - search directory for modified time

**URL:** <https://www.ruby-forum.com/t/mtime-search-directory-for-modified-time/155964>\
**Category:** Ruby\
**Created:** [December 18, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/mtime-search-directory-for-modified-time/155964 "2008-12-18T16:28:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 18, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/mtime-search-directory-for-modified-time/155964/1 "2008-12-18T16:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 18, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/mtime-search-directory-for-modified-time/155964/2 "2008-12-18T21:26:49Z")

</div>

2008/12/18 Mmcolli00 Mom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
