Get a list of File names on the input box
-
The code
Dir.new('C:\\TestFolder\\').each { |file| puts file }
Will print out all the file names in the console.Is there a way to get that loop to populate the dropdown input box?
` ents=Sketchup.active_model.active_entities
#Dir.new('C:\TestFolder\').each { |file| puts file }
prompts = ["Find a file?"]
defaults = [""]list=[Dir.new('C:\TestFolder\').each { |file| puts file }]
input = UI.inputbox prompts, defaults,list, "File Name"
a=inputputs a`
-
You should use
list = Dir["C:/Testfolder/*.*"].join("|")
Fredo
-
(1) Ruby file and directory operations work best with paths using "/" which is defined as
File::SEPARATOR
.This statement will replace each occurrence of "\" OR "" OR "//" with "/":
path.gsub(/\\\\|\\|\/\//,'/')
Wrap it up in a utility method:
def slashify(path) path.gsub(/\\\\|\\|\/\//,'/') end
(2) If you wish only the filenames, without the path prepended to every filename, use:
Dir.entries("C:/TestFolder/")[2..-1].join('|')
The
[2..-1]
sub-range ignores the first two members (2
is the third member) which are"."
and".."
. The-1
means the last member.
(3) The preferred means is (because this is how the user is used to choosing files):
# In the Constant definitions at top of class or module; WIN =(Sketchup;;platform == ;platform_win rescue RUBY_PLATFORM !~ /darwin/i) # Later on in some method of your code; chosen = UI.openpanel( 'Choose a file...', 'C;/TestFolder', WIN ? 'All files|*.*||' ; '*.*' ) if chosen # nil if user cancels openpanel # chosen is an good absolute filepath # On PC, the paths will have backslashes # so "slashify" them after UI.openpanel(); path = slashify(chosen) end
-
Thank you both for the answers,
Both
list = Dir["C:/Testfolder/*.*"].join("|")
and
Dir.entries("C:/TestFolder/")[2..-1].join('|')
They list all the files, but all on one line and the list ends up in the title with a blank textbox.` ents=Sketchup.active_model.active_entities
def slashify(path)
path.gsub(/\\|\|///,'/')
end#Dir.new('C:\TestFolder\').each { |file| puts file }
prompts = ["Find a file?"]
defaults = ["a"]list = Dir.entries("C:/TestFolder/")[2..-1].join('|')
input = UI.inputbox prompts, defaults,list, "File Name"
a=inputputs a
puts list` -
@davesexcel said:
[attachment=0:i9zhoo1z]<!-- ia0 -->FileListRuby.jpg<!-- ia0 -->[/attachment:i9zhoo1z]Thank you both for the answers,
Both
list = Dir["C:/Testfolder/*.*"].join("|")
and
Dir.entries("C:/TestFolder/")[2..-1].join('|')
They list all the files, but all on one line and the list ends up in the title with a blank textbox.` ents=Sketchup.active_model.active_entities
def slashify(path)
path.gsub(/\\|\|///,'/')
end#Dir.new('C:\TestFolder\').each { |file| puts file }
prompts = ["Find a file?"]
defaults = ["a"]list = Dir.entries("C:/TestFolder/")[2..-1].join('|')
input = UI.inputbox prompts, defaults,list, "File Name"
a=inputputs a
puts list`list, like prompts and defaults, must be an array
prompts = ["Find a File"] defaults = ["a"] list = [Dir.entries('c;/users/public/test')[2..-1].join("|")] input = UI.inputbox(prompts,defaults,list,"File Name")
-
The list works very well, but the workbook is not opening, I narrowed it down to it not recognizing "s" as a string.
` ents=Sketchup.active_model.active_entities
def slashify(path)
path.gsub(/\\|\|///,'/')
end#Dir.new('C:\TestFolder\').each { |file| puts file }
prompts = ["Find a file?"]
defaults = ["a"]list = [Dir.entries("C:/TestFolder/Doit with Sketchup/")[2..-1].join('|')]
input = UI.inputbox prompts, defaults,list, "File Name"
j=inputputs j
#j = j.gsub(/[^a-zA-Z0-9-." "]/,"")puts j s='C:/TestFolder/Doit with Sketchup/' + j
puts s
excel = WIN32OLE.new('Excel.Application')
excel.Visible = true#number_of_sheets = excel.SheetsInNewWorkbook #excel.SheetsInNewWorkbook = 1 excel.Workbooks.Open(s) #workbook = excel.Workbooks.Add #excel.SheetsInNewWorkbook = number_of_sheets ws = excel.Worksheets(1)`
-
Dave, make it easier on yourself and e1-else.
Use [ code ] tags:[code] # Ruby program goes here ... Indent works [/code]
-
You might try:
s = File.join('C:/TestFolder/Doit with Sketchup/', j)
-
OH! I see your booboo: inputbox always returns an array (except when the user cancels it returns
false
. So always check the result boolean-wise.)input = UI.inputbox prompts, defaults,list, "File Name" return unless input j = input[0]
Advertisement