<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Delete a folder by deleting all files in all subfolders in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927550#M44751</link>
    <description>Works like charm. Thanks.</description>
    <pubDate>Wed, 08 May 2024 14:47:26 GMT</pubDate>
    <dc:creator>Babado</dc:creator>
    <dc:date>2024-05-08T14:47:26Z</dc:date>
    <item>
      <title>Delete a folder by deleting all files in all subfolders</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927498#M44749</link>
      <description>&lt;P&gt;I would like to delete the folder&amp;nbsp;folderToDelete, which contains subfolders and those folders might contain subfolders, and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not have permission to do the x command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following code (after looking at the following post &lt;A href="https://communities.sas.com/t5/SAS-Programming/delete-a-folder-directory-and-all-files-on-it/td-p/584870" target="_self"&gt;Delete folder (directory) and all files in it&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro delete_folder(folderToDelete);

data work.FilesToDelete;
   length Name $ 100;&lt;BR /&gt;
   keep Name;

   rc = filename("folder", "&amp;amp;folderToDelete.");
   dirId = dopen("folder");

   do i = 1 to dnum(dirID);
      Name = dread(dirId, i);
      output;
   end;

   rc = dclose(dirId);
run;

data _null_;
   set work.FilesToDelete end=lastDeleted;

   put "Deleting " Name;

   rc = filename("delfile", cats("&amp;amp;folderToDelete./", Name));
   rc = fdelete("delfile");
   put "del file " rc=;
   rc = filename("delfile");

   if lastDeleted then do;
      put "Deleting the folder '&amp;amp;folderToDelete.'";
      rc = filename("folder", "&amp;amp;folderToDelete.");
      rc = fdelete("folder");
      put "del folder " rc=;
      rc = filename("folder");
   end;
run;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, since my folder contains subfolders, only the tables are delete and not the subfolders, resulting in the main folder not being deleted. In that sense, what &lt;STRONG&gt;I would like was to apply a function recursively to delete every file inside any subfolder &lt;/STRONG&gt;(or any other suggestion you might have).&lt;BR /&gt;&lt;BR /&gt;As an example, if my folder has the following structure:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; folder&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;gt; subfolder_1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;gt; T_1_1.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;gt; T_1_2.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;gt; T_1_3.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;gt; subsubfolder_1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;gt; T_1_1_1.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;gt; subfolder_2&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;gt; T_2_1.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;gt; T_1.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;gt; T_2.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;gt; T_3.sas7bdat&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;gt; T_4.sas7bdat&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after running the first data step, which reads the folder, I get&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Name&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;subfolder_1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;subfolder_2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;T_1.sas7bdat&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;T_2.sas7bdat&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;T_3.sas7bdat&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;T_4.sas7bdat&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then in the second data step, only the tables are deleted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I would like to delete the entirety of the folder. Can someone help?&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 10:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927498#M44749</guid>
      <dc:creator>Babado</dc:creator>
      <dc:date>2024-05-08T10:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a folder by deleting all files in all subfolders</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927501#M44750</link>
      <description>&lt;P&gt;You could use the %dirtree macro that I found here:&amp;nbsp;&lt;A href="https://github.com/sasutils/macros/blob/master/dirtree.sas" target="_blank" rel="noopener"&gt;https://github.com/sasutils/macros/blob/master/dirtree.sas&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dirtree
  /*---------------------------------------------------------------------------
  Build dataset of files in directory tree(s)
  ----------------------------------------------------------------------------*/
  (directory    /* Pipe delimited directory list (default=.) */
  ,out=dirtree  /* Output dataset name */
  ,maxdepth=120 /* Maximum tree depth */
  );
  /*---------------------------------------------------------------------------
  Use SAS functions to gather list of files and directories

  directory - Pipe delimited list of top level directories

  out - Dataset to create
  maxdepth - Maximum depth of subdirectories to query

  Output dataset structure
  --NAME-- Len  Format      Description
  FILENAME $256             Name of file in directory
  TYPE       $1             File or Directory? (F/D)
  SIZE        8 COMMA20.    Filesize in bytes
  DATE        4 YYMMDD10.   Date file last modified
  TIME        4 TOD8.       Time of day file last modified
  DEPTH       3             Tree depth
  PATH     $256             Directory name

  Size is not available for the directories.
  LASTMOD timestamp is only available on Unix for directories.

  Will not scan the subtree of a directory with a path that is
  longer then 256 bytes. For such nodes TYPE will be set to L .

  ----------------------------------------------------------------------------*/
  %local fileref ;
  %let fileref=__FL__ ;
  %if 0=%length(&amp;amp;directory) %then %let directory=. ;

  * Setup dataset and seed with starting directory list ;
  data &amp;amp;out;
    length filename $256 type $1 size 8 date time 4 depth 3 path $256 ;
    retain filename ' ' depth 0 type ' ' date . time . size . ;
    format size comma20. date yymmdd10. time tod8. ;
    do _n_=1 to countw(symget('directory'),'|');
      path=scan(symget('directory'),_n_,'|');
      output;
    end;
  run;

  %* Allow use of empty OUT= dataset parameter ;
  %let out=&amp;amp;syslast;

  data &amp;amp;out;
    modify &amp;amp;out;
    retain sep "%sysfunc(ifc(&amp;amp;sysscp=WIN,\,/))";
    retain maxdepth &amp;amp;maxdepth;
  * Create FILEREF pointing to current file/directory ;
    rc1=filename("&amp;amp;fileref",catx('/',path,filename));
    if rc1 then do;
      length message $256;
      message=sysmsg();
      put 'ERROR: Unable to create fileref for ' path= filename= ;
      put 'ERROR- ' message ;
      stop;
    end;
  * Try to open as a directory to determine type ;
    did=dopen("&amp;amp;fileref");
    type = ifc(did,'D','F');
    if type='D' then do;
  * Make sure directory name is not too long to store. ;
      if length(catx('/',path,filename)) &amp;gt; vlength(path) then do;
        put 'NOTE: Directory name too long. ' path= filename= ;
        type='L';
        rc3=dclose(did);
      end;
      else do;
  * Move filename into the PATH and if on Unix set lastmod ;
        path=catx(sep,path,filename);
        filename=' ';
        if sep='/' then do;
          lastmod = input(dinfo(did,doptname(did,5)),nldatm100.);
          date=datepart(lastmod);
          time=timepart(lastmod);
        end;
      end;
    end;
    else do;
  * For a file try to open file and get file information ;
      fid=fopen("&amp;amp;fileref",'i',0,'b');
      if fid then do;
        lastmod = input(finfo(fid,foptname(fid, 5)), nldatm100.);
        date=datepart(lastmod);
        time=timepart(lastmod);
        size = input(finfo(fid,foptname(fid,ifn(sep='/',6,4))),32.);
        rc2 = fclose(fid);
      end;
    end;
  * Update the observation in the dataset ;
    replace;
    if type='D' then do;
  * When current file is a directory add directory members to dataset ;
      depth=depth+1;
      if depth &amp;gt; maxdepth then put 'NOTE: ' maxdepth= 'reached, not reading members of ' path= ;
      else do i=1 to dnum(did);
        filename=dread(did,i);
        output;
      end;
      rc3=dclose(did);
    end;
  * Clear the fileref ;
    rc4=filename("&amp;amp;fileref");
  run;

%mend dirtree;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Then use the macro to create a directory listing and delete all listed files and folders.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let topFolderToDelete=c:\temp\deldirtest;

/* create table work.dirtree with directory listing */
%dirtree(&amp;amp;topFolderToDelete);
proc sort data=work.dirtree;
  by DESCENDING path DESCENDING type;
run;

/** delete files and folders **/
/* 1. folder separator \ or / (Win or Unix/Linux)? */
data _null_;
  set work.dirtree;
  if find(path,'\') then sep='\';
  else sep='/';
  if not missing(sep) then 
    do;
      call symputx('sep',sep);
      stop;
    end;
run;

/* 2. delete files and folders */
data _null_;
  set work.dirtree;
  by DESCENDING path DESCENDING type;
  fname='tempfile';
  if type='F' then
    rc=filename(fname, catx("&amp;amp;sep",path,filename));
  else
    rc=filename(fname, path);
  if rc = 0 and fexist(fname) then
     rc=fdelete(fname);
  rc=filename(fname);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 12:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927501#M44750</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-05-08T12:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a folder by deleting all files in all subfolders</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927550#M44751</link>
      <description>Works like charm. Thanks.</description>
      <pubDate>Wed, 08 May 2024 14:47:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/927550#M44751</guid>
      <dc:creator>Babado</dc:creator>
      <dc:date>2024-05-08T14:47:26Z</dc:date>
    </item>
    <item>
      <title>Re: Delete a folder by deleting all files in all subfolders</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/943943#M45230</link>
      <description>There is a new way now: use the GIT_DELETE_REPO function(&lt;A href="https://go.documentation.sas.com/doc/en/workbenchcdc/v_001/vwblefunctionsref/n05xa2vo2wnzzon1ujsxkgduv1bh.htm" target="_blank"&gt;https://go.documentation.sas.com/doc/en/workbenchcdc/v_001/vwblefunctionsref/n05xa2vo2wnzzon1ujsxkgduv1bh.htm&lt;/A&gt;)&lt;BR /&gt;&lt;BR /&gt;Don't be fuzzy about the "GIT" name, this function can be used to delete local file folders, with or without files in it.</description>
      <pubDate>Sat, 14 Sep 2024 09:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Delete-a-folder-by-deleting-all-files-in-all-subfolders/m-p/943943#M45230</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-09-14T09:24:50Z</dc:date>
    </item>
  </channel>
</rss>

