Forum: Ruby Script doesnt work as wished

B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-02 18:14
Attachment: script.txt (369 Bytes)
Hi there
I made a script that should compress and delete files but it doesn't
work as wished.
What should I change so that it can compress and delete files.

Regards
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-04 07:08
In what respect doesn't it work as expected?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-04-04 12:15
Ruby Amateur wrote in post #1185739:
> Hi there
> I made a script that should compress and delete files but it doesn't
> work as wished.
> What should I change so that it can compress and delete files.
>
> Regards

Why my last response dosen't help ? :
https://www.ruby-forum.com/topic/6879343#1185730
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 09:45
Regis d'Aubarede wrote in post #1185749:
> Ruby Amateur wrote in post #1185739:
>> Hi there
>> I made a script that should compress and delete files but it doesn't
>> work as wished.
>> What should I change so that it can compress and delete files.
>>
>> Regards
>
> Why my last response dosen't help ? :
> https://www.ruby-forum.com/topic/6879343#1185730

It did, but now I want to compress the files and it doesnt compress it.
What do I wrong?
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 10:04
Ruby Amateur wrote in post #1185751:
> It did, but now I want to compress the files and it doesnt compress it.
> What do I wrong?

For this, you need us to show the code you have written, then we have
something to discuss.

In your original posting, you uploaded *some* code, but I guess you
uploaded by mistake a wrong file, because what you uploaded is not only
syntactically incorrect (the comment mark in front of a line which is
obviously supposed to be a comment is missing), it also does not contain
anything which looks like an attempt to compress a file.

Ronald
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 10:14
Ronald Fischer wrote in post #1185752:
> Ruby Amateur wrote in post #1185751:
>> It did, but now I want to compress the files and it doesnt compress it.
>> What do I wrong?
>
> For this, you need us to show the code you have written, then we have
> something to discuss.
>
> In your original posting, you uploaded *some* code, but I guess you
> uploaded by mistake a wrong file, because what you uploaded is not only
> syntactically incorrect (the comment mark in front of a line which is
> obviously supposed to be a comment is missing), it also does not contain
> anything which looks like an attempt to compress a file.
>
> Ronald

Here's my code:

`require 'fileutils'
require 'date'
require "zlib"

data_to_compress = File.read("don_quixote.txt")
current_day = Time.now.to_datetime.jd

This will get log files for all subdirectories
Dir["\ss002207\TAAHAMO1$\Desktop\Test\test.log"].each do |file|
file_day = File.mtime(file).to_datetime.jd

if current_day - file_day >= 1
FileUtils.rm file
end
end`

What's wrong with that? ):
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 10:18
Yes, that's the one you posted.

As I said, it is syntactically incorrect (now I can even spot two syntax
errors), so I conclude that you have never executed it. Rule number one
in programming: A program which you do not execute, does not compress
anything.

Second, and again, what I said in my previous posting (I now wonder
whether you actually *read* the responses you get), I don't see anything
in this program which might compress a file.

Of course, FileUtils.rm might be considered a very radical form of
compression, but: What actually makes you think that this program
possibly *could* do a file compression?
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 10:39
Ronald Fischer wrote in post #1185755:
> Yes, that's the one you posted.
>
> As I said, it is syntactically incorrect (now I can even spot two syntax
> errors), so I conclude that you have never executed it. Rule number one
> in programming: A program which you do not execute, does not compress
> anything.
>
> Second, and again, what I said in my previous posting (I now wonder
> whether you actually *read* the responses you get), I don't see anything
> in this program which might compress a file.
>
> Of course, FileUtils.rm might be considered a very radical form of
> compression, but: What actually makes you think that this program
> possibly *could* do a file compression?

I thought the "zlip" block could compress it ):
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 10:50
You don't have a "zlib block" in your code. The only reference to zlib
is in a "require"-statement, and I hope you don't seriously believe that
requiring the zlib module would magically compress all the files which
come close to your program. I think in this case you would need a

   require 'hogwarts/zlib'

;-)

Now, seriously: It is not clear, what you want to achieve exactly, but
to start a discussion, You need not only to show what you have
programmed, but also explain what, exactly, you think the code should
do, and what it is doing instead.

Plus, please make sure that you post only code which you have at least
run once already, i.e. syntactically correct code.
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 11:13
Ronald Fischer wrote in post #1185757:
> You don't have a "zlib block" in your code. The only reference to zlib
> is in a "require"-statement, and I hope you don't seriously believe that
> requiring the zlib module would magically compress all the files which
> come close to your program. I think in this case you would need a
>
>    require 'hogwarts/zlib'
>
> ;-)
>
> Now, seriously: It is not clear, what you want to achieve exactly, but
> to start a discussion, You need not only to show what you have
> programmed, but also explain what, exactly, you think the code should
> do, and what it is doing instead.
>
> Plus, please make sure that you post only code which you have at least
> run once already, i.e. syntactically correct code.

So, yes you're right Ronald
That's the script, I created and should compress the file (log.txt).
The point is, that i have to save this in a file, I heard file.open
would help.
But i dont know, take a look at my code and tell me what's wrong:

[code=ruby]
require 'fileutils'
require 'date'
require "zlib"

current_day = Time.now.to_datetime.jd

# This will get log files for all subdirectories
Dir["Test"].each do |file|
file_day = File.mtime(file).to_datetime.jd

if current_day - file_day >= 3
FileUtils.rm file

end
#if current_day - file_day >= 1
data_compressed = Zlib::Deflate.deflate(file)
#end
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 12:26
Did you look at the description of deflate?

========
 Zlib::Deflate.deflate(string[, level])

Compresses the given string. Valid values of level are NO_COMPRESSION,
BEST_SPEED, BEST_COMPRESSION, DEFAULT_COMPRESSION, and an integer from 0
to 9 (the default is 6).
=========

So defalte works on strings not on files.

You can use deflate. Another possibility would be to use
Zlib::GzipWriter. There is a nice example in the Ruby docs
(http://ruby-doc.org/stdlib-1.9.3/libdoc/zlib/rdoc/...).
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 13:25
Ronald Fischer wrote in post #1185759:
> Did you look at the description of deflate?
>
> ========
>  Zlib::Deflate.deflate(string[, level])
>
> Compresses the given string. Valid values of level are NO_COMPRESSION,
> BEST_SPEED, BEST_COMPRESSION, DEFAULT_COMPRESSION, and an integer from 0
> to 9 (the default is 6).
> =========
>
> So defalte works on strings not on files.
>
> You can use deflate. Another possibility would be to use
> Zlib::GzipWriter. There is a nice example in the Ruby docs
> (http://ruby-doc.org/stdlib-1.9.3/libdoc/zlib/rdoc/...).

I worked on the script and the result is this:

[code=ruby]
require 'fileutils'
require 'date'
require "zlib"

current_day = Time.now.to_datetime.jd

# Holt die Logfiles
Dir["\\ss002207\TAAHAMO1$\Desktop\Test"].each do |file|
file_day = File.mtime(file).to_datetime.jd

if current_day - file_day >= 3
FileUtils.rm file

end
#if current_day - file_day >= 1
Zlib::GzipWriter.open('test.log') do |gz|
  gz.write 'test'
end

File.open('test.log', 'w') do |f|
  gz = Zlib::GzipWriter.new(f)
  gz.write 'test'
  gz.close
end
#end

end

But the point is, it does nothing.
What must I change so that the script does?
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 14:47
I find it a bit strange that you try to write the same file twice, but
for me, it works (I tried only the first version). Here is my irb
transscript:

============
$  irb
irb(main):001:0> require "zlib"
=> true
irb(main):002:0> Zlib::GzipWriter.open('test.log') do |gz|; gz.write
'test'; end
=> 4
irb(main):003:0> system('ls test.*')
test.log
=> true
irb(main):004:0> system('file test.*')
test.log: gzip compressed data, last modified: Wed Apr  5 12:44:42 2017,
from Unix
=> true
irb(main):005:0>
============

Now you say that nothing happens, i.e. it does not create the file
test.log, and it also does not produce any error message?????
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 15:03
Ronald Fischer wrote in post #1185761:
> I find it a bit strange that you try to write the same file twice, but
> for me, it works (I tried only the first version). Here is my irb
> transscript:
>

>
> Now you say that nothing happens, i.e. it does not create the file
> test.log, and it also does not produce any error message?????

Which script have you tried?
If it works its fine, but in my pc it doesnt
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 15:07
??? You can see what I have tried. I posted the transscript of my irb
session. Did you try to do exactly the same?
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 15:17
Ronald Fischer wrote in post #1185763:
> ??? You can see what I have tried. I posted the transscript of my irb
> session. Did you try to do exactly the same?

Do you mean this?
require "zlib"
Zlib::GzipWriter.open('test.log') do |gz|; gz.write'test'; end
system('ls test.*')
test.log
system('file test.*')
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 15:27
Yes, of course! It's basically your code, plus some verification that it
achieved its means. Well, for the safe side I should have deleted
test.log before.
B8c0fa903537801d9268fc77b98e0039?d=identicon&s=25 Ruby Amateur (rubyamateur)
on 2017-04-05 15:55
Ronald Fischer wrote in post #1185765:
> Yes, of course! It's basically your code, plus some verification that it
> achieved its means. Well, for the safe side I should have deleted
> test.log before.

thats funny
At my pc, the script doesnt work
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2017-04-06 06:55
> At my pc, the script doesnt work

Since it requires zlib, there may be some reasons why it may
not work on your system. It is not possible to say what the
reasons are without knowing more specifices.

On my system the script works too. It has created something
as verified by "file":

test.log: gzip compressed data, last modified: Thu Apr  6 04:54:05 2017,
from Unix

I think you have to show the exact error. For some reason
you seem to have an extremely ... difficult to understand
part on feeding back errors. Are you really running
anything at all or just claiming that you do? Because if
you would run it, you'd see some message output in most
cases...  ;)
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-06 10:30
Robert Heiler wrote in post #1185769:
> Since it requires zlib, there may be some reasons why it may
> not work on your system. It is not possible to say what the
> reasons are without knowing more specifices.

How can we verify, that Ruby was built with gzip support (and how likely
is it, that we encounter an implementation where this is not supported)?

BTW, if understand the Ruby docs correctly, even if Zlib::GzipWriter
would not be supported, Zlib::Deflate.deflate should work. Am I right
with this?

Ronald
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-06 10:49
Ruby Amateur wrote in post #1185766:
> At my pc, the script doesnt work

What does

   system('file test.*')

output on your system? What platform are you using?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2017-04-06 14:50
Ruby Amateur wrote in post #1185739:
> Hi there
> I made a script that should compress and delete files but it doesn't
> work as wished.
> What should I change so that it can compress and delete files.
>
> Regards

First, define File.compress(fn) and File.uncompress(fn) :

> gem install rubyzip

===
require 'zip'
require 'fileutils'

class File
  def self.uncompress(zfile)
    raise("file doesn't exist : '#{zfile}'")unless File.exist?(zfile)
    Zip::File.open(zfile) do |zipfile|
      zipfile.each do |entry|
        IO.copy_stream(entry.get_input_stream,entry.name)
      end
    end
  end
  def self.compress(file)
    raise("file doesn't exist : '#{file}'") unless File.exist?(file)
    return file if file =~ /.zip$/
    zfile="#{file}.zip"
    FileUtils.rm(zfile) if File.exist?(zfile)
    Zip::OutputStream.open(zfile) do |zos|
      zos.put_next_entry(file)
      IO.copy_stream(file,zos)
    end
    if File.exist?(zfile)
      FileUtils.rm(file)
      zfile
    else
      raise("fail to create zip file...")
    end
  end
end
==

Then use it, replacing File.rm by File.compress :

==
last_month= Date.today << 1
Dir.glob("*.log").select {|f| File.mtime(f).to_date<last_month }.each
{|f|
  File.compress(f)
}
==

Tested in windows 10, ruby  2.2.4p230
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.