Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • User selection of multiple files for macro

    Hi friends

    Is there any way to allow selection of multiple files to pass to a macro that I can iterate through, perhaps something like tk_choose.files in the R tcltk library? I'd then iterate through the file names in the macro to create an aggregate.

    Due to a variety of issues with changes in file names, etc. the most desirable way to implement this, I think, is using something like fopen but allowing a user to select multiple files which could be passed to a macro. Relying on standard file names or a repository directory where new files are placed and all are imported using a macro extended function isn't a viable option.

    Right now, I'm iterating through file selections using fopen to construct the macro based on the _rc value with each iteration and it works as expected but I was wondering if there was a more elegant solution.

    Code:
    loc exit=0
    while `exit'==0{
    capture window fopen D_dta "Select a dataset to use:" "Stata Data (*.dta)|*.dta|All Files (*.*)|*.*" dta
    loc exit=_rc
    loc files `files' $D_dta
     }

  • #2
    Some might suggest how to program this using dialog -help dialog_programming-, If using python is an option:
    Code:
    python:
    
    from sfi import Macro 
    from tkinter import filedialog
    
    files=filedialog.askopenfilenames() 
    
    Macro.setLocal("files", " ".join(files) )
    
    end

    Comment


    • #3
      Thanks very much Bjarte, I'll give the python option a try.

      I subsequently discovered the native option using the loop code I provided in the opening message doesn't perform as expected if there are spaces in the file names and it also introduces a blank element to the end of the aggregated macro on exit when _rc==601. The following code addresses this.

      Code:
      loc exit=0
      while `exit'==0{
      capture window fopen D_dta "Select a dataset to use:" "Excel File .xlsx" xlsx
      loc exit=_rc
      if `exit'==0{
      local data: di `""$D_dta""'
      loc files `"`files' `data'"'             
      }
      }
      
      loc index=0
      foreach lev of loc files{
          di "`lev'"
          import excel "`lev'", sheet("IP") firstrow clear case(lower)
              if `index'==0{
                  save file.dta, replace
              }
          else{
              append using file.dta 
              save file.dta, replace
          }
      loc index=`index'+1
      }
      Last edited by cammcdermaid; 22 Apr 2022, 10:10.

      Comment


      • #4
        To address spaces in filenames, and removing the Tk window:
        Code:
        python:
        
        from sfi import Macro
        from tkinter import filedialog  
        
        # close Tk window
        root = tkinter.Tk(); root.withdraw()
        
        tuple_files = filedialog.askopenfilenames()
        
        string_files =  " , ".join(f'"{w}"' for w in tuple_files)
        
        Macro.setLocal("files", string_files)
        
        end
        
        macro dir _files
        Last edited by Bjarte Aagnes; 22 Apr 2022, 10:35.

        Comment

        Working...
        X