Ruby scrip to find Dir based on last modified date

B

Bal Sidhu

Hi
Can someone here please help/guide me how I can implement the following
scenario.

Scenario:

A directory on my Mac has list of Sub-directories, What I need is Ruby
script that finds the Sub Directory that was Last Modified based on the
modified date and then copy that Sub Directory and it is content to my
Different Dir on my Mac.

Appreciate your much needed help

bal
 
R

Robert Klemme

Hi
Can someone here please help/guide me how I can implement the following
scenario.

Scenario:

A directory on my Mac has list of Sub-directories, What I need is Ruby
script that finds the Sub Directory that was Last Modified based on the
modified date and then copy that Sub Directory and it is content to my
Different Dir on my Mac.

Appreciate your much needed help

This should get you started:

irb(main):001:0> Dir["*"].select {|f| test ?d, f}
=> ["a1", "bin", "desktop", "doc", "lib", "ruby", "vimfiles"]
irb(main):002:0> Dir["*"].select {|f| test ?d, f}.sort_by {|f| File.mtime f}
=> ["lib", "vimfiles", "ruby", "a1", "doc", "bin", "desktop"]

Cheers

robert
 
B

Bal Sidhu

Robert Klemme wrote in post #960918:
Appreciate your much needed help

This should get you started:

irb(main):001:0> Dir["*"].select {|f| test ?d, f}
=> ["a1", "bin", "desktop", "doc", "lib", "ruby", "vimfiles"]
irb(main):002:0> Dir["*"].select {|f| test ?d, f}.sort_by {|f|
File.mtime f}
=> ["lib", "vimfiles", "ruby", "a1", "doc", "bin", "desktop"]

Cheers

robert


Hi Robert
Thanks for you reply, much appreciated, above command work fine, seems
it need further change, This is what I am trying to achieve,

e.g. Below is list of Directories exists in my home Dir, say my home Dir
is /home/myhome/

drwxr-xr-x 4 testuser staff 136 Oct 19 15:27 .argouml
drwxr-xr-x 7 testuser staff 238 Aug 25 15:02 .yed3
drwxr-xr-x 23 testuser staff 782 Aug 12 13:23 enli.x

using ruby script I lik to find Dir with latest time stamp and then copy
the that Dir to diff Dir e.g. /usr/bin/

I google to see if their is ruby method exist to find scan the Dir then
find the one with latest time stamp, i couln't find anything,

any thoughts ?

thanks
 
R

Robert Klemme

Robert Klemme wrote in post #960918:
Appreciate your much needed help

This should get you started:

irb(main):001:0> Dir["*"].select {|f| test ?d, f}
=> ["a1", "bin", "desktop", "doc", "lib", "ruby", "vimfiles"]
irb(main):002:0> Dir["*"].select {|f| test ?d, f}.sort_by {|f|
File.mtime f}
=> ["lib", "vimfiles", "ruby", "a1", "doc", "bin", "desktop"]
Thanks for you reply, much appreciated, above command work fine, seems
it need further change, This is what I am trying to achieve,

e.g. Below is list of Directories exists in my home Dir, say my home Dir
is /home/myhome/

drwxr-xr-x 4 testuser staff 136 Oct 19 15:27 .argouml
drwxr-xr-x 7 testuser staff 238 Aug 25 15:02 .yed3
drwxr-xr-x 23 testuser staff 782 Aug 12 13:23 enli.x

using ruby script I lik to find Dir with latest time stamp and then copy
the that Dir to diff Dir e.g. /usr/bin/

I google to see if their is ruby method exist to find scan the Dir then
find the one with latest time stamp, i couln't find anything,

any thoughts ?

Use #max_by. And look into FileUtils for the copying.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top