3
\$\begingroup\$

It compiles, works well, solves my problem of zipping old or big log files and removes them from a hard drive. However, following this answer, I would like to know what was wrong with the code.

Dir.foreach(FileUtils.pwd()) do |f|
 if f.end_with?('log')
 File.open(f) do |file|
 if File.size(f) > MAX_FILE_SIZE
 puts f
 puts file.ctime
 puts file.mtime
 # zipping the file
 orig = f
 Zlib::GzipWriter.open('arch_log.gz') do |gz|
 gz.mtime = File.mtime(orig)
 gz.orig_name = orig
 gz.write IO.binread(orig)
 puts "File has been archived"
 end
 #deleting the file
 begin
 File.delete(f)
 puts "File has been deleted"
 rescue Exception => e
 puts "File #{f} can not be deleted"
 puts " Error #{e.message}" 
 puts "======= Please remove file manually =========="
 end
 end
 end
 end
end
asked Feb 12, 2012 at 23:59
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Ruby's indentation style is usually 2 spaces, just as a minor heads up. \$\endgroup\$ Commented Nov 3, 2012 at 1:39

1 Answer 1

4
\$\begingroup\$

My proposal, please see also the comments in the code.

require 'zlib'
MAX_FILE_SIZE = 1024 #One KB
#Use a glob to get all log-files
Dir["#{Dir.pwd}/*.log"].each do |f|
 #skip file, if file is small
 next unless File.size(f) > MAX_FILE_SIZE
 #Make a one line info
 puts "#{f}: #{File.size(f)/1024}KB, created #{File.ctime(f)} modified #{File.mtime(f)} "
 # zipping the file - each log in a file
 Zlib::GzipWriter.open("arch_#{File.basename(f)}.gz") do |gz|
 gz.mtime = File.mtime(f)
 gz.orig_name = File.basename(f) #filename without path
 #File.read should be fine for log files. Or is there a reason for IO.binread
 gz.write File.read(f) 
 puts "File #{f} has been archived"
 end
 #deleting the file
 begin
 #~ File.delete(f)
 puts "File #{f} has been deleted"
 rescue Exception => e
 puts "File #{f} can not be deleted"
 puts " Error #{e.message}" 
 puts "======= Please remove file manually =========="
 end
end

Remark:

This solution creates one gz-file per log file. Your old solution created one arch_log.gz. If there were two log-files, the 2nd would overwrite the 1st.

answered Feb 13, 2012 at 9:35
\$\endgroup\$
3
  • \$\begingroup\$ defintely sorry I didn't put require and global variables, thanks for a good answer \$\endgroup\$ Commented Feb 13, 2012 at 11:24
  • 1
    \$\begingroup\$ @DanielMyasnikov if this answered the question (made a good suggestion?) then you should mark it as the selected answer. \$\endgroup\$ Commented Feb 14, 2012 at 18:43
  • \$\begingroup\$ @knut, no there were no reason for IO.binread, I just got if from Ruby Doc library web-site (ruby-doc.org/stdlib-1.9.3/libdoc/zlib/rdoc/Zlib/GzipWriter.html) \$\endgroup\$ Commented Feb 15, 2012 at 22:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.