Home

Resume

My SAS

Research

My SAS

Family

Feedback

Home

 

My SAS - SAS Code


Back to SAS Code

List sub-directories in a folder


SAS Code:

/*

          Name:  list_directories.sas
   Description:  List sub directories in a directory

         Input:  path: the directory need to be processed

        Output:  Global macro variables:
                 path_dir_?:  path and directory name. ? is a number from 1 to &dim_i.
                 dim_i: number of macro variable.

   Example:
         %include "c:\zhu_test\test8\list_directories.sas"
         %list_files(path=c:\zsl_test);


   References:  dopen, dread, dnum, dclose.

         History:   Date        Name                  Comments
                    04/15/06    Songlin Zhu           created

*/

%macro list_files(path=);
   %global dim_i path_dir_1;
   %local filrf rc_filen rc_dopen rc_dclose filrf1 rc_filen1 rc_dopen1 rc_dnum1 rc_dclose1 filrf2 rc_filen2 rc_dopen3 rc_dclose3;
   %let path = %left(%trim(&path.));
   %put ;
   %put The program is finding the subdirectories in &path., please wait ...  ;
   %put ;

   %if "%substr(&path., %length(&path.), 1)" = "\" %then
      %do;
         %let path = %substr(&path., 1, %eval(%length(&path.)-1));
      %end;

   %let path_dir_temp_1 = &path;
   %let dim_i = 0;
   %let dim_j = 1;

   %let filrf=MYDIR;
   %let rc_filen=%sysfunc(filename(filrf,&path_dir_temp_1.));
   %let rc_dopen=%sysfunc(dopen(&filrf));
  /* %if &rc_dopen. > 0 %then %do; %let rc_dclose = %sysfunc(dclose(&rc_dopen.));   %end; */

   %if &rc_dopen. > 0 %then
      %do;
          %let dim_i = 1;
          %let path_dir_1 = &path.;
          %do %while(&dim_j. > 0);

             %let dim_k = 0;
             %do k = 1 %to &dim_j.;
                %let path = &&&path_dir_temp_&k.;

                /* Determine sub-directories in each directory (&path) */
                %let filrf1=MYDIR1;
                %let rc_filen1=%sysfunc(filename(filrf1,&path.));
                %let rc_dopen1=%sysfunc(dopen(&filrf1));

                %if &rc_dopen1 > 0 %then
                   %do;
                      /* Get number of files and sub-directories in directory */
                      %let rc_dnum1  = %sysfunc(dnum(&rc_dopen1.));

                      %if &rc_dnum1 > 0 %then
                         %do;
                            %do i = 1 %to &rc_dnum1.;
                                /* Get name of a file or directory */
                                %let rc_dread1 = %sysfunc(dread(&rc_dopen1., &i.));
                                %let path_temp = %trim(%left(&path.))\%trim(%left(&rc_dread1.));

                                %let filrf1=MYDIR3;
                                %let rc_filen3=%sysfunc(filename(filrf3,&path_temp.));
                                %let rc_dopen3=%sysfunc(dopen(&filrf3));

                                /* If it is a directory, save the path and name to a macro variable &path_dir_? */
                                /* and also save it to &path_dir_temp_? for finding sub-directories             */
                                %if &rc_dopen3 > 0 %then
                                   %do;
                                      %let dim_k = %eval(&dim_k. + 1);
                                      %let dim_i = %eval(&dim_i. + 1);
                                      %global path_dir_&dim_i.;

                                      %let path_dir_&dim_i.  = &path_temp.;
                                      %let path_dir_temp_&dim_k. = &path_temp.;

                                      %let rc_dclose3 = %sysfunc(dclose(&rc_dopen3.));
                                   %end;
                                %else
                                   %do;

                                   %end;
                            %end;
                      %end;

                   %let rc_dclose1=%sysfunc(dclose(&rc_dopen1));
                %end;
                /* End 3  */

             %end;

             %let dim_j = &dim_k.;

          %end;
          %let rc_dclose = %sysfunc(dclose(&rc_dopen.));
      %end;
   %else
      %do;
         %put The directory does not exist;
      %end;
 
   %put Finding subdirectories finished.;
   %put ;
%mend list_files;


If you have any questions, feel free to send me feedback.