From: "Abinoam Jr." Date: 2013-05-06T08:22:16+09:00 Subject: Re: Files management with Ruby 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 ... https://gist.github.com/abinoam/5522530 Abinoam Jr. On Sun, May 5, 2013 at 5:47 PM, sdsd asda 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/. >