Page 1 of 1

Ruby question-delete files by their suffix; possible?

PostPosted: Fri Oct 28, 2016 8:30 pm
by kortezzzz
Hi,

I have this code which is based tulamide's suggestion.
Code: Select all
def event i
   if i == "do_it"
      File.delete(@path, File.dirname(@path))
   end
end


This ruby code deletes a given file from the H.D. if you feed it's string input with the full file's name+location
(for instance: "C:\Users\kortezzzz\Pictures\bla bla.png") and push the trigger input. The problem is that it won't delete as many files as you need (based on their suffix) if you feed it's string input with "*.something" method like the windows "CMD" command line does (for instance: "C:\Users\kortezzzz\Pictures\*.png"). Is there any way to force ruby do it?

Re: Ruby question-delete files by their suffix; possible?

PostPosted: Fri Oct 28, 2016 9:00 pm
by KG_is_back
Yes, it can be done, actually very easily.

Code: Select all

def event(i,v,t)
if i == "do_it"
paths=Dir.glob(@path) #makes an array of all files which match shell-styled pattern (for example "C:/*.doc")
paths.each{|path| #iterates through the array and deletes files one by one
File.delete(path, File.dirname(path))

}
end
end


EDIT: I messed up the parentheses... fixed now

Re: Ruby question-delete files by their suffix; possible?

PostPosted: Fri Oct 28, 2016 9:32 pm
by kortezzzz
Thanks for the super fast reply, KG :)

For some reason, your code gives an error message on line no. 6
Seems like I'm doing something wrong. Uploaded my little schematic here for testing.

Thanks a lot!

Re: Ruby question-delete files by their suffix; possible?

PostPosted: Sat Oct 29, 2016 9:18 am
by kortezzzz
Sorry KG, but it's not working for me even after your edit. Can you please take a look at the schematic? Perhaps FS ruby doesn't support that feature?

Re: Ruby question-delete files by their suffix; possible?

PostPosted: Sat Oct 29, 2016 11:02 am
by kortezzzz
Found other cool, easy and green way to it by calling "CMD" feature (using the "Exec" primitive). This combination can do so much more and it's easy to use as well.

Thanks anyway, KG :D

Re: Ruby question-delete files by their suffix; possible?

PostPosted: Wed May 19, 2021 10:45 pm
by rewiredrecords
kortezzzz wrote:Found other cool, easy and green way to it by calling "CMD" feature (using the "Exec" primitive). This combination can do so much more and it's easy to use as well.

Thanks anyway, KG :D


Excellent! Thanks mate. Just what I needed :D