DATA Step, Macro, Functions and more

delete folders with files from SAS

Reply
Contributor
Posts: 55

delete folders with files from SAS

Hi,

Is it possible to delete a complete folder with files in it with one singe statement ?

Or do you first need to remove the files and then the folders ?

Thanks

B

 

Super User
Super User
Posts: 9,840

Re: delete folders with files from SAS

Posted in reply to Billybob73
Super User
Posts: 10,570

Re: delete folders with files from SAS

Posted in reply to Billybob73

The fdelete() function (which is the SAS tool for this) can only remove empty directories. Removing a non-empty directory will require XCMD and the proper command from the OS.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,840

Re: delete folders with files from SAS

Posted in reply to KurtBremser

Well, you could combine the first example, with a loop over dir and then a delete folder:

https://amadeus.co.uk/sas-tips/listing-files-in-a-directory/

 

So take the second box, replace the put line with an:

rc=fdelete(physname);

 Possibly need path as well - can't check at the moment.  Then at the end fdelete the folder.

Not saying its ideal at any rate, but it is possible with just SAS functions.

Contributor
Posts: 55

Re: delete folders with files from SAS

I combined both, but there's still a problem that remains. Folders which did not have files in them were not deleted.

General question : How can I delete empty folders with empty sub folders from SAS ? Is this possible ?

I tried x commands but they don't work despite path authorization and XCMD switched on.

Super User
Super User
Posts: 9,840

Re: delete folders with files from SAS

Posted in reply to Billybob73

Just saying "it doesn't work" does not help at all.  Please show examples of what code you have tried.

With the X command ones, likelihood is what you sent to the operating system was incorrect.  Again. code would show this.  Also note, that the commands for windows are different to linux/unix or other OS's.

 

Also out of interest, why do you want to do this, sounds more like a sys admin role than a SAS programming one.

Contributor
Posts: 55

Re: delete folders with files from SAS

Hi,

You're right about that. I've been assigned as sysadmin to get listings of files for our sas user community.

 

I'm running this :

 

%let path = %nrstr(G:\TEST\R&D);

options noxwait ;
x "rmdir &path. /s /q";
options xwait;

 

Thanks

Super User
Super User
Posts: 9,840

Re: delete folders with files from SAS

Posted in reply to Billybob73

Then not sure why you are doing this in SAS?   Anyways, something umped out at me

%let path = %nrstr(G:\TEST\R&D);

                                                ^

 

 

Yep, good old special characters in a system path.  Bad, bad, bad.  

 

What happens when you run the code, can you run it with /q off and see what the system error is?  I suspect either the & is messing with macro resolution, or you lack some sort of permission/that folder doesn't exist.

Contributor
Posts: 55

Re: delete folders with files from SAS

Hi,

The & is the reason I'm using a %nrstr

 

I've made some progress, but still not working unfo..

 

%let path=%nrstr(G:\TEST\R&D);
options noxsync noxwait noxmin;
  x rd /S /Q "&path";
options xwait;

 

something in the syntax is still not right........

 

 

Super User
Posts: 10,570

Re: delete folders with files from SAS

Posted in reply to Billybob73

Instead of the x statement, use the filename pipe method:

filename oscmd pipe "rd /S /Q '&path' 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

This will show all output and messages in the SAS log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 55

Re: delete folders with files from SAS

Posted in reply to KurtBremser

Hi Kurt,

Thanks
Unfortunately this is not working

Rgds

B

Super User
Posts: 10,570

Re: delete folders with files from SAS

Posted in reply to Billybob73

"not working" is very un-helpful.

Please post the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 55

Re: delete folders with files from SAS

Posted in reply to KurtBremser

Hi Kurt,

 

I found out that this is working :

 

options noxsync noxwait noxmin;

x RD /S /Q "%superq(folderdelete)";

 

options xwait;

 

Thanks to your  hints I managed to do this  :-)

 

Rgds,

B

Contributor
Posts: 55

Re: delete folders with files from SAS

Posted in reply to Billybob73

By the way folderdelete is a macro variable that comes from a do loop. This is the loop code :

     %let max_id = 0;
     proc sql noprint; select max(id) into:max_id from deleterootfolders2; quit;
     %put &max_id.;
     %if %eval(&max_id.) ne  0 %then
     %do;
        %do a=1 %to &max_id.;
        data _null_;
        set deleterootfolders2;
        call symputx('folderdelete', year_root);
        where ID = &&a.;
        run;
        options noxsync noxwait noxmin;
        x RD /S /Q "%superq(folderdelete)";
        options xwait;
        %end;
     %end;

Contributor
Posts: 55

Re: delete folders with files from SAS

Posted in reply to KurtBremser

I used these. Rather than x commands. Thanks !

Ask a Question
Discussion stats
  • 14 replies
  • 828 views
  • 0 likes
  • 3 in conversation