Hi,
I have an issue when trying to delete empty folders structure in SAS stored process web application. I'm trying to delete empty folders:
%macro deleteDirectory(filepath);
filename testdir "&filepath";
data _null_;
rc=fdelete('testdir');
put rc=;
msg=sysmsg();
put msg=;
run;
filename testdir clear;
%mend deleteDirectory;
%deleteDirectory(&_TempFolderPath/docx/word/_rels);
%deleteDirectory(&_TempFolderPath/docx/word);
and on the second deletion (&_TempFolderPath/docx/word) I receive an error message:
rc=20047
msg=ERROR: xxx\docx\word is not empty and cannot be deleted.
_rels directory is removed successfully. I don't have x commands available.
Use this macro to traverse the whole directory tree and show all files:
%macro find(directory);
%local did i name subdir fref fref2;
%let did=%sysfunc(filename(fref,&directory));
%let did=%sysfunc(dopen(&fref));
%if &did ne 0
%then %do;
%do i = 1 %to %sysfunc(dnum(&did));
%let name=&directory/%sysfunc(dread(&did,&i));
%let subdir=%sysfunc(filename(fref2,&name));
%let subdir=%sysfunc(dopen(&fref2));
%if &subdir ne 0
%then %do;
%let subdir=%sysfunc(dclose(&subdir));
%find(&name)
%end;
%else %put &name;
%let subdir=%sysfunc(filename(fref2));
%end;
%let did=%sysfunc(dclose(&did));
%end;
%let did=%sysfunc(filename(fref));
%mend;
Thank you for the help! It seems like folders are actually deleted after execution. Since folders below are deleted (checked from windows explorer after the execution):
The whole call goes like this:
%deleteDirectory(&_TempFolderPath/docx/_rels);
%deleteDirectory(&_TempFolderPath/docx/docProps);
%deleteDirectory(&_TempFolderPath/docx/word/_rels);
%deleteDirectory(&_TempFolderPath/docx/word);
%deleteDirectory(&_TempFolderPath/docx);
%deleteDirectory(&_TempFolderPath/mht/images);
%deleteDirectory(&_TempFolderPath/mht);
%deleteDirectory(&_TempFolderPath);
%* LOG: ;
%* xxx\docx\word is not empty and cannot be deleted.;
%* xxx\docx is not empty and cannot be deleted. ;
%* xxx\mht is not empty and cannot be deleted. ;
%* xxx is not empty and cannot be deleted. ;
%find(&_TempFolderPath);
%* LOG: ;
xxx/docx/docProps
xxx/docx/word/_rels
xxx/docx/_rels
xxx/mht/images
You might have additional directories in there; as posted, my macro only shows files.
Add another %PUT:
%if &subdir ne 0
%then %do;
%let subdir=%sysfunc(dclose(&subdir));
%put dir=&name;
%find(&name)
%end;
dir=xxx/docx xxx/docx/docProps dir=xxx/docx/word xxx/docx/word/_rels xxx/docx/_rels dir=xxx/mht xxx/mht/images
Thanks again for the reply. Images, _rels and docProps are also folders at least in file explorer. I have checked multiple times the folder structure and folders should be deleted in correct order. Before folder deletion I have a macro executed which removes all the files inside folders and that works as expected. Actual path (xxx) is network folder path.
So there is a file called docProps in xxx/docx. This prevents the directory from being deleted. Similar for the others.
Also keep in mind that the SAS code runs on a server, and what you see in your Windows Explorer might only be your local directory tree on the desktop.
I found out that if I try to delete the same folder twice the second deletion attempt throws an error about insufficient authorization:
%macro deleteDirectory(filepath);
filename testdir "&filepath";
data _null_;
rc=fdelete('testdir');
put rc=;
msg=sysmsg();
put msg=;
run;
filename testdir clear;
%mend deleteDirectory;
%deleteDirectory(&_TempFolderPath/docx/_rels);
%deleteDirectory(&_TempFolderPath/docx/_rels);
LOG:
rc=0
msg=
rc=20029
msg=ERROR: Insufficient authorization to access xxx\docx\_rels.
So the first deletion actually does not delete the file. There should not be authorization issues.. And if I check afterwards in the same run files with your %find macro the folder still exist.
I think you should put this to the attention of SAS Technical Support, as this makes no sense to me at all.
The only explanation that comes to my mind is that some of the numerous Windows idiosyncrasies play a role here. If your xxx actually points to a UNC or other shared network location, that might also be part of the problem.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.