Announcement

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

  • Deleting .txt files after import loop

    Hello Statalist,

    I have 234 .txt files in a folder that I have written a loop to import and save as stata files. I would like to modify the code to delete the .txt files after import but am having trouble getting the code to work. What I have thus far is:

    Code:
    clear
    cd "Z:\Data - Copy\DEED (transactions)\Import Test"
    
    local myfilelist : dir . files "*.txt"
    
    foreach x of local myfilelist {
      import delimited using `x', delimiters("|") case(preserve)
      local x = subinstr("`x'",".txt","",.)
      save "`x'", replace
      clear
              }
    That codes does its job quite well but I am unclear on what modifications to make to it to get it to delete the .txt files.

    Any help would be greatly appreciated.

    Best,
    Joseph

  • #2
    Code:
     help shell
    seems to be the thing you are looking for.

    Comment


    • #3
      Try

      Code:
      help erase

      Comment


      • #4
        This ended up being much simpler than I imagined. The code I utilized is as follows, for future search and use by others in need (hopefully):

        Code:
        local myfilelist : dir . files "*.txt"
        foreach x of local myfilelist {
                      import delimited using `x', delimiters("|") case(preserve)
                      local x = subinstr("`x'",".txt","",.)
                      save "`x'",replace
                      clear
                      rm "`x'.txt"
                  }
        All the best.
        Joseph

        Comment

        Working...
        X