DATA Step, Macro, Functions and more

Want to delete 3 year old files only from a folder

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Want to delete 3 year old files only from a folder

Dear Experts ,

 

I am trying to delete the files which are 3 year and above old . If I use the code which I attached this will delete the files which I need and its deleting the files which are in other formats also. But I need only the specified files to be deleted.

 

Please help me on this

 

Manesh

options mlogic;
    %macro delete_year_files_in_folder(folder);
       filename filelist "&folder";
       data _null_;
	      dir_id = dopen('filelist');
          total_members = dnum(dir_id);
          do i = 1 to total_members;  
           member_name = dread(dir_id,i);

		datestring = scan(member_name,4,'_');
		
		month = input(substr(datestring,5,2),best.);
		day = input(substr(datestring,5,2),best.);
		year = input(substr(datestring,1,4),best.);
		date = mdy(month, day, year);
			 if intnx('year', today(),-3,'S') > date then do;
              file_id = mopen(dir_id,member_name,'i',0);
              if file_id > 0 then do; 
                freadrc = fread(file_id);
                rc = fclose(file_id);
                rc = filename('delete',member_name,,,'filelist');
                rc = fdelete('delete');
             end;
             rc = fclose(file_id);
          end;
          end;
          rc = dclose(dir_id);
       run;
    %mend;


	%delete_year_files_in_folder(C:\Users\UCS1MKP\Desktop\check)



Accepted Solutions
Solution
‎10-05-2016 06:29 AM
Super User
Posts: 7,766

Re: Want to delete 3 year old files only from a folder

Posted in reply to ambadi007

ambadi007 wrote:
Hi The file name is
SFRE_BIL_SIT_20160812_134317_PAM_FILES1.zip
So from here I am only taking the year part from the file name. But When I run the program it is deleting the other files which are in different format for example the files like one.txt or two.zip something like this .


If code like this doesn't work like you expected, comment the part that actually does something (in yout case, the delete action) and instead create a dataset that contains all relevant variables, and use output instead of the action. Then inspect your dataset, and you will be able to follow all the conditions.

If that is not granular enough, use put _all_ at the relevant stages of your decision process. Then you find the status of all variables at every intersting step in the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: Want to delete 3 year old files only from a folder

Posted in reply to ambadi007

I would suggest that you would be better off talking to your IT group and get them to archive files over 3 years old.  They will have some automated process for such a task and it provides a safety net for you, both in the fact that you are not deleting files (which can go wrong - badly), and if you ever want to go back to those files (which is 99% probabilty after you delete them) then you can retrieve from archive.

 

As for your code, can you not put in another if statement, say for .txt only:

if index(upcase(member_name),".TXT") then do;

...delete

end;

Super User
Posts: 5,426

Re: Want to delete 3 year old files only from a folder

Posted in reply to ambadi007

What do you mean by "specified"? Apply to the name standard of date in the filename?

Why are month and day picked from the same location, can't be right.

Perhaps some files doesn't have a valid date in the name, and missing is a smaller value than three years from now.

Do you get any warnings in the log for the assignment statements?

Data never sleeps
Contributor
Posts: 64

Re: Want to delete 3 year old files only from a folder

Hi The file name is
SFRE_BIL_SIT_20160812_134317_PAM_FILES1.zip
So from here I am only taking the year part from the file name. But When I run the program it is deleting the other files which are in different format for example the files like one.txt or two.zip something like this .

Solution
‎10-05-2016 06:29 AM
Super User
Posts: 7,766

Re: Want to delete 3 year old files only from a folder

Posted in reply to ambadi007

ambadi007 wrote:
Hi The file name is
SFRE_BIL_SIT_20160812_134317_PAM_FILES1.zip
So from here I am only taking the year part from the file name. But When I run the program it is deleting the other files which are in different format for example the files like one.txt or two.zip something like this .


If code like this doesn't work like you expected, comment the part that actually does something (in yout case, the delete action) and instead create a dataset that contains all relevant variables, and use output instead of the action. Then inspect your dataset, and you will be able to follow all the conditions.

If that is not granular enough, use put _all_ at the relevant stages of your decision process. Then you find the status of all variables at every intersting step in the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 64

Re: Want to delete 3 year old files only from a folder

Hi The file name is
SFRE_BIL_SIT_20160812_134317_PAM_FILES1.zip
So from here I am only taking the year part from the file name. But When I run the program it is deleting the other files which are in different format for example the files like one.txt or two.zip something like this .
Super User
Posts: 5,426

Re: Want to delete 3 year old files only from a folder

Posted in reply to ambadi007

So your problem can basically be solved by what I pointed out in my previous post.

Data never sleeps
Contributor
Posts: 64

Re: Want to delete 3 year old files only from a folder

Yes I am getting a note like

NOTE: Missing values were generated as a result of performing an operation on missing values.

Each place is given by: (Number of times) at (Line)Smiley SadColumn).

 

 

Please advice me how to give a standard name to the files which I need to delete ?

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 387 views
  • 3 likes
  • 4 in conversation