Checking if a file exists without knowing the extension?
#1
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
Reply }
#2
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
Some scripts :
Working on :
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Map name as save file name Whisper 9 10,195 05-01-2020, 05:35 PM
Last Post: Whisper
   Script Request - Variables Specific Save File JayRay 2 5,109 04-28-2014, 05:15 AM
Last Post: JayRay
   checking event coordinates? shintashi 4 7,569 05-28-2010, 03:34 PM
Last Post: Jaberwocky
   Can I execute a txt file from a script? deValdr 8 10,973 04-18-2010, 07:34 PM
Last Post: deValdr



Users browsing this thread: 1 Guest(s)