Using Ruby to find a file with changing directory - ruby

I'm trying to find a file in which the directory will change its name with upcoming versions, so an example could be that it is located under /opt/here/test-1.44/bin/progname and will follow the format same time.
I'm looking to do something like if File.exist?("/opt/here/test-*/bin/progname") but is that the correct format? When searching around I'm also seeing references to using Dir, so would it be something like if Dir['/opt/here/*'.select { |f| f =~ /progname/} then ?
Thanks!

Do
Dir.glob("/opt/here/test-*/bin/progname").empty?
Use any? instead of empty? if you want true when there is such file.

Related

Pathname with Regex

I would like to know how to use Regex when instantiating a new Pathname.
I am instantiating a Pathname and passing it to FileUtils#rm_rf to delete the file. The problem I am trying to solve is to remove files that have a certain name without regard to extension:
See this contrived example:
target = Pathname.new(["#{#app_name}/#{#file_name}"])
FileUtils.rm_rf(target)
#file_name does not include extensions such as .rb or html.erb, but I would like to match all files with name equal to #file_name no matter what extensions they use.
My initial approach was to use Regex. But how can I use it, or any other suggestions?
You can use Dir.Glob like this:
Dir.glob("#{#app_name}/#{#file_name}.*").each { |f| File.delete(f) }
See more on that at http://ruby-doc.org/core-2.2.1/Dir.html#method-c-glob

check file exists or not using regular expression in ruby

I have a folder which contains many files. Now I have to check file with specific name exists or not.
For checking file existence I have used File.exists? method. For example
File.exists? ("/home/ubuntu/vga2usb.sql")
=> true
but sometimes filename contains timestamps (e.g. 20140707_vga2usb.sql). I think I have to use regular expression for it.
So I tired with
File.exists?("/home/ubuntu/*_vga2usb")
=> false
Is there any other way for checking file existence.
Thanks in advance.
Try this:
!Dir.glob("/yourpattern/*.sql").empty?
You can use Dir or Dir.glob http://ruby-doc.org/core-2.1.2/Dir.html
Here:
Dir["/home/ubuntu/*_vga2usb"]
Dir.glob("/home/ubuntu/*_vga2usb")

Ruby - Search and collect files in all directorys

I'm trying to search for a certain file type within all directories on my unix system using a ruby script. I understand the following code will search all files ending with .pdf within the current directory:
my_pdfs = Dir['*pdf']
As well as:
my_pdfs = Dir.glob('*.pdf').each do |f|
puts f
end
But how about searching all directories and sub-directories for files with the .pdf extension?
Check out the Find module:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/find/rdoc/Find.html
Using Dir.glob is less than ideal since globbing doesn't handle recursion nearly as well as something like find.
Also if you're on a *nix box try using the find command. Its pretty amazingly useful for one liners.
Maybe something like:
pdfs=Dir['/**/*.pdf']
?
Not using Linux right now, so don't know if that will work. The ** syntax implies recursive listing.

Look for a configuration file with Ruby

I written a Ruby tool, named foobar, having a default configuration into a
file (called .foobar).
When the tool is executed without any params, the configuration file's params
can be used: ~/.foobar.
But, if the current tool's path is ~/projects/foobar and if
~/projects/foobar/.foobar exists, then this file should be used instead of
~/.foobar.
That's why the way to look for this configuration file should start from the
current folder until the current user folder.
Is there a simple way to look for this file?
cfg_file = File.open(".foobar", "r") rescue File.open("~/.foobar", "r")
Although to be honest, I almost always do two things: provide an option for a config file path, and allow overriding of default config values. The problem with the latter is that unless you know config files are ordered/have precedence, it can be confusing.
I would do this:
if File.exists(".foobar")
# open .foobar
else
# open ~/..
end

How do I get the directory where the executable is located?

I got the filename like this:
_TCHAR filename[_MAX_PATH];
GetModuleFileName(NULL,filename,sizeof(filename));
How do I remove the filename from this full path? Should I use regex?
You can use the Windows shell API function PathRemoveFileSpec to do this. Example usage is listed on the linked page.
Since you use VS++, you can use:
_splitpath and _wsplitpath functions to break apart path

Resources