Sunday, June 17, 2007

Ruby: Tk, select directory

Just quick code for selecting input/output directory#!/usr/bin/env ruby
require 'tk'
bdir=Tk.chooseDirectory('initialdir'=>'./')
puts bdir

Below an example that takes a directory and lists all txt files that contain string '_data' in their name. Than contents of each such file is copied to one output file ('allData.txt'):#!/usr/bin/env ruby
require 'tk'

bdir=Tk.chooseDirectory('initialdir'=>'./')

fout= open('allData.txt', 'w')

Dir.foreach(bdir) { |x|
next if x !~ /_data/
fpath=bdir+'/'+x
File.open(fpath).each_line {|l| fout << l}
}
fout.close