<?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: Delete files if more then 5 in directory in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791803#M81458</link>
    <description>&lt;P&gt;there are .log files and .xlsx files in the directory. It is a file directory.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes the code is SAS.&lt;/P&gt;&lt;P&gt;Files are not sorted by anything. I dont need any sorting etc.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2022 09:45:07 GMT</pubDate>
    <dc:creator>Citrine10</dc:creator>
    <dc:date>2022-01-24T09:45:07Z</dc:date>
    <item>
      <title>Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791799#M81456</link>
      <description>&lt;P&gt;Hi there&lt;/P&gt;&lt;P&gt;I have code that checks and lists files in a directory. I would like to do a check to see if there are less than 5 files it should continue else if there are more than 5 files it should delete the extra files and exit.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I implement that?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 09:18:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791799#M81456</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T09:18:38Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791801#M81457</link>
      <description>Is the file you are referring to a SAS data set or a file on the OS?&lt;BR /&gt;Also, is the code SAS? Is it a shell script (command, batch, PowerShell, etc.)?&lt;BR /&gt;What exactly are the extra files? Are they sorted by date, by name, or by file size?&lt;BR /&gt;Does it terminate when it's done checking one target directory? Or does it check recursively?&lt;BR /&gt;&lt;BR /&gt;The question is too vague, so please organize it a little better.</description>
      <pubDate>Mon, 24 Jan 2022 09:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791801#M81457</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-24T09:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791803#M81458</link>
      <description>&lt;P&gt;there are .log files and .xlsx files in the directory. It is a file directory.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes the code is SAS.&lt;/P&gt;&lt;P&gt;Files are not sorted by anything. I dont need any sorting etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 09:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791803#M81458</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T09:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791805#M81459</link>
      <description>&lt;P&gt;I'm not sure what kind of code you have, but you can use&lt;BR /&gt;1. save the list of files into a dataset with their file paths.&lt;BR /&gt;2. If there are more than 5 obs, delete them based on the file path.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* start create test files */
%Macro MTestFileCreate;
  %do i=1 %to 10;
  filename f "C:\Temp\extrafile&amp;amp;i..log";
  data _null_;
    file f;
    put 'sample';
  run;
  %end;
%Mend;
%MTestFileCreate;
data filelist;
  length path $200;
  do i=1 to 10;
    path=cats("C:\Temp\extrafile",i,".log");
    output;
  end;
  drop i;
run;
/* end create test files */
data _null_;
  set filelist;
  rc=filename('del',path);
  if _n_&amp;gt;5 then rc=fdelete('del');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 10:13:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791805#M81459</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-24T10:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791809#M81460</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;thank you for the above. I tried the code but it does not work. it does not delete the files &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Alternatively, I tried getting only 5 records from the list of files and thereafter decided to remove the remaining files however I am struggling with the delete and the if statement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 10:29:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791809#M81460</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T10:29:19Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791810#M81461</link>
      <description>How are you getting the file list in your code?&lt;BR /&gt;If you show the code, I can advise you.</description>
      <pubDate>Mon, 24 Jan 2022 10:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791810#M81461</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-24T10:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791812#M81462</link>
      <description>&lt;P&gt;I have a macro that lists all the files and I call it&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro list_files(dir,ext);
	%local filrf rc did memcnt name i;
	%let rc=%sysfunc(filename(filrf,&amp;amp;dir));
	%let did=%sysfunc(dopen(&amp;amp;filrf));
	%if %sysfunc(exist(tables)) = 1 %then
		%do;
			proc sql;
				drop table tables;
			quit;
		%end;
	%if &amp;amp;did eq 0 %then
		%do;
			%put Directory &amp;amp;dir cannot be open or does not exist;
			%return;
		%end;
	%do i = 1 %to %sysfunc(dnum(&amp;amp;did));
		%let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));
		%if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then
			%do;
				%put &amp;amp;dir\&amp;amp;name;
				data _tmp;
					length dir $512 name $100 ext $100;
					dir=symget("dir");
					name=symget("name");
					ext=upcase("&amp;amp;ext.");
				run;
				proc append base=tables data=_tmp;
				run;
				quit;
			%end;
		%else %if %qscan(&amp;amp;name,2,.) = %then
			%do;
			%end;
	%end;
	%let rc=%sysfunc(dclose(&amp;amp;did));
	%let rc=%sysfunc(filename(filrf));
	proc sql;
		drop table _TMP;
	quit;
%mend list_files;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;I call the macro :&lt;/P&gt;&lt;PRE&gt;include "/C:/list_files.sas";

/*file paths*/
%let path_in= /myfiles;

/*List of files*/
%list_files("&amp;amp;path_in",xlsx)

proc sql;
create table allfiles as
select * from tables;
quit;

proc sql;
create table FileCount as
select count(name) into: Filecount from tables;
quit;&lt;/PRE&gt;&lt;P&gt;Now this is where I need it to do a check to see if there are more then 5 files in the directory then delete them and exit, if there are less than 5 files, then continue&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 10:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791812#M81462</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T10:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791827#M81464</link>
      <description>If the directory has more than 5 files, and you want to delete the "extra" files, how do you determine which files to delete?</description>
      <pubDate>Mon, 24 Jan 2022 12:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791827#M81464</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-24T12:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791828#M81465</link>
      <description>&lt;P&gt;Yes correct. it can be any of the files&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 12:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791828#M81465</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T12:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791842#M81466</link>
      <description>&lt;P&gt;I suggest reviewing:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sgf/2018/07/17/delete-sas-logs-admin/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2018/07/17/delete-sas-logs-admin/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 13:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791842#M81466</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-24T13:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791847#M81467</link>
      <description>Thank you Quentin, that is very useful. Do you know where I can put a check to see if there are more than 5 files then delete them and exit else continue?</description>
      <pubDate>Mon, 24 Jan 2022 13:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791847#M81467</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T13:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791856#M81468</link>
      <description>&lt;P&gt;I would approach this in chunks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you happy with your %listfiles macro?&amp;nbsp; If so, then you've got a dataset that lists all the files in the directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would next write a %deletefile() macro which allows you to pass it a list of files to be deleted.&amp;nbsp; I would use the blog post as a guide for approaches to writing that macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have %deletefiles done, then you can run your code like you have it to get a dataset of files, and you can check that dataset to see if it has more then 5 files, and if it does, you can use %deletefiles to delete the extras.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or since you said you don't care which files are deleted, maybe you could do something quick like (untested pseudo-code):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    set tables; *your dataset listing all the files found in the directory;
    if _n_&amp;gt;5;
    rc=filename(fname, cats(dir,name,ext)); 
    rc=fdelete(fname);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 14:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791856#M81468</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-01-24T14:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791863#M81469</link>
      <description>&lt;P&gt;thank you Quentin, you are really helping me get there &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have a macro that deletes a file from a directory and when testing with one file, it works however when I try to pass it through the code it doesnt work. It only shows me the remaining list which should be deleted yet it doesnt delete&lt;/P&gt;&lt;PRE&gt;%macro delfile(file);
%if %sysfunc(fileexist(&amp;amp;file)) ge 1 %then %do;
   %let rc=%sysfunc(filename(temp,&amp;amp;file));
   %let rc=%sysfunc(fdelete(&amp;amp;temp));
%end; 
%else %put The file &amp;amp;file does not exist;
%mend check;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/*lists all files to delete*/&lt;BR /&gt;data list;&lt;BR /&gt;set allfiles; *your dataset listing all the files found in the directory;&lt;BR /&gt;if _n_&amp;gt;5;&lt;BR /&gt;rc=filename(fname, cats(dir,name,ext)); &lt;BR /&gt;rc=fdelete(fname);&lt;BR /&gt;%delFile(&amp;amp;fullpath);&lt;BR /&gt;run;&lt;BR /&gt; &lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 14:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791863#M81469</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-24T14:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791875#M81470</link>
      <description>&lt;P&gt;how about this before use %delfile.&lt;/P&gt;
&lt;P&gt;I think filename and fdelete function in datastep needs quoted fileref.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
set allfiles; *your dataset listing all the files found in the directory;
if _n_&amp;gt;5;
rc=filename('fname', cats(dir,name,ext)); 
rc=fdelete('fname');&lt;BR /&gt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 15:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791875#M81470</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-24T15:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791921#M81471</link>
      <description>&lt;P&gt;How to delete files from a list of files:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*list of files to take an action on;
data test;
infile datalines;
input file_nm $50.;
datalines;
/home/users/temp1.sas7bndx
/home/users/temp2.sas7bndx
/home/users/temp3.sas7bndx
/home/users/temp4.sas7bndx
/home/users/temp5.sas7bndx
/home/users/temp6.sas7bndx
;
run;

*action to do per file - delete file in this case;
%macro check(del_file);
%if %sysfunc(fileexist(&amp;amp;del_file)) ge 1 %then %do;
%let rc=%sysfunc(filename(temp, &amp;amp;del_file));
%let rc=%sysfunc(fdelete(&amp;amp;temp));
%end;
%else %put The file &amp;amp;file does not exist;
%mend check;

*call macro for each file;
data _null_;
set test;

str = catt('%check(', file_nm, ');');

call execute(str);

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 17:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/791921#M81471</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-24T17:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792065#M81478</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I figured it is not deleting because the rc variable is another format and I'm guessing it is unable to identify it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 09:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792065#M81478</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T09:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792068#M81479</link>
      <description>&lt;P&gt;I changed it now to reference the variable 'fullpath' but it still doesnt delete the files:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jan 2022 09:01:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792068#M81479</guid>
      <dc:creator>Citrine10</dc:creator>
      <dc:date>2022-01-26T09:01:28Z</dc:date>
    </item>
    <item>
      <title>Re: Delete files if more then 5 in directory</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792070#M81480</link>
      <description>&lt;P&gt;Is it possible to specify one of those full paths and delete it with the following code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename fname 'specify fullpath';
data _null_;
  rc=fdelete('fname');
  put rc=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If rc is 0, then the file has been deleted.&lt;BR /&gt;Could you try setting the return value of the filename function to "rc0" instead of "rc" and check if the filename function is being handled correctly?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Delete-files-if-more-then-5-in-directory/m-p/792070#M81480</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2022-01-25T07:04:55Z</dc:date>
    </item>
  </channel>
</rss>

