Check if file matching regex, glob, or wildcard pattern exists? [duplicate] - ruby

This question already has an answer here:
Check if a file exists using a wildcard
(1 answer)
Closed 7 years ago.
I'm aware of File.exists?, but that appears to operate with strings or IO objects. What I need is the ability to use a regex, glob, or wildcard.
For example, I want to say File.exists?("/pictures/tank_man.*"), to see if an image named tank_man exists, whether it's a jpg, tif, png, whatever.

You can use Dir["/pictures/tank_man.*"]
It will return existing paths

Related

How can I access an xpath with a colon in the name? [duplicate]

This question already has answers here:
How does XPath deal with XML namespaces?
(2 answers)
Closed 2 months ago.
I am trying to access the contents of the path below:
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/rtr:altId
However, the path is not being accepted due to the colon, is there any way to escape this character or make the contents of this path accessible.
I have tried using something similar to this:
/*[name()='rtr:altId']
however I'm unsure where I would put this in the CSS and if I have formatted correctly.
Try this:
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/*[name()="altId"]
or
/newsMessage/itemSet/newsItem[2]/contentSet/remoteContent[3]/*[local-name()="altId"]

How does ASCII help explain how many bytes in a text file [duplicate]

This question already has an answer here:
Two identical files have different file size based on the way it is written from C#
(1 answer)
Closed 3 years ago.
why do text files with the same content have 2 different sizes of file?
Is it because they are not created at the same time?
The two files probably use different character encodings. See https://en.wikipedia.org/wiki/Text_file#Encoding and https://knowledgebase.progress.com/articles/Article/000057930.
You can use a hex-dump program, such as xxd to view the bytes in the text files. Then, from the bytes, you'll be able to tell if the two files use different character encodings.

ruby way - relative path [duplicate]

This question already has answers here:
How to do a safe join pathname in ruby?
(2 answers)
Closed 5 years ago.
I am using File.read('file.txt') in a ruby script. But I don't know what is the best way to create a correct relative path, I can do
File.read(File.dirname(__FILE__) + '/file.txt)
but I wonder if there a more beautiful way in ruby?
There's a number of ways, but this one is the most flexible:
File.read(File.expand_path('file.txt', File.dirname(__FILE__))
You may also want to use Dir.pwd to be relative to the current working directory in your shell.
Note that using string concatenation for paths may work, but using expand_path or join is more reliable and properly handles platform differences, like on Windows where parts are natively joined with backslash instead.

What does wc do? And how do you use it to count words in a file? [duplicate]

This question already has answers here:
Whats is the behaviour of the "wc" command?
(2 answers)
Closed 6 years ago.
I'm new to the Linux/Unix world but found this decent online resource https://linuxjourney.com that's sort of explains some general command line code. I'm running an Oracle VM with Ubuntu based variant. I'm away from my computer now so I don't know which version but I'm fairly certain it's bash4
WC (Word Count) is a simple command line utility that displays the number of words in a file. It is especially useful for text files. Word documents, Libre office documents etc are a different matter.
Simply type 'wc ' and it will output the number of words in the file. If you need more information, type 'man wc' in a terminal and it will show you the full list of options.

How to get all images inside a folder with MATLAB? [duplicate]

This question already has an answer here:
How to load all files from a directory of two different types in MATLAB
(1 answer)
Closed 6 years ago.
If I knew the extension of images (.jpg for example), then I would use the following to list all images:
images = dir([myfolder '\*.jpg']);
But what if I don't know the extension, and want it to work for a given set of extensions (let's say jpg, png and bmp). How can I do a generic function that takes a folder path as input and returns all the images with these extensions?
Thanks for any help,
Since dir() returns you a struct, you can just concatenate them together:
images = [dir('*.jpg'); dir('*.png'); dir('*.bmp')];

Resources