BookmarkSubscribeRSS Feed
bearda
Obsidian | Level 7

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.

 

 

7 REPLIES 7
Kurt_Bremser
Super User

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;
bearda
Obsidian | Level 7

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):

  • docx/_rels
  • docx/docprops
  • docx/word/_rels
  • mht/images

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

 

 

Kurt_Bremser
Super User

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;
bearda
Obsidian | Level 7
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. 

Kurt_Bremser
Super User

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.

 

bearda
Obsidian | Level 7

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.

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 1217 views
  • 0 likes
  • 2 in conversation