Ruby question-delete files by their suffix; possible?

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Ruby question-delete files by their suffix; possible?

Post 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?
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post 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
Last edited by KG_is_back on Fri Oct 28, 2016 9:43 pm, edited 1 time in total.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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!
Attachments
(ruby- delete by preffix).fsm
(588 Bytes) Downloaded 1443 times
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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?
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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
Attachments
(delete any file_green).fsm
(1.7 KiB) Downloaded 1476 times
rewiredrecords
Posts: 8
Joined: Wed Nov 27, 2019 6:11 pm

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

Post 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
Post Reply