How to store slected file directory name on a variable?
-
Hi, I want to have the user open a directory folder (expample C:\Users\name\Desktop\GOSUFiles)...then pick a file (example: sound.wav) and click ok.
After user click ok the directory address of that file will be stored in a variable like this...
@sound = C:\Users\name\Desktop\GOSUFiles\sound.wav
How can I do this?
Thanks in advance!!!
-
Easy ..
def get_sound_file(title = "Choose a sound file") filepath = UI.openpanel( title, "*.wav" ) if filepath @sound = filepath.gsub("\\",'/') else return nil end end # methodUI.openpanel( title, "*.wav" )
On Windows the browser will open to the last path where the user selected a ".wav" file.
Windows keeps track of MRU (Most Recently Used) paths for all file extensions.If you want to put the user in a specific directory, then:
UI.openpanel( title, absolute_path, "*.wav" )If you do not want to filter by extension...
UI.openpanel( title, absolute_path )The UI file browse methods are bugged and we cannot specify multiple file extensions.

-
:: Dan Rathbun ::
Thank you so much this was exactly what I wanted!
I have another question...

Lets say I only want to choose folder and not a file...how can this be done?
Again thanks!
-
I kinda found a way, here it is...
def get_folder(title = "Choose a folder") filepath = UI.openpanel( title, "" ) if filepath file_path = filepath.gsub("\\",'/') @folder = File.dirname(file_path) else return nil end end # methodOnly draw back is that you need to pick file in other to get folder anyway...but its ok I guess.
Cheers!
-
@dan rathbun said:
@sound = filepath.gsub("\\",'/')Since / as path separator isn't hard coded I tend to use:
@sound = File.expand_path(filepath)One could also do:
@sound = filepath.gsub("\\", File::SEPARATOR)But I prefer the first.
-
@Rafael: The API
UImodule does not have a folderpicker, YET. We have filed Feature Requests for one, and fontpicker, and colorpicker, and etc., etc.For now users must pick a file within the target folder. (We are out of luck if the folder is empty.)
@thomthom said:
@dan rathbun said:
@sound = filepath.gsub("\\",'/')Since / as path separator isn't hard coded I tend to use:
@sound = File.expand_path(filepath)One could also do:
@sound = filepath.gsub("\\", File::SEPARATOR)But I prefer the first.
Your second one is safer. Yes I got lazy, but my purpose here is going from the OS to Ruby. I imagine that after tha statement the path will be used by Ruby's
DirandFilemethods.Later on... to go back to the OS I'd use
filepath.gsub("\/", File::SEPARATOR)if it is even necessary.Be careful with
expand_path()because it is DUMB!
Your example will work only because theUI.openpanelmethod returns an absolute path.When the path is a relative path,... the method simply concatenates the current working directory path and the argument path, regardless if the result is really a valid path.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement