Skip to content

Instantly share code, notes, and snippets.

@ccoenen
Created February 10, 2016 19:08
Show Gist options
  • Save ccoenen/628d456c34e90a09cd95 to your computer and use it in GitHub Desktop.
Save ccoenen/628d456c34e90a09cd95 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# moves files containing 002 on line 9 to a different directory.
require 'fileutils'
FileUtils.mkdir_p('with002')
FileUtils.mkdir_p('no002')
def contains_zero_zero_two?(filename)
File.open(filename,'r') do |f|
lines = f.each_line.take(9)
puts "- #{lines.last}"
return lines && lines.last.rstrip.end_with?("002")
end
end
Dir['*.txt'].each do |file|
puts "checking #{file}"
if contains_zero_zero_two?(file)
FileUtils.mv(file, "with002/#{file}")
else
FileUtils.mv(file, "no002/#{file}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment