BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ambadi007
Quartz | Level 8

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)


1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@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.

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

LinusH
Tourmaline | Level 20

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
ambadi007
Quartz | Level 8
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 .

Kurt_Bremser
Super User

@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.

ambadi007
Quartz | Level 8
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 .
LinusH
Tourmaline | Level 20

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

Data never sleeps
ambadi007
Quartz | Level 8

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):(Column).

 

 

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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