<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Modify file deletion code to only delete files older than 5 days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636634#M189160</link>
    <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #999999;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320085" target="_blank" rel="noopener"&gt;sastef41&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;Your exact same problem is solved in my recent blog post &lt;A title="Delete files based on their date stamp" href="https://blogs.sas.com/content/sgf/2018/07/17/delete-sas-logs-admin/" target="_self"&gt;SAS administrators tip: Automatically deleting old SAS logs&lt;/A&gt; .&lt;/P&gt;
&lt;P&gt;There, I used the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mr_clean(dirpath=,dayskeep=30,ext=.log);
   data _null_;
      length memname $256;
      deldate = today() - &amp;amp;dayskeep;
      rc = filename('indir',"&amp;amp;dirpath");
      did = dopen('indir');
      if did then
      do i=1 to dnum(did);
         memname = dread(did,i);
         if reverse(trim(memname)) ^=: reverse("&amp;amp;ext") then continue;
         rc = filename('inmem',"&amp;amp;dirpath/"!!memname);
         fid = fopen('inmem');
         if fid then 
         do;
            moddate = input(finfo(fid,'Last Modified'),date9.); /* see WARNING below */
            rc = fclose(fid);
            if . &amp;lt; moddate &amp;lt;= deldate then rc = fdelete('inmem');
         end;
      end; 
      rc = dclose(did);
      rc = filename('inmem');
      rc = filename('indir');
   run;
%mend mr_clean;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can easily modify it to adapt to your scenario.&lt;/P&gt;
&lt;P&gt;The key here is the following statement to capture file date stamp:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;moddate = input(finfo(fid,'Last Modified'),date9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and IF statement to delete files of specified date interval:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if . &amp;lt; moddate &amp;lt;= deldate then rc = fdelete('inmem');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope, this help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 01 Apr 2020 19:04:26 GMT</pubDate>
    <dc:creator>LeonidBatkhan</dc:creator>
    <dc:date>2020-04-01T19:04:26Z</dc:date>
    <item>
      <title>Modify file deletion code to only delete files older than 5 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636601#M189139</link>
      <description>&lt;P&gt;I have the below code that deletes all files within a SAS folder.&amp;nbsp; I would like to modify it, &lt;STRONG&gt;or&lt;/STRONG&gt; come up with a different solution that would only delete the files in the said folder if older than 5 days.&amp;nbsp; Any help would be appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro delete_all_the_files_in_folder(folder);
   filename filelist "&amp;amp;folder";
   data _null_;
      dir_id = dopen('filelist');
      total_members = dnum(dir_id);
      do i = 1 to total_members;  /* walk through all the files in the folder */
         member_name = dread(dir_id,i);
         file_id = mopen(dir_id,member_name,'i',0);
         if file_id &amp;gt; 0 then do; /* if the file is readable */
            freadrc = fread(file_id);
            rc = fclose(file_id);
            rc = filename('delete',member_name,,,'filelist');
            rc = fdelete('delete');
         end;
         rc = fclose(file_id);
      end;
      rc = dclose(dir_id);
   run;
%mend;
%delete_all_the_files_in_folder(/sasem/service/Face_IT/Export/)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Apr 2020 16:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636601#M189139</guid>
      <dc:creator>sastef41</dc:creator>
      <dc:date>2020-04-01T16:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: Modify file deletion code to only delete files older than 5 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636602#M189140</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro delete_all_the_files_in_folder(folder);
   filename filelist "&amp;amp;folder";
   data _null_;
      dir_id = dopen('filelist');
      total_members = dnum(dir_id);
      do i = 1 to total_members;  /* walk through all the files in the folder */
         member_name = dread(dir_id,i);
         file_id = mopen(dir_id,member_name,'i',0);
         if file_id &amp;gt; 0 then do; /* if the file is readable */
            freadrc = fread(file_id);
            if finfo(file_id,"info_item") &amp;lt; value then do;
            /* info_item is operating system- and locale-dependent */
            /* determine it by using foptname() once */
            /* value must match the type, usually datetime */
              rc = fclose(file_id);
              rc = filename('delete',member_name,,,'filelist');
              rc = fdelete('delete');
          end;
         end;
         rc = fclose(file_id);
      end;
      rc = dclose(dir_id);
   run;
%mend;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As stated in the comment, you need to run a loop from 1 to &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0obx7o9ceaq31n1xwpddcub8d7f.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;foptnum&lt;/A&gt; once and use &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n1ocdzkn9uhsa7n1qv3wwp4d3dot.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;foptname(file_id,num)&lt;/A&gt; to find the correct name for your "file information item".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 16:38:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636602#M189140</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-01T16:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: Modify file deletion code to only delete files older than 5 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636604#M189142</link>
      <description>&lt;P&gt;But since this is highly dependent on the operating environment anyway, I'd much rather use OS tools to get this done, e.g. by using &lt;FONT face="courier new,courier"&gt;ls&lt;/FONT&gt; with proper options to retrieve the last modification timestamps for all files; Linux (GNU) &lt;FONT face="courier new,courier"&gt;ls&lt;/FONT&gt; provides options for outputting the modification timestamp in GMT in ISO 8601 notation.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 16:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636604#M189142</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-01T16:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Modify file deletion code to only delete files older than 5 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636634#M189160</link>
      <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #999999;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/320085" target="_blank" rel="noopener"&gt;sastef41&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;Your exact same problem is solved in my recent blog post &lt;A title="Delete files based on their date stamp" href="https://blogs.sas.com/content/sgf/2018/07/17/delete-sas-logs-admin/" target="_self"&gt;SAS administrators tip: Automatically deleting old SAS logs&lt;/A&gt; .&lt;/P&gt;
&lt;P&gt;There, I used the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mr_clean(dirpath=,dayskeep=30,ext=.log);
   data _null_;
      length memname $256;
      deldate = today() - &amp;amp;dayskeep;
      rc = filename('indir',"&amp;amp;dirpath");
      did = dopen('indir');
      if did then
      do i=1 to dnum(did);
         memname = dread(did,i);
         if reverse(trim(memname)) ^=: reverse("&amp;amp;ext") then continue;
         rc = filename('inmem',"&amp;amp;dirpath/"!!memname);
         fid = fopen('inmem');
         if fid then 
         do;
            moddate = input(finfo(fid,'Last Modified'),date9.); /* see WARNING below */
            rc = fclose(fid);
            if . &amp;lt; moddate &amp;lt;= deldate then rc = fdelete('inmem');
         end;
      end; 
      rc = dclose(did);
      rc = filename('inmem');
      rc = filename('indir');
   run;
%mend mr_clean;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can easily modify it to adapt to your scenario.&lt;/P&gt;
&lt;P&gt;The key here is the following statement to capture file date stamp:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;moddate = input(finfo(fid,'Last Modified'),date9.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and IF statement to delete files of specified date interval:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if . &amp;lt; moddate &amp;lt;= deldate then rc = fdelete('inmem');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope, this help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 19:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636634#M189160</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-04-01T19:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Modify file deletion code to only delete files older than 5 days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636867#M189267</link>
      <description>Thanks so much!</description>
      <pubDate>Thu, 02 Apr 2020 13:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-file-deletion-code-to-only-delete-files-older-than-5-days/m-p/636867#M189267</guid>
      <dc:creator>sastef41</dc:creator>
      <dc:date>2020-04-02T13:29:47Z</dc:date>
    </item>
  </channel>
</rss>

