• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Get a list of File names on the input box

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 4 Posters 715 Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    davesexcel
    last edited by 23 Jan 2017, 21:00

    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=input

    puts a`

    1 Reply Last reply Reply Quote 0
    • F Offline
      fredo6
      last edited by 23 Jan 2017, 21:25

      You should use

      list = Dir["C:/Testfolder/*.*"].join("|")

      Fredo

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 24 Jan 2017, 10:39


        (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
        
        

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          davesexcel
          last edited by 24 Jan 2017, 13:38

          ListBoxThank 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=input

          puts a
          puts list`

          1 Reply Last reply Reply Quote 0
          • S Offline
            sdmitch
            last edited by 24 Jan 2017, 14:14

            @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=input

            puts 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")
            
            

            Nothing is worthless, it can always be used as a bad example.

            http://sdmitch.blogspot.com/

            1 Reply Last reply Reply Quote 0
            • D Offline
              davesexcel
              last edited by 24 Jan 2017, 17:02

              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=input

              puts 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)`
              
              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 24 Jan 2017, 17:42

                Dave, make it easier on yourself and e1-else.
                Use [ code ] tags:

                [code]
                  # Ruby program goes here ...
                  Indent works
                [/code]
                

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 24 Jan 2017, 17:45

                  You might try:
                  s = File.join('C:/TestFolder/Doit with Sketchup/', j)

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 24 Jan 2017, 17:47

                    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]

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    1 / 1
                    • First post
                      1/9
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement