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
Example 2:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245893.htm
Do be careful, there is no way back though.
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.
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.
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.
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.
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
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.
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........
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.
Hi Kurt,
Thanks
Unfortunately this is not working
Rgds
B
"not working" is very un-helpful.
Please post the log.
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
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;
I used these. Rather than x commands. Thanks !
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.
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.