DATA Step, Macro, Functions and more

deleting folders (keeping 5 days folders only)

Reply
Frequent Contributor
Posts: 76

deleting folders (keeping 5 days folders only)

Hi all,

I want to delete the folders (want to keep 5 days folders only).

my code is creating folders every day yymmdd10. format.

but I want to keep only 5 days folders only.

my folders are like this

20180419
20180420

20180421
20180423

20180424
20180425

20180426
20180427

20180428
20180429

20180430

 

Thanks,

SS.

 

Super User
Super User
Posts: 9,441

Re: deleting folders (keeping 5 days folders only)

Something like (and you don't mention which os you use so windows assumed):

http://support.sas.com/kb/45/805.html

Filename filelist pipe "dir /b /s c:\temp\*.sas"; 
                                                                                   
Data _null_;                                        
  Infile filelist truncover;
  Input filename $100.;
  if _n_>5 then call execute(cats('%sysexec(del c:/temp/"',filename,'");'));
Run; 

You can change the if as you like, maybe input as date and check if date is today()-5 or less.

Frequent Contributor
Posts: 76

Re: deleting folders (keeping 5 days folders only)

It is windows.

 I want to delete the folders irrespective of files in those folders.

Below filename statement referring .sas files.

Filename filelist pipe "dir /b /s c:\temp\*.sas";

 

see the attached

 

 

Attachment
Super User
Super User
Posts: 9,441

Re: deleting folders (keeping 5 days folders only)

I am not opening zip files.

Just change the filename to point to your area, i.e replace c:/temp with where your folders are located:

Filename filelist pipe "dir /b /s /ad c:\temp\";

Note I added /ad to restrict list to directories only.

Frequent Contributor
Posts: 76

Re: deleting folders (keeping 5 days folders only)

Thank you,

I think,I have an issue with my sas shortcut.

NOTE: The infile FILELIST is:Unnamed Pipe Access Device,PROCESS=dir /b /s C:\test\.

Thanks,

SS

Super User
Super User
Posts: 9,441

Re: deleting folders (keeping 5 days folders only)

I don't know what "I think,I have an issue with my sas shortcut." means?  The note seems correct, assuming all your folders are in c:/test.  Note your missing the /ad, which only gets directories.

Super User
Posts: 23,354

Re: deleting folders (keeping 5 days folders only)

You need XCMD enabled to use PIPE commands or to use system commands.

 

options xcmd;

If that errors out, you'll need a different method, most likely using FDELETE instead. It's still possible, just a different approach required.

Ask a Question
Discussion stats
  • 6 replies
  • 103 views
  • 3 likes
  • 3 in conversation