From: Clement Ow Date: 2008-04-23T18:01:42+09:00 Subject: Re: Extract date from filenames using regex > Can you post the smallest example that fails? Do you have files with > different date formats? > > Jesus. delete_date = DateTime.now - delete regexp = Regexp.compile(/(\d\d)(\d\d)(\d\d\d\d)/) fileData = Struct.new(:name, :size) deleted_files = [] folders.each do |folder| Dir.glob(folder+"/*") do |file| puts match = regexp.match(File.basename(file)) if match file_date = DateTime.strptime(match[1] , fmt='%d%M%Y') size = (File.size(file))/1024 if delete_date > file_date deleted_files << fileData.new(file,size) FileUtils.rm_r file if File.exist?(file)==false puts "Files/Folders deleted: #{file} size: #{size} KB" end #if end #if end #if end #do end #each end #if end #delFiles >>it'll show this error: c:/ruby/lib/ruby/1.8/date.rb:1536:in `new_by_frags': invalid date (ArgumentError ) from c:/ruby/lib/ruby/1.8/date.rb:1563:in `strptime' from testing.conf.rb:166:in `delFiles' >>I do have files with different date formats, but the format yyyymmdd works when i use DateTime.parse maybe because DateTime accepts this format? however if i use strptime it also cant work. Any help will be greatly appreciated =) -- Posted via http://www.ruby-forum.com/.