Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ruby reading files in many directories

Reply
Thread Tools

ruby reading files in many directories

 
 
Peter Smith
Guest
Posts: n/a
 
      06-04-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Damjan Rems
Guest
Posts: n/a
 
      06-05-2008
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


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Onur Gungor
Guest
Posts: n/a
 
      06-05-2008
Peter Smith 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']

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      06-05-2008

On Jun 5, 2008, at 04:43 , Onur Gungor wrote:

> Peter Smith 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


 
Reply With Quote
 
Bastian G.
Guest
Posts: n/a
 
      06-05-2008
>> 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...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple bin-directories with virtual directories? =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?= ASP .Net 0 11-09-2004 05:49 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM
how to rename 200 files in many sub-directories? Geoff Cox Perl Misc 20 08-19-2003 10:17 PM
Using virtual directories for common directories (scripts, images, styles, etc.) Jeffry van de Vuurst ASP .Net 2 07-30-2003 07:00 PM



Advertisments