Save-Point
Checking if a file exists without knowing the extension? - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Games Development (https://www.save-point.org/forum-4.html)
+--- Forum: Code Support (https://www.save-point.org/forum-20.html)
+--- Thread: Checking if a file exists without knowing the extension? (/thread-3527.html)



Checking if a file exists without knowing the extension? - PK8 - 05-29-2011

Is there a way to check if a file exists without having to know the file extension?

I'm trying to check if a sound effect specified in a setting exists, but I really don't want to have to create a conditional like this: /lazy
Code:
if File.exist?('Audio/SE/'+setting+'.ogg') or File.exist?('Audio/SE/'+setting+'.wav') or File.exist?('Audio/SE/'+setting+'.mp3') or File.exist?('Audio/SE/'+setting+'.mid') or File.exist?('Audio/SE/'+setting+'.wma')
# Something happens
end



RE: Checking if a file exists without knowing the extension? - MGC - 05-29-2011

You should use the Dir.glob method that returns a list of found files depending on a certain pattern.

If you really don't know the extension :
Code:
if Dir.glob("Audio/SE/" + setting + ".*").length > 0
  # Something happens
end

or with a list of extensions names :
define a constant :
Code:
AUDIO_EXT = ".{ogg,wav,mp3,mid,wma}"
then just use :
Code:
if Dir.glob("Audio/SE/" + setting + AUDIO_EXT).length > 0
  # Something happens
end