BookmarkSubscribeRSS Feed
Billybob73
Quartz | Level 8

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

 

14 REPLIES 14
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Billybob73
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Billybob73
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Billybob73
Quartz | Level 8

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

 

 

Kurt_Bremser
Super User

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.

Billybob73
Quartz | Level 8

Hi Kurt,

Thanks
Unfortunately this is not working

Rgds

B

Billybob73
Quartz | Level 8

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

Billybob73
Quartz | Level 8

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;

Billybob73
Quartz | Level 8

I used these. Rather than x commands. Thanks !

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
  • 14 replies
  • 11350 views
  • 0 likes
  • 3 in conversation