# File question

**URL:** <https://www.ruby-forum.com/t/file-question/141018>\
**Category:** Ruby\
**Created:** [June 20, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/file-question/141018 "2008-06-20T20:31:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 20, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/file-question/141018/1 "2008-06-20T20:31:52Z")

</div>

File name: some\_random\_report.2008-06-20.csv

I know which folder this file will be in, I just don’t know what the  
exact file name is. It should look similar to what I have above, except  
everyday the date could be different. How would I search the folder for  
the file? Also, I want my program to be able to open a new document  
called “File name\_extra.txt”. How do I capture the File name into a  
string, once I’ve figured out which file to open in the first place?

Thanks!!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 20, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/file-question/141018/2 "2008-06-20T20:56:11Z")

</div>

On Fri, Jun 20, 2008 at 11:30 AM, Justin To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> File name: some\_random\_report.2008-06-20.csv

this should get you started

folder\_name = “/path/to/folder”  
reports = Dir[“#{folder\_name}/some\_random\_report\*.csv”]

Dir[] uses file globbing, not regexp matching. If you want the latter,  
try Dir[“#{folder\_name}/\*.csv”].grep(/pattern/)

martin

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 21, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/file-question/141018/3 "2008-06-21T18:13:10Z")

</div>

Thanks for the detailed explanation Bryan. I’ve figured out how to use  
it now, but I’m still stuck on how to check for the latest file:

For instance,

some\_file.2008-06-02.csv  
some\_file.2008-06-03.csv

Now I want to traverse the folder and open up the latter file. If it is  
a viable solution, is there a command that returns the most recent file  
with the name pattern “some\_file.\d{4}-\d{2}-\d{2}.csv”

Thanks so much!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 21, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/file-question/141018/4 "2008-06-21T18:29:33Z")

</div>

Hmm, well they’re named nicely, so you can probably do something like

File.open(Dir[‘some\_file.\*.csv’].sort.last) do |f|  
puts f.read # print out the contents of f  
end

if you’re confident that the dates in the names are accurate.  
If you want to be more pedantic, you can check by the file’s ctime  
(which is when the directory’s info for that file was changed) or  
mtime (which is when the file itself was last changed.

latest = Dir[‘some\_file.\*.csv’].sort\_by { |f| File.ctime(f) }.last

(Use Dir.glob instead if you need to). That’s basically just sorting  
the filenames based on the file’s timestamp (ascending), and taking  
the last one.

JJ

2008/6/21 Justin To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 21, 2008, 11:39am UTC](https://www.ruby-forum.com/t/file-question/141018/5 "2008-06-21T11:39:19Z")

</div>

Dir::glob is a bit more powerful than Dir::[], and it can take a  
block, to which the filename gets passed. ri Dir::glob has pretty  
comprehensive documentation on this.

Dir.glob(“/path/to/files/_._-_-_.csv”) do |filename|  
File.open(“name\_extra.txt”, “w+”) do |file|  
file.puts filename  
end  
end

But if you need to mangle the filename, then, as Martin said, you can  
run a regexp on the string once you have it.

2008/6/20 Justin To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
