Search code examples
ruby-on-railsrubyregexrakerequire

create file in different directories with ruby


I'm new to ruby. Actually I'm trying to create an empty file "myfile.txt" in each of the following directories:

../../../../../TESTS/Test_A/myTest_A/
../../../../../TESTS/Test_B/myTest_B/
../../../../../TESTS/Test_C/myTest_C/
../../../../../TESTS/Test_D/myTest_D/

As you can see, the name of the Top directory is "TEST" and than after this, every directory have a different name but starts with "Test_", and than each "Test_*" directory contains only one directory and there my file should go in. I'm trying something like this:

require 'pathname'

def create_myFile
  pn = Pathname.new('../../../../../TESTS/Test_*/**')
  myFile = File.new("#{pn}/myFile.txt", "w+")
end
create_myFile

It doesn't work. Any suggestions?


Solution

  • Pathname does not accept wildcards. Dir#[] does:

    Dir['../../../../../TESTS/Test_*/**'].each do |dir|
      File.new File.join(dir, 'myFile.txt'), 'w+'
    end