Hi,
Suppose I have an Excel sheet, which has the some numbers say:
77787
45877
78985 so on...
Now I have an directory called as "D://Filehosting" in windows 7
machine. under that directory I have some 500 folders, each of them
having 120 files in it. Now I want to delete the contents of each folder
which which are 2 months older from the current date. Now the folders
are arranged something like below:
D://Filehosting/Document77787
D://Filehosting/Document45877 .. so on
Script should take the numbers as mentioned above, and accordingly find
the right directory and accordingly delete the contents.Must check if
the if the folder exists or not before content deletion approach.
what I have is only the Request number and the base directory
Filehosting. then how would I make Document77787 so on.. on the fly?
Can it be done using Ruby?
on 2013-01-10 10:01
on 2013-01-10 10:17
this deletes a file
require "fileutils"
FileUtils.rm_r(s)
this deletes all files in a given dir
Dir["Filehosting/*"].each {|s| FileUtils.rm_r(s)}
on 2013-01-10 10:20
On Thu, Jan 10, 2013 at 10:01 AM, Arup Rakshit <lists@ruby-forum.com> wrote: > having 120 files in it. Now I want to delete the contents of each folder > what I have is only the Request number and the base directory > Filehosting. then how would I make Document77787 so on.. on the fly? > > Can it be done using Ruby? Probably. I can't help you with reading the numbers from the Excel sheet but assuming you have those numbers in an Array or Set you could do something like this: require 'pathname' require 'fileutils' DELTA = 2 * 30 * 24 * 60 * 60 base = Pathname "D:/Filehosting" limit = Time.now - DELTA numbers.each do |num| dir = base + "Document#{num}" next unless dir.directory? newest = dir.mtime dir.find do |file| newest = [newest, file.mtime].max end FileUtils.rm_rf dir if newest < limit end Cheers robert
on 2013-01-10 10:23
On Thu, Jan 10, 2013 at 10:19 AM, Robert Klemme <shortcutter@googlemail.com> wrote: >> machine. under that directory I have some 500 folders, each of them >> > require 'fileutils' > > newest = dir.mtime > > dir.find do |file| > newest = [newest, file.mtime].max > end > > FileUtils.rm_rf dir if newest < limit > end PS: You can as well ignore the Excel and read directories from the file system: Pathname.glob(base + "Document*").each do |dir| ... end Kind regards robert
on 2013-01-10 11:05
Robert Klemme wrote in post #1091710: > On Thu, Jan 10, 2013 at 10:01 AM, Arup Rakshit <lists@ruby-forum.com> > wrote: > numbers.each do |num| > dir = base + "Document#{num}" > > next unless dir.directory? > > newest = dir.mtime > > dir.find do |file| > newest = [newest, file.mtime].max > end > > FileUtils.rm_rf dir if newest < limit > end > Cheers > robert Thanks Robert for your help! Now what such "#" operator is doing? Could you just guide me? Any other package which can do the same instead of "stdlib", would you refer? I would like to read file system module of Ruby.
on 2013-01-10 11:14
#{} inside "" is for automatic interpolaration ...
like "a#{1+2}b" is automatic coverted into "a3b"
on 2013-01-10 11:23
Hans Mackowiak wrote in post #1091722: > #{} inside "" is for automatic interpolaration ... > like "a#{1+2}b" is automatic coverted into "a3b" Thanks Hans for your clarifications! so for "Document#{num}" if num is 1234 then the sysntax will give me the Document#1234 => Document1234 Perfect!
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.