In Ruby, how can I check if a file exists using a wildcard?
Apparently this does not seem to work:
File.exists?("/folderOfFile/Filename*.ext")
Your wildcard would refer to a set of files, not a single file. You could use Dir::glob for this:
!Dir.glob('/folderOfFile/Filename*.ext').empty?
Related
How to check if the file exists in all subdirectories within chef recipe (not_if guard).
There is no specific answer other than "write some Ruby code to check what you want". You'll probably want to do a Dir glob and then compare the various outputs but maybe you just need an all? and File.exist?.
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")
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.
I have a job using get ftp files, i am using this wildcard to fetch all the xml.zip files
.*.zip
Works ok, but downloads all the files. I tried a wildcard to download only files with this pattern: .outsideXXXXXX.xml.zip (with no success).
.outside\*.zip
What i am doing wrong?
Finally i found the solution:
.outside.*\.zip
FYI: pentaho pdi regexp are based on java.util.regex package. The exact syntax for creating regular expressions is defined in the java.util.regex.Pattern javadoc.
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Notice a difference between outside*\\.zip (correct) and .outside\\*.zip (incorrect)?
You need to escape ".", NOT the "*"
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