DATA Step, Macro, Functions and more

Delete hidden files

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Delete hidden files

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?


Accepted Solutions
Solution
‎10-19-2015 09:09 AM
PROC Star
Posts: 1,322

Re: Delete hidden files

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.

View solution in original post


All Replies
Solution
‎10-19-2015 09:09 AM
PROC Star
Posts: 1,322

Re: Delete hidden files

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.
New Contributor
Posts: 3

Re: Delete hidden files

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.

 

PROC Star
Posts: 1,322

Re: Delete hidden files

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.

 

New Contributor
Posts: 3

Re: Delete hidden files

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 279 views
  • 0 likes
  • 2 in conversation