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?
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.
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.
Many thanks for your solution and for taking the time to elaborate on it. It was very helpful to me
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.