BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I want to count the number of files in specific folder and specific kind of files. How I can achieve it in efficient way. I am looking because depending on the count I need to run another macro.  In the image I mentioned  How my folder structure and also how I want a outputs ('account' is the major folder which have Two subfolders with 'Expense' and 'Profits' which contains files.)

SASuserlot_1-1667666291170.png

Thank you for your inputs.


data filenames_;
	length fref $8 fname $200;
	did = filename(fref,"c:\documents\accounts");
	did = dopen(fref);
/*	count=  dnum(did);*/
	do i = 1 to dnum(did);
		  fname = dread(did,i);
;
		  output;
	end;
did = dclose(did);
did = filename(fref);
keep fname;
run;
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

If option XCMD set then I find it coding wise often easier and "shorter" to use an OS command for recursive file listings. Below how this could look-like for Windows.

data work.path_and_filename;
  infile 'dir "c:\documents\accounts\*" /B/S/A-D/ON' pipe truncover;
  input path_file $1000.;
  length path $1000 file_name $60 suffix $10.;
  path=substr(path_file,1,findc(path_file,'\',-1000)-1);
  file_name=scan(path_file,-1,'\');
  suffix=scan(path_file,-1,'.');
  if findc(suffix,'\') then call missing(suffix);
  /* restrict to certain suffixes */
  /*if lowcase(suffix) in ('sas','txt','csv');*/
run;

proc sql;
  create table counts as
  select 
    path,
    suffix,
    count(*) as n_files
  from work.path_and_filename
  group by
    path,
    suffix
  ;
quit;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

PROC FREQ can do this simple counting.

--
Paige Miller
SASuserlot
Barite | Level 11

Thanks, I will try that way, I have like hundreds of subfolders ( I thought Freq is not efficient) so, I am looking this way where it automatically goes to the path provided and counts the file number.

Tom
Super User Tom
Super User

@SASuserlot wrote:

Thanks, I will try that way, I have like hundreds of subfolders ( I thought Freq is not efficient) so, I am looking this way where it automatically goes to the path provided and counts the file number.


Do you mean you want to subset the list of files to just those for a particular directory?

 

If you have a dataset that like the one generated by this macro:  https://github.com/sasutils/macros/blob/master/dirtree.sas

You could count how many files are directly in each directory in the tree by aggregating by the PATH variable.

 

But what do you mean by TYPE?  Are you talking about the extension on the file?  You could create a variable that has just the extension part of the filename (the part after the last period).

 

data want;
  set dirtree;
  if index(filename,'.') then extension=scan(filename,-1,'.');
run;

You could then summarize by the extension in each directory.

proc freq data=want;
  tables path*extension / list;
run;
PaigeMiller
Diamond | Level 26

@SASuserlot wrote:

Thanks, I will try that way, I have like hundreds of subfolders ( I thought Freq is not efficient) so, I am looking this way where it automatically goes to the path provided and counts the file number.


FREQ is very efficient. And with "hundreds" of folders (as opposed to millions), its hard to imagine you'll find a noticeably faster way. Certainly you could do lots of coding and achieve small increases in speed.

--
Paige Miller
Patrick
Opal | Level 21

If option XCMD set then I find it coding wise often easier and "shorter" to use an OS command for recursive file listings. Below how this could look-like for Windows.

data work.path_and_filename;
  infile 'dir "c:\documents\accounts\*" /B/S/A-D/ON' pipe truncover;
  input path_file $1000.;
  length path $1000 file_name $60 suffix $10.;
  path=substr(path_file,1,findc(path_file,'\',-1000)-1);
  file_name=scan(path_file,-1,'\');
  suffix=scan(path_file,-1,'.');
  if findc(suffix,'\') then call missing(suffix);
  /* restrict to certain suffixes */
  /*if lowcase(suffix) in ('sas','txt','csv');*/
run;

proc sql;
  create table counts as
  select 
    path,
    suffix,
    count(*) as n_files
  from work.path_and_filename
  group by
    path,
    suffix
  ;
quit;
SASuserlot
Barite | Level 11

Thank you @Patrick  , It worked for what I am looking.

Special thank you @PaigeMiller @Tom  for taking your time and provide the valuable alternatives. I tried the your approach as well. Which do the same job as expected. I really thank you for all your time.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2297 views
  • 3 likes
  • 4 in conversation