Announcement

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

  • Deleting files from folder in a loop

    Hi,

    I am trying to delete files from a folder in a loop using locals. The files have a number as prefix (1-2300ish) and the date when they were saved as suffix. I will get the following message: (note: file 26_2022-04-17.dta not found). However, when I manually enter the file name it works as expected, the file is deleted.



    foreach ID in `IDlist' {

    * A lot of data cleansing etc..
    * Even more data cleansing etc..

    * Now I want to delete the previous file, save a new with the prefix and todays date

    * first delete the previous file (created in another date)
    local s =insID
    cd "/Users/jesper.eriksson/.../somefolder"
    local file : dir . files "`s'_*"
    local file2 : subinstr local file " " "", all
    capture noisily erase `file2'

    * After the previous line is where my problem is, where the error message is showing up


    * Finally save the new file (with todays date)
    local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
    di "`todate'"

    * Remove irritating space in local
    local todate : subinstr local todate " " "", all

    * Save
    save "`s'_`todate'.dta", replace
    }



    As I said, when I use capture noisily erase 26_2022-04-17.dta the file is deleted as expected, however I canĀ“t get it to work with locals. I know you can not have wildcards in filenames.

    Any tips?

  • #2
    Can you give the names of two of your files (let's say the first two) that you'd expect to loop over and say what you'd want them to change to? Then, re run your code and tell us where it went wrong?

    And just for clarity since it was a little hard to follow, can you put your code in code delimiters?

    Comment


    • #3
      Of course, sorry.

      Files (examples):

      3_2022-04-17.dta
      4_2022-04-17.dta
      6_2022-04-17.dta
      11_2022-04-17.dta
      ...
      2308_2022-04-17.dta


      Code:
      foreach ID in `IDlist' {
      
      local s =insID
      cd "/Users/jesper.eriksson/.../somefolder"
      local file : dir . files "`s'_*"
      local file2 : subinstr local file " " "", all
      capture noisily erase `file2'
      
      local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
      di "`todate'"
      
      local todate : subinstr local todate " " "", all
      
      save "`s'_`todate'.dta", replace
      }

      Comment


      • #4
        What is the purpose of the first subinstr? From your list of filenames, none of them have spaces and it seems that there is only one file per ID, so there wouldn't be any spaces in local file. The only thing I can think of is that there are multiple files per ID which are being pulled by dir . files "`s'_*", and you are combining them into a spaceless list, which you then feed into erase. However, erase cannot erase multiple files at once. You would need a sub-loop for that:

        Code:
        foreach ID in `IDlist'{
            local s =insID
            cd "/Users/jesper.eriksson/.../somefolder"
            local file : dir . files "`s'_*"
            foreach f of local file{
                capture noisily erase "`f'"
            }
            local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
            di "`todate'"
            local todate : subinstr local todate " " "", all
            save "`s'_`todate'.dta", replace
        }
        Absent additional details, I can't identify any other problems. What is the error you get when you remove capture from the erase command?

        Added: crossed with #5, which rightly points out the lack of reference to your loop ID macro. My guess is the code should look like this (with file2 still removed, holding the assumption that there is only one file per ID):

        Code:
        foreach ID in `IDlist'{
            cd "/Users/jesper.eriksson/.../somefolder"
            local file : dir . files "`ID'_*"
            capture noisily erase `file'
            local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
            di "`todate'"
            local todate : subinstr local todate " " "", all
            save "`ID'_`todate'.dta", replace
        }
        Last edited by Ali Atia; 18 Apr 2022, 08:46.

        Comment


        • #5
          Okay so the thing I'm not understanding firstly is that your macro ID doesn't appear anywhere in this loop. That is,
          Code:
          foreach ID in `IDlist' {
          
          local s =insID
          cd "/Users/jesper.eriksson/.../somefolder"
          
          local file : dir . files "`s'_*"
          local file2 : subinstr local file " " "", all
          
          capture noisily erase `file2'
          
          local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
          
          di "`todate'"
          
          local todate : subinstr local todate " " "", all
          
          save "`s'_`todate'.dta", replace
          
          }
          
          **## Where is the `ID'?
          the `ID' macro you begin with doesn't appear here. So on first glance, of course the loop won't do anything since the thing it's predicated on isn't in the loop.

          Comment


          • #6
            And ref
            * Remove irritating space in local
            your
            Code:
            local todate: display %td_CCYY-NN-DD date(c(current_date), "DMY")
            insert a space using "_" in the format (see [D] Datetime display formats. Syntax). Thus, remove the underscore from your format definition:
            Code:
            local todate: display %tdCCYY-NN-DD date(c(current_date), "DMY")
            alternative
            Code:
            local todate = string(today(), "%tdCCYY-NN-DD")

            Comment


            • #7
              Hi,

              Terribly sorry for my bad example. I cut out some of the code in an effort to make the example easier. The result was the opposite. However I have now solved the problem. It was merely a typing error. Anyway great thanks for your time and help, thanks also for the help in #6. Did not know that regarding date and times.

              Again, sorry for the bad example.

              Comment

              Working...
              X