# Ruby reading files in many directories

**URL:** <https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555>\
**Category:** Ruby\
**Created:** [June 4, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555 "2008-06-04T23:11:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gluni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@gluni](https://www.ruby-forum.com/u/gluni)\
**Post date:** [June 4, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555/1 "2008-06-04T23:11:41Z")

</div>

Hi,

i have a question. I have a working script, that scans some txt files in  
a directory ,reads the lines and gives a few of that lines out in new  
files.  
But now, i wanted to scan the full subdirectorys for txt files. And copy  
some lines out of that files. The files are in different directorys  
belonging to the same subdirectory.

for example  
\subdirectory1\txtdirectory1  
\subdirectory1\txtdirectory2  
\subdirectory1\txtdirectory3

\subdirectory2\txtdirectory1  
\subdirectory2\txtdirectory2  
\subdirectory2\txtdirectory3  
and so on

i used from wiki the example

Dir[’\*.txt’].each do |txt|  
next unless File.file?(txt)

but this only scans the same directory the ruby file is in.  
Now i changed this in

Dir[’/nameofdirectory/\*.txt’].each do |txt|

this doesnt work.  
then i tried  
Dir[’\*.txt’].each do |txt|  
next file.directory(".")

At the end I also wanted to copy the processed files into a new  
directory.  
Its no problem with  
File.new(“newfile”, “w+”) or file.new to make new datas, but i cant  
change the directory with file.open.  
the directory should be called converted or so on and should be in  
subdirectory1\converted  
…  
I needed hours to get run the script in the same directory and spent  
also hors to get that run with different directorys. But i didnt  
finished the script.  
Thank for answers.  
Cheers  
Bastian

---

<div class="post-metadata">

**Author:** ![gluni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@gluni](https://www.ruby-forum.com/u/gluni)\
**Post date:** [June 5, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555/2 "2008-06-05T12:00:22Z")

</div>

Try this:

def scan\_file(file\_name)  
if File.ftype(file\_name) == ‘file’  
puts file\_name  
elsif File.ftype(file\_name) == ‘directory’  
Dir[file\_name + “/\*”].each { |f| scan\_file(f) }  
end  
end  
scan\_file(ARGV.first)

call: ruby test.rb /somedir

by  
TheR

---

<div class="post-metadata">

**Author:** ![gluni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@gluni](https://www.ruby-forum.com/u/gluni)\
**Post date:** [June 5, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555/3 "2008-06-05T13:44:22Z")

</div>

Peter S. wrote:

> Hi,
> 
> i have a question. I have a working script, that scans some txt files in  
> a directory ,reads the lines and gives a few of that lines out in new  
> files.  
> But now, i wanted to scan the full subdirectorys for txt files. And copy  
> some lines out of that files. The files are in different directorys  
> belonging to the same subdirectory.

try this:  
Dir[’./\*\*/\*.txt’]

---

<div class="post-metadata">

**Author:** ![gluni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@gluni](https://www.ruby-forum.com/u/gluni)\
**Post date:** [June 5, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555/4 "2008-06-05T23:25:29Z")

</div>

> > try this:  
> > …

hello thanks for the answers I will try this tomorrow.  
I helped me with a dirthy solution. The script is needed for windows, so  
i wrote a batch file, now it works but this solution is only a rescue  
aid. I am very new on ruby, so I will try and learn…

---

<div class="post-metadata">

**Author:** ![gluni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/49beb7/32.png) [@gluni](https://www.ruby-forum.com/u/gluni)\
**Post date:** [June 5, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/ruby-reading-files-in-many-directories/139555/5 "2008-06-05T20:15:24Z")

</div>

On Jun 5, 2008, at 04:43 , Onur G. wrote:

> Peter S. wrote:
> 
> > i have a question. I have a working script, that scans some txt  
> > files in a directory ,reads the lines and gives a few of that lines  
> > out in new  
> > files. But now, i wanted to scan the full subdirectorys for txt  
> > files. And copy some lines out of that files. The files are in  
> > different directorys belonging to the same subdirectory.
> 
> try this:  
> Dir[’./\*\*/\*.txt’]

well… you have to deal with the top level dir as well:

Dir[’./\*\*/_.txt’] + Dir[’_.txt’]

that’s always bugged me about “\*\*”.

You can also use find for more flexibility:

require ‘find’

txt = []

Find.find(".") do |path|  
Find.prune if File.basename(path)[0] == ?.  
txt \<\< path if path =~ /txt$/  
end
