Forum: Ruby if file exists then move

Posted by Sloan Ruby (sloan)
on 2013-02-15 00:59
I have been googling and am not sure how to approach this.  I have a
file that I need to match.  The date string in the filename varies so I
want to match any date.

/path/to/file/hist_sar01-31-2013.tar

What options do i have for matching the/any date or string.  Is there a
method for matching?

hist_sar*.tar
Posted by RKA (Guest)
on 2013-02-15 01:47
(Received via mailing list)
yes! You want to use a regex. May I suggest something like:
/hist_sar\d+-\d+-\d+\.tar/
Posted by Kendall Gifford (zettabyte)
on 2013-02-15 01:53
(Received via mailing list)
On Thu, Feb 14, 2013 at 4:59 PM, Sloan Ruby <lists@ruby-forum.com> 
wrote:

>
if Dir["/path/to/file/hist_sar*.tar"].empty?
  # code to run if no files matching pattern exist
else
  # code to run otherwise
end

You could also make the pattern more specific:
http://www.ruby-doc.org/core-1.9.3/Dir.html#method-c-glob

Example:
Dir["/path/to/file/hist_sar{0,1}[0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9].tar"]
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
No account? Register here.