Announcement

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

  • Dropping file handles

    Dear statalist,

    I am trying to append a list of files using this code

    Code:
    local tempdir "mytempdir"
    cd "`tempdir'"
    ! dir file_output_* /a-d /b >filelist.txt
    
    file open myfile using "filelist.txt", read
    file read myfile line
    
    use `line'
    save "mydir\output_all", replace
    *save master_data, replace
    file read myfile line
    
    while r(eof)==0 {
    append using `line'
    file read myfile line
    }
    
    file close myfile
    Which worked the first time I did it, but then it started giving me the error

    Code:
    file handle myfile already exists
    I tried with
    Code:
    macro drop myfile
    but the error persists. How can I effectively drop a file handle? Any help would be much appreciated. Thank you!

    Simone
    Last edited by simone p; 10 Aug 2015, 09:38.

  • #2
    Simone: welcome to the Forum. Have a look at help macro and entries relating to e.g. tempfile. I'm guessing this might help you. However it is unclear to me what you are ultimately trying to do. What sorts of files are you trying to read into Stata and then appending to each other? [And why use file open to open "myfile" rather than some other command such as import delimited or ...]

    PS the Forum FAQ requests participants to register using their full names (firstname, lastname). It's simple and fast to re-register: again, see the FAQ. Thank you.

    Comment


    • #3
      Thank you for your response Stephen. The code produces what I expect if I run it with
      Code:
      macro drop myfile
      at the end, but the same command
      Code:
      macro drop
      does not seem to get rid of file handles used at earlier times, which puzzles me. If anyone has an idea of why this might happen or what I am doing wrong here I would be very happy to hear about it.
      Stephen, what I am trying to do is to append a bunch of files from a specific directory. I don't use file open as I don't exactly know the file names as they are named using the levels of a variable. I was not able to find a quicker way to do this, although of course I am not denying there might be one.

      Comment


      • #4
        Let me suggest that instead of attempting to process the output of the system dir command, you can create a macro with the list of filenames using the macro extended function dir (see help extended_fcn which is linked to from help macro). I'm not quite sure I understand your dir command, so let me just show the following example from my Mac.
        Code:
        . ! ls "joinby problem"
        
        Data_070514_c.dta
        job31e.log
        t1mna_a.dta
        
        . local files : dir "joinby problem" files "*.dta"
        
        . display `"`files'"'
        "Data_070514_c.dta" "t1mna_a.dta"
        I could then process these files one at a time using
        Code:
        foreach file of local files {
        ...
        }
        Last edited by William Lisowski; 10 Aug 2015, 12:16.

        Comment


        • #5
          Thank you very much William, I will try your solution as soon as I can.
          I have taken the dir command from this Stata FAQ UCLA page http://www.ats.ucla.edu/stat/stata/f...many_files.htm, where it is described as creating "a list of all files with the extension .dta in the current directory, and saves that list to d:\filelist.txt", although in my case I wanted to obtain a list af all files beginning in "file_output_".

          Comment


          • #6
            Have a look at the user-written package filelist, available at SSC.
            Code:
            ssc d filelist

            Comment


            • #7
              FYI - just adding clear * before the block of code at UCLA Stata FAQ list works.

              Comment

              Working...
              X