Files management with Ruby

Hi there

I am trying to build a script that does the following. Say there are two
directories A and B. In A, there is the file called "Today". In
directory B there are the files called "Today1", "Today2", "otherFile".
I want to loop through directory A, open the file called "Today" and
append all the files in Directory B that have the word "Today" in their
file name.

So where I am at is, I am trying to write a method first to just loop
through Directory A and just add some text to the file called "Today".
This is not working below. Can anyone tell me where I am going wrong?

Much appreciated

def append_data_to_daily_files
       Dir.entries('A').each do |file|
         next if !(file.match(/today/))
         File.open(file, 'w') {|file|
         file.write("hello")
         file.close}
     end
   end

···

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

So where I am at is, I am trying to write a method first to just loop
through Directory A and just add some text to the file called "Today".
This is not working below. Can anyone tell me where I am going wrong?

The first thing wrong is not defining EXACTLY what you mean
by "not working". However, possible 'errors':

def append_data_to_daily_files
       Dir.entries('A').each do |file|
         next if !(file.match(/today/))

1. Are you trying to match "Today" or "today"?
2. Not an error, but more idiomatic (and readable):

            next unless file.match /today/

3. That will also match e.g. 'potatodaytime' or 'Platodaylight' --
     is that acceptable? Maybe you just want a simple equality
     check?

         File.open(file, 'w') {|file|

4. Opening a file to "write" creates a zero-length file, so your
    content will *not* be appended, it will replace what was there
    before.

         file.write("hello")
         file.close}

5. The file.close is redundant; it's closed when the block passed to
    File.open ends.

HTH,

···

On Sun, May 5, 2013 at 1:47 PM, sdsd asda <lists@ruby-forum.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

Dir.entries('A').each do |file|

Returns a string with the name of file without the "A" segment of the path.
It's something like [".", "..", "Today"]

So, when opening, a quick fix would do

File.open(File.join("A", file), 'w') {|file|

Just to 'prepend' the "A" to render "A/Today"

The matching is broken also.
You're seaching for "today" expecting a match with "Today" and your
regexp is case sensitive.

Make it insensitive with

next if !(file.match(/today/i))

Another fail is the mode you're opening your file.
You're trying to append but you're opening in 'w' mode.
Fix it with...

File.open(file, 'a') {|file|

File.open with a block takes care of closing your file, so you
shouldn't bother with this.
Just strip off this line
# file.close

I think you want to append lines, am I right?
Or you'll get a file like this
hellohellohellohellohellohellohello

So, you should insert the line breaks.
File#write doesn't do this for you.
Try using puts! Yes! The same you're used to.
Just send puts to the file object.

file.puts("hello")

The complete code

def append_data_to_daily_files
  Dir.entries('A').each do |file|
    next if !(file.match(/today/i))
    File.open(File.join("A",file), 'a') do |file|
      file.puts("hello")
    end
  end
end

With "bonus" syntax highlighting ...

Abinoam Jr.

···

On Sun, May 5, 2013 at 5:47 PM, sdsd asda <lists@ruby-forum.com> wrote:

Hi there

I am trying to build a script that does the following. Say there are two
directories A and B. In A, there is the file called "Today". In
directory B there are the files called "Today1", "Today2", "otherFile".
I want to loop through directory A, open the file called "Today" and
append all the files in Directory B that have the word "Today" in their
file name.

So where I am at is, I am trying to write a method first to just loop
through Directory A and just add some text to the file called "Today".
This is not working below. Can anyone tell me where I am going wrong?

Much appreciated

def append_data_to_daily_files
       Dir.entries('A').each do |file|
         next if !(file.match(/today/))
         File.open(file, 'w') {|file|
         file.write("hello")
         file.close}
     end
   end

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

I expect to be shot for this, but you know this works amazingly well:

$ cat B/*Today* >> A/Today

···

On Sun, May 5, 2013 at 3:47 PM, sdsd asda <lists@ruby-forum.com> wrote:

Hi there

I am trying to build a script that does the following. Say there are two
directories A and B. In A, there is the file called "Today". In
directory B there are the files called "Today1", "Today2", "otherFile".
I want to loop through directory A, open the file called "Today" and
append all the files in Directory B that have the word "Today" in their
file name.

So where I am at is, I am trying to write a method first to just loop
through Directory A and just add some text to the file called "Today".
This is not working below. Can anyone tell me where I am going wrong?

Much appreciated

def append_data_to_daily_files
       Dir.entries('A').each do |file|
         next if !(file.match(/today/))
         File.open(file, 'w') {|file|
         file.write("hello")
         file.close}
     end
   end

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

Poor kittens. Here let me rubify it for you so you don't get shot:

#!/usr/bin/env ruby
`cat B/*Today* >> A/Today`

···

On Sun, May 5, 2013 at 11:24 PM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Sun, May 5, 2013 at 3:47 PM, sdsd asda <lists@ruby-forum.com> wrote:

Hi there

I am trying to build a script that does the following. Say there are two
directories A and B. In A, there is the file called "Today". In
directory B there are the files called "Today1", "Today2", "otherFile".
I want to loop through directory A, open the file called "Today" and
append all the files in Directory B that have the word "Today" in their
file name.

So where I am at is, I am trying to write a method first to just loop
through Directory A and just add some text to the file called "Today".
This is not working below. Can anyone tell me where I am going wrong?

Much appreciated

def append_data_to_daily_files
       Dir.entries('A').each do |file|
         next if !(file.match(/today/))
         File.open(file, 'w') {|file|
         file.write("hello")
         file.close}
     end
   end

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

I expect to be shot for this, but you know this works amazingly well:

$ cat B/*Today* >> A/Today