BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bris_cph
Calcite | Level 5

Hi

 

I am seeking help on a problem with deleting files. On sascommunity.org I have found a piece of code that can recursively delete files and folders:

   /**********************************************************************   Parameters:   root_path - All directories and files under this root will be deleted. The root_path will not be deleted (just empty) lev - Used by the recursion algorithm, no need to modify default rmFiles_lev0 - Set this to N if you only want to delete directories in the root_path and leave the files alone (any files in subdirectories WILL be deleted)     **********************************************************************/
 
 
    %macro recursiveDelete(root_path=_NONE_,lev=0,rmFiles_lev0=Y) / store;
 
        %local rc root_path root_ID root_FN fname_path fname_ID fname_FN ifile nfile;
 
        %if %bquote(&root_path) = _NONE_ %then
            %return;
 
        %put Recursion level &lev;
        %put root_path = &root_path;
 
        /* Open root directory */
        %let rc = %sysfunc(filename(root_FN,&root_path));
        %if &rc ^= 0 %then %do;
            %put %sysfunc(sysmsg());
            %return;
        %end;
        %put root_FN = &root_FN;
        %let root_ID = %sysfunc(dopen(&root_FN));
 
 
        /* Get a list of all files in root directory */
        %let nfile = %sysfunc(dnum(&root_ID));
        %do ifile = 1 %to &nfile;
 
            /* Read pathname of file */
            %local fname_path_&ifile;
            %let fname_path_&ifile = %sysfunc(dread(&root_ID,&ifile));
 
            /* Set fileref */
            %local fname_FN_&ifile;
            %let rc = %sysfunc(filename(fname_FN_&ifile,&root_path/&&fname_path_&ifile));
            %if &rc ^= 0 %then %do;
                %put %sysfunc(sysmsg());
                %return;
            %end;
 
        %end;
 
        /* Loop over all files in directory */
        %do ifile = 1 %to &nfile;
 
            /* Test to see if it is a directory */
            %let fname_ID = %sysfunc(dopen(&&fname_FN_&ifile));
            %if &fname_ID ^= 0 %then %do;
 
                %put &root_path/&&fname_path_&ifile is a directory;
 
                /* Close test */
                %let close = %sysfunc(dclose(&fname_ID));
 
                /* Close root path */
                %let close_root = %sysfunc(dclose(&root_ID));
 
                /* Remove files in this directory */
                %recursiveDelete(root_path=&root_path/&&fname_path_&ifile,lev=%eval(&lev+1));
                %put Returning to recursion level &lev;
 
                /* Remove directory */
                %put Deleting directory &root_path/&&fname_path_&ifile;
                %let rc = %sysfunc(fdelete(&&fname_FN_&ifile));
                %put %sysfunc(sysmsg());
 
                /* Reopen root path */
                %let root_ID = %sysfunc(dopen(&root_FN));
 
            %end;
            %else %if &rmFiles_lev0 = Y or &lev > 0 %then %do;
                %put Deleting file &root_path/&&fname_path_&ifile;
                %let rc = %sysfunc(fdelete(&&fname_FN_&ifile));
                %put %sysfunc(sysmsg());
            %end;
 
        %end;
 
    %mend recursiveDelete;

 

I have used this piece of code for some time without any problems, but last time I used it there apparently was a hidden file called thumbs.db which I understand is a file created by windows. This file is not deleted using the above mentioned code. So my question is: can I rewrite the code so it would also delete hidden files?

 

Any insights?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User
Just a wild guess, but I'd say no. You don't mention what OS you are on. Generally, if a file is truly hidden from the SAS process, you won't be able to delete using this approach, because the file won't even be seen by DNUM() DREAD() etc. If the goal is to delete all the files and directories in &root_path, maybe it would be easier to just delete &root_path itself (which should delete everything in it) and then recreate a folder named &root_path? Unless you hit a permissions issue, I think that would work.
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

4 REPLIES 4
Quentin
Super User
Just a wild guess, but I'd say no. You don't mention what OS you are on. Generally, if a file is truly hidden from the SAS process, you won't be able to delete using this approach, because the file won't even be seen by DNUM() DREAD() etc. If the goal is to delete all the files and directories in &root_path, maybe it would be easier to just delete &root_path itself (which should delete everything in it) and then recreate a folder named &root_path? Unless you hit a permissions issue, I think that would work.
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Bris_cph
Calcite | Level 5

Hi Quentin

 

Thank you for your suggestion.

 

I am not very experienced in programming so just to be sure I don't misunderstand. You suggest I discard the previous code alltogether and replaced it with a musch simpler code which just deletes the the "parent" folder and then creates the "parent" folder again? Permisions shouldn't be a problem.

 

Btw. I am runnig Windows 7.

 

Thanks again for your help.

 

Quentin
Super User

Yes, you have understood my suggestion correctly.  You could use the current macro as a starting point.  It looks like it has the code to delete a folder.  You could remove all the stuff about deleting individual files, and the recursion call, etc.  

 

Another option would be to execute operating system commands (e.g. with X statement) instead of using the macro functions, e.g. :

 

x rmdir /s /q "d:\junk\SomeDir";
x mkdir "d:\junk\SomeDir";

 

Some SAS server configurations disallow execution of OS commands.

 

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Bris_cph
Calcite | Level 5

Many thanks for your solution and for taking the time to elaborate on it. It was very helpful to me

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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