[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby reading files in many directories

Peter Smith

6/4/2008 9:11:00 PM

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-....

12 Answers

Damjan Rems

6/5/2008 9:59:00 AM

0

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-....

Onur Gungor

6/5/2008 11:43:00 AM

0

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-....

Ryan Davis

6/5/2008 6:14:00 PM

0


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


Peter Smith

6/5/2008 9:24:00 PM

0

>> 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-....

Steve

8/11/2008 4:38:00 PM

0

On Aug 11, 12:03 pm, Stu Reid <stur...@shaw.ca> wrote:
> Holy crap. I'm gonna be in the middle of a Disneyworld trip on the 1st.
> Hmmmmm....


Well....if you're in the neighborhood...I know...umm....people.

Steve S.

Stu Reid

8/11/2008 7:18:00 PM

0

Steve wrote:
> On Aug 11, 12:03 pm, Stu Reid <stur...@shaw.ca> wrote:
>> Holy crap. I'm gonna be in the middle of a Disneyworld trip on the 1st.
>> Hmmmmm....
>
>
> Well....if you're in the neighborhood...I know...umm....people.
>
> Steve S.


The beautiful thing is we're at Disney the week before and on the 1st we
planned to check out and before hitting Seaworld for a couple of days,
spend a lazy day on a beach somewhere. Or in the middle of the Raymond
James Stadium in Tampa. Whatever's convenient.

HalS007

8/11/2008 7:28:00 PM

0

On Aug 11, 12:18 pm, Stu Reid <stur...@shaw.ca> wrote:
> Steve wrote:
> > On Aug 11, 12:03 pm, Stu Reid <stur...@shaw.ca> wrote:
> >> Holy crap. I'm gonna be in the middle of a Disneyworld trip on the 1st.
> >> Hmmmmm....
>
> > Well....if you're in the neighborhood...I know...umm....people.
>
> > Steve S.
>
> The beautiful thing is we're at Disney the week before and on the 1st we
> planned to check out and before hitting Seaworld for a couple of days,
> spend a lazy day on a beach somewhere. Or in the middle of the Raymond
> James Stadium in Tampa. Whatever's convenient.

Considering face value tickets to the Super Bowl are $750/1000 and
it's basically impossible for a member of the general public to get
one, which meant you couldn't get in the building for less than around
$2500 last year (for tickets in the far reaches of the stadium), that
would be an expensive 15 minutes. The average ticket for Colts-Bears
was just above 4 grand on the secondary market as noted in the article
below.

http://sports.espn.go.com/nfl/playoffs07/news/story?...

HalS007

8/11/2008 7:29:00 PM

0

On Aug 11, 12:27 pm, HalS007 <HalS...@aol.com> wrote:
> On Aug 11, 12:18 pm, Stu Reid <stur...@shaw.ca> wrote:
>
> > Steve wrote:
> > > On Aug 11, 12:03 pm, Stu Reid <stur...@shaw.ca> wrote:
> > >> Holy crap. I'm gonna be in the middle of a Disneyworld trip on the 1st.
> > >> Hmmmmm....
>
> > > Well....if you're in the neighborhood...I know...umm....people.
>
> > > Steve S.
>
> > The beautiful thing is we're at Disney the week before and on the 1st we
> > planned to check out and before hitting Seaworld for a couple of days,
> > spend a lazy day on a beach somewhere. Or in the middle of the Raymond
> > James Stadium in Tampa. Whatever's convenient.
>
> Considering face value tickets to the Super Bowl are $750/1000 and
> it's basically impossible for a member of the general public to get
> one, which meant you couldn't get in the building for less than around
> $2500 last year (for tickets in the far reaches of the stadium), that
> would be an expensive 15 minutes. The average ticket for Colts-Bears
> was just above 4 grand on the secondary market as noted in the article
> below.
>
> http://sports.espn.go.com/nfl/playoffs07/news/story?...

ps. I should have added that I plan on being but not because of
Bruce. I'll be there with Brett...

pps Yes, it's August and every year hope springs eternal... :)

Bill1255

8/11/2008 7:45:00 PM

0

On Aug 11, 3:27 pm, HalS007 <HalS...@aol.com> wrote:
> On Aug 11, 12:18 pm, Stu Reid <stur...@shaw.ca> wrote:
>
> > Steve wrote:
> > > On Aug 11, 12:03 pm, Stu Reid <stur...@shaw.ca> wrote:
> > >> Holy crap. I'm gonna be in the middle of a Disneyworld trip on the 1st.
> > >> Hmmmmm....
>
> > > Well....if you're in the neighborhood...I know...umm....people.
>
> > > Steve S.
>
> > The beautiful thing is we're at Disney the week before and on the 1st we
> > planned to check out and before hitting Seaworld for a couple of days,
> > spend a lazy day on a beach somewhere. Or in the middle of the Raymond
> > James Stadium in Tampa. Whatever's convenient.
>
> Considering face value tickets to the Super Bowl are $750/1000 and
> it's basically impossible for a member of the general public to get
> one, which meant you couldn't get in the building for less than around
> $2500 last year (for tickets in the far reaches of the stadium), that
> would be an expensive 15 minutes.  The average ticket for Colts-Bears
> was just above 4 grand on the secondary market as noted in the article
> below.
>
> http://sports.espn.go.com/nfl/playoffs07/news/story?...

Good ole buzz-killer Hal.

Stu Reid

8/11/2008 8:01:00 PM

0

> Good ole buzz-killer Hal.

I was checking out a Tom Petty board just before Superbowl time this
year, gathering info for the tour on-sale that happened the next day.
There was an open 'casting call' of sorts on the board, posted by the
Superbowl people, looking for folks to be the audience for the Petty
halftime spot. You had to commit a good deal of time to get in on it (I
guess you had to rehearse going "Woo Hoo!" or something for hours on end
beforehand) and obviously you couldn't stick around for the game, but
there were perks. Odds are there'll be a similar kind of offer made, I'm
betting via Backstreets.

If not, I'll just settle for riding in front of Bruce on Splash Mountain
the week before.