Announcement

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

  • erase all files that match

    Is there any way to erase, from Stata, all files that match some stem? I tried this:
    Code:
    erase "Results\Table*.xls"
    and it didn't generate an error, but it didn't erase the files either. There's no mention of a wildcard character, such as *, in help erase.

  • #2
    Why would I want to do this? Well, Table 1, Table 2, etc are files containing output accumulated incrementally the last time I ran my do-file. I'd like to delete them at the start of my do-file so that I can create new output, starting with a clean slate.

    Comment


    • #3
      -erase- only takes a single filename argument. You can instead collect the filenames in a local macro and the loop over that:
      Code:
      local table_files: dir "Results" files "Table*.xls"
      
      foreach t of local table_files {
          erase `t'
      }
      Notes:

      1. If some of the filenames contain embedded blanks, you will need to wrap `t' inside quotes.
      2. If you are on a Mac, filenames are case sensitive, so make sure the names of the files have the capitalization you showed. (In Windows, filenames are not case sensitive, so no issue.)

      Comment


      • #4
        You could always call the OS via shell. On Windows:

        Code:
        ! erase "Results\Table*.xls"
        should do it.

        You could also use extended macro functions and loop over files. Not tested:

        Code:
        cd Results
        
        local files : dir "." files "Table*.xls"
        
        foreach f of local files {
            erase "`f'"
        }
        Be careful with either approach as files are typically removed from disk, permanently; no recycle bin, no recovery.

        Comment

        Working...
        X