Published on :
Administration CREATION_INTERNE

Recursive Folder Deletion Macro

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
This macro, `mp_deletefolder`, is designed to erase a specified directory and all its contents (files and subdirectories). It starts by checking the validity of the folder path. If the folder is valid, it uses another macro (`%mp_dirlist`) to get a recursive list of all items inside the folder. This list is then sorted by descending level to ensure that internal items are deleted before their containers. The root folder is explicitly added to the deletion list. A `DATA _NULL_` step is used to iterate over this list, deleting each file and folder using the `filename()` and `fdelete()` functions, with rudimentary error handling. Finally, the temporary table used for the folder list is deleted.
Data Analysis

Type : CREATION_INTERNE


The script generates a temporary table (`work.&tempds`) to store the list of file and folder paths to be deleted. These paths are obtained by filesystem introspection via the `mp_dirlist` macro, and not from external business data or the SASHELP library. The main input is a filesystem path.

1 Code Block
Macro SAS
Explanation :
This block initializes the `mp_deletefolder` macro. It first checks if the provided path (`&folder`) is a valid and accessible directory using the `%mf_isdir` macro. If so, it declares a local variable `tempds` and assigns it a unique name generated by `%mf_getuniquename()` for a temporary work table that will store the list of items to be deleted.
Copied!
1%macro mp_deletefolder(folder);
2 /* proceed if valid directory */
3 %IF %mf_isdir(&folder)=1 %THEN %DO;
4 /* prep temp table */
5 %local tempds;
6 %let tempds=%mf_getuniquename();
2 Code Block
Macro SAS (%mp_dirlist) Data
Explanation :
The `%mp_dirlist` macro is called to generate a recursive list of all files and subfolders contained within the directory specified by `&folder`. The results of this list are stored in the temporary table `work.&tempds`, with a maximum search depth (`maxdepth=MAX`).
Copied!
1/* recursive directory listing */
2%mp_dirlist(path=&folder,outds=work.&tempds, maxdepth=MAX)
3 Code Block
PROC SORT
Explanation :
This `PROC SORT` step sorts the temporary table `work.&tempds` by the `level` variable in descending order. This is crucial to ensure that the deepest files and subfolders (higher level) are deleted before their parent folders, thus avoiding errors when deleting non-empty folders.
Copied!
1 /* sort descending level so can delete folder contents before folders */
2 PROC SORT DATA=work.&tempds;
3 BY descending level;
4 RUN;
4 Code Block
PROC SQL
Explanation :
This `PROC SQL` block explicitly adds the root folder path (`&folder`) to the `work.&tempds` table. This ensures that the initial folder, once emptied of its contents, will also be deleted at the end of the process.
Copied!
1/* ensure top level folder is removed at the end */
2 
3PROC SQL;
4insert into work.&tempds SET filepath="&folder";
5 
5 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` step is the core of the deletion logic. It iterates over each record in the `work.&tempds` table. For each `filepath`, it assigns a temporary file reference (`fref`) using the `filename()` function, then attempts to delete the file or folder with `fdelete()`. In case of an error (`rc` not equal to zero), a message is displayed in the SAS log. The file reference is then released.
Copied!
1 /* delete everything */
2 DATA _null_;
3 SET work.&tempds END=last;
4 LENGTH fref $8;
5 fref='';
6 rc=filename(fref,filepath);
7 rc=fdelete(fref);
8 IF rc THEN DO;
9 msg=sysmsg();
10 put "&sysmacroname:" / rc= / msg= / filepath=;
11 END;
12 rc=filename(fref);
13 RUN;
6 Code Block
PROC SQL
Explanation :
After all items have been deleted, this `PROC SQL` block deletes the temporary table `work.&tempds`, thus freeing up resources and cleaning the working environment.
Copied!
1/* tidy up */
2 
3PROC SQL;
4drop TABLE work.&tempds;
5 
7 Code Block
Macro SAS
Explanation :
This block contains the end of the conditional logic. If the initial folder was not valid or accessible (initial `%if` condition), an error message is displayed in the SAS log, indicating that the specified folder is not valid. Finally, `%mend mp_deletefolder;` marks the end of the macro definition.
Copied!
1%END;
2%ELSE %put &sysmacroname: &folder: is not a valid / accessible folder. ;
3%mend mp_deletefolder;
4 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de


Banner
Expert Advice
Expert
Simon
Expert SAS et fondateur.
« The mp_deletefolder macro follows a golden rule of file system management: a directory can only be deleted if it is strictly empty. The intelligence of this script lies in the use of a descending level sort (descending level). This "Bottom-Up" approach ensures that leaf nodes (files and deepest subfolders) are erased before SAS attempts to remove the parent containers, preventing the common "Directory not empty" errors encountered in standard linear processing. »