Announcement

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

  • Erasing older files if exists

    I am trying to execute some sytax to erase old archived input files that are two months old. I've placed a test file in my target archive folder, but it is not being erased. I've confirmed that the local for 'two months old' and my file name are in the same format and appear as 06May2024. Any advice on how to troubleshoot is appreciated.

    Code:
    *** 2.A Determining reference dates
    ********************************************
    
    *** 2.A.1 Creating fake dataset to calculate dates of interest
    // Need today's date and two months ago
    // Using 9 weeks or 63 days ago (getting a target divisible by 7 for weekly automation)
    insobs 1 gen today = date(c(current_date),"DMY") gen twomonthsago = today - 63 format today twomonthsago %tdDDMonCCYY *** 2.A.2 Moving formatted dates into macros quietly summarize today, mean local today: disp %tdDDMonCCYY r(mean) quietly summarize twomonthsago, mean local twomonthsago: disp %tdDDMonCCYY r(mean) *** 2.B Managing Copies of the HAPI Files ******************************************** *** 2.B.1 Erasing file from two months ago, if it exists if fileexists("`fldr_archive'\*`twomonthsago'.csv")== 1 /// erase "`fldr_archive'\*`twomonthsago'.csv" else di "No archive file to erase"

  • #2
    Could you clarify your intent? Do you want to erase an archive file that is exactly 63 days old -- this is what your code does -- or do you want to erase all archive files that are at least 63 days old?

    Incidentally, I'm also not sure why you would need to create a variable (today, twomonthsago) for this, when you could work directly with macros:

    Code:
    local today: disp %tdDDMonCCYY date(c(current_date),"DMY")
    local twomonthsago: disp %tdDDMonCCYY `=date(c(current_date),"DMY")-63'
    That said, this might be an improvement in code efficiency, but is not the fix for your problem.

    Finally, your code excerpt does not define the local macro folder_archive, so there is a chance you are not looking in the correct path. Or perhaps your actual code has this, and it is just your extract that is missing the macro definition.
    Last edited by Hemanshu Kumar; 08 Jul 2024, 09:40.

    Comment


    • #3
      Here is some code to delete all archive files that are at least 63 days old:

      Code:
      local fldr_archive . // CHANGE THIS TO YOUR ACTUAL FOLDER
      local archive_files: dir `"`fldr_archive'"' files "*.csv"
      
      *** 2.B Managing Copies of the HAPI Files
      ********************************************
      *** 2.B.1 Erasing files from at least two months ago, if they exist
      
      local erased 0
      foreach file of local archive_files {
          local file_date = substr("`file'",-13,9)
          local file_date = date("`file_date'","DMY")
          if `file_date' <= `=date(c(current_date),"DMY")-63' {
              dis "Erasing `file' ..."
              erase "`fldr_archive'/`file'"
              local erased 1
          }
      }
      
      if !`erased' dis "No archive file to erase"
      Last edited by Hemanshu Kumar; 08 Jul 2024, 09:56.

      Comment


      • #4
        Thank you very much Hemanshu Kumar. That syntax worked, I was indeed intending to delete all of the files more than 63 days old. Kind thanks!

        Comment

        Working...
        X