Script doesnt work as wished


#1

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


#2

In what respect doesn’t it work as expected?


#3

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


#4

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


#5

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?


#6

Ronald F. 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? ):


#7

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?


#8

Ronald F. 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 ):


#9

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’

:wink:

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.


#10

Ronald F. 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’

:wink:

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


#11

Ronald F. 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/Zlib/GzipWriter.html).

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?


#12

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???


#13

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/Zlib/GzipWriter.html).


#14

Ronald F. 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


#15

Ronald F. 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.
’)


#16

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.


#17

??? You can see what I have tried. I posted the transscript of my irb
session. Did you try to do exactly the same?


#18

Ronald F. 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


#19

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… :wink:


#20

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?