<?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: List of Files in Directory and Subdirectories with File Attributes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696137#M212572</link>
    <description>&lt;P&gt;If you have XCMD enabled, use the proper external command (find with .ls) in a FILENAME PIPE and read the output.&lt;/P&gt;
&lt;P&gt;If not, here's a very neat piece of code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;that does it in a single data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist;
  length dname filename $256 dir level 8 lastmod size 8;
  format lastmod datetime20.;
  input dname;
  retain filename ' ' level 0 dir 1;
cards4;
/folders/myfolders
;;;;

data filelist;
  modify filelist;
  rc1=filename('tmp',catx('/',dname,filename));
  rc2=dopen('tmp');
  dir = not not rc2;
  if not dir then do;
    fid=fopen('tmp','i',0,'b');
    lastmod=input(finfo(fid,foptname(fid,5)),NLDATM100.);
    size=input(finfo(fid,foptname(fid,6)),32.);
    fid=fclose(fid);
  end;
  else do;
    dname=catx('/',dname,filename);
    filename=' ';
    lastmod=input(dinfo(rc2,doptname(rc2,5)),NLDATM100.);
  end;
  replace;
  if dir;
  level=level+1;
  do i=1 to dnum(rc2);
    filename=dread(rc2,i);
    output;
  end;
  rc3=dclose(rc2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Nov 2020 10:06:15 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-03T10:06:15Z</dc:date>
    <item>
      <title>List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696059#M212517</link>
      <description>&lt;P&gt;I'm trying to get a list of files in a directory and the subdirectories with file attributes, in particular the size of each file.&lt;/P&gt;
&lt;P&gt;the files are listed in the main directory, but there a bunch of subdirectories with files.&amp;nbsp; I've been using the following code, but&lt;/P&gt;
&lt;P&gt;have not figured out how to get the size of the file, it only gives the directory and file.&amp;nbsp; I've used the FINFO function, but doesn't give the size.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro list_files(dir,ext);&lt;BR /&gt;%local filrf rc did memcnt name i;&lt;BR /&gt;%let rc=%sysfunc(filename(filrf,&amp;amp;dir));&lt;BR /&gt;%let did=%sysfunc(dopen(&amp;amp;filrf));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if &amp;amp;did eq 0 %then %do; &lt;BR /&gt;%put Directory &amp;amp;dir cannot be open or does not exist;&lt;BR /&gt;%return;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i = 1 %to %sysfunc(dnum(&amp;amp;did));&lt;/P&gt;
&lt;P&gt;%let name=%qsysfunc(dread(&amp;amp;did,&amp;amp;i));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* %if %qupcase(%qscan(&amp;amp;name,-1,.)) = %upcase(&amp;amp;ext) %then %do;*/&lt;BR /&gt;%if %qscan(&amp;amp;name,2,.) ne %then %do;&lt;BR /&gt;%put &amp;amp;dir/&amp;amp;name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _tmp;&lt;BR /&gt;length dir $512 name $100;&lt;BR /&gt;dir=symget("dir");&lt;BR /&gt;name=symget("name");&lt;/P&gt;
&lt;P&gt;size=FINFO(OPEN(symget("name")), 'FILE SIZE (BYTES)');&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc append base=want&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; data&amp;nbsp; =_tmp;&lt;BR /&gt;run;quit;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%else %if %qscan(&amp;amp;name,2,.) = %then %do; &lt;BR /&gt;%list_files(&amp;amp;dir/&amp;amp;name,&amp;amp;ext)&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%let rc=%sysfunc(dclose(&amp;amp;did));&lt;BR /&gt;%let rc=%sysfunc(filename(filrf));&lt;/P&gt;
&lt;P&gt;%mend list_files;&lt;BR /&gt;%list_files(/gpfsFS2/sasdata/adhoc/pofin/nps/npsts/prod/cera/data,*)&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2020 00:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696059#M212517</guid>
      <dc:creator>suncawy</dc:creator>
      <dc:date>2020-11-03T00:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696071#M212524</link>
      <description>&lt;P&gt;Try the FOPEN function. The OPEN function is for SAS datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also I think you need to assign a FILEREF for the file you want to open then refer to that, not the actual file name:&lt;/P&gt;
&lt;PRE&gt;rc=filename("curdirfl", "c:\temp\MytestFile.txt");
fid=fopen("curdirfl", "i") ;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Nov 2020 01:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696071#M212524</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-11-03T01:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696137#M212572</link>
      <description>&lt;P&gt;If you have XCMD enabled, use the proper external command (find with .ls) in a FILENAME PIPE and read the output.&lt;/P&gt;
&lt;P&gt;If not, here's a very neat piece of code from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;that does it in a single data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filelist;
  length dname filename $256 dir level 8 lastmod size 8;
  format lastmod datetime20.;
  input dname;
  retain filename ' ' level 0 dir 1;
cards4;
/folders/myfolders
;;;;

data filelist;
  modify filelist;
  rc1=filename('tmp',catx('/',dname,filename));
  rc2=dopen('tmp');
  dir = not not rc2;
  if not dir then do;
    fid=fopen('tmp','i',0,'b');
    lastmod=input(finfo(fid,foptname(fid,5)),NLDATM100.);
    size=input(finfo(fid,foptname(fid,6)),32.);
    fid=fclose(fid);
  end;
  else do;
    dname=catx('/',dname,filename);
    filename=' ';
    lastmod=input(dinfo(rc2,doptname(rc2,5)),NLDATM100.);
  end;
  replace;
  if dir;
  level=level+1;
  do i=1 to dnum(rc2);
    filename=dread(rc2,i);
    output;
  end;
  rc3=dclose(rc2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Nov 2020 10:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/696137#M212572</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-03T10:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/707622#M217285</link>
      <description>&lt;P&gt;Can we get the owner of the file/directory as well (ie:who created/modified)&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 10:03:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/707622#M217285</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2020-12-22T10:03:58Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/707632#M217287</link>
      <description>&lt;P&gt;You can get the owner, but not the modifier if that is different from the owner (if a file is group-and/or world-writable). Modifiers are not recorded in any operating system I know of, unless you run some kind of auditing software; in which case, the modifier would still not be stored in file metadata, but in a separate database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The owner is file info item #2 (at least on UNIX).&lt;/P&gt;
&lt;P&gt;See this example code to get a feel for the construct:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data check;
length fref $8;
fid = filename(fref,"insert your file path here");
fid = fopen(fref);
do i = 1 to foptnum(fid);
  opt = foptname(fid,i);
  value = finfo(fid,opt);
  output;
end;
fid = fclose(fid);
fid = filename(fref);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will show you all available information items for your platform, and the values for the file selected.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 11:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/707632#M217287</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-22T11:10:38Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773166#M245560</link>
      <description>&lt;P&gt;This is a great solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know how I can add the owner (person who created the files) to the report?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DarrylLawrence_1-1633727641322.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64548i3577C71C295AE55F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DarrylLawrence_1-1633727641322.png" alt="DarrylLawrence_1-1633727641322.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DarrylLawrence_2-1633727783548.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64549i1F3F5EF5B7A76356/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DarrylLawrence_2-1633727783548.png" alt="DarrylLawrence_2-1633727783548.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Oct 2021 21:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773166#M245560</guid>
      <dc:creator>DarrylLawrence</dc:creator>
      <dc:date>2021-10-08T21:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773189#M245579</link>
      <description>&lt;P&gt;Please show your code.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 05:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773189#M245579</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-09T05:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773194#M245583</link>
      <description>&lt;P&gt;This is the code I am usign - copied from this blog&lt;BR /&gt;&lt;BR /&gt;I need to add the owner of the files - so we can send out reports to the owners to delete obsolete files.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;data filelist;&lt;BR /&gt;length dname filename $256 dir level 8 lastmod size 8 user $32 ;&lt;BR /&gt;format lastmod datetime20.;&lt;BR /&gt;input dname;&lt;BR /&gt;retain filename ' ' level 0 dir 1;&lt;BR /&gt;cards4;&lt;BR /&gt;/sas/analytics/sasdata/users&lt;BR /&gt;/sas/analytics/projects&lt;BR /&gt;;;;;&lt;/P&gt;
&lt;P&gt;data filelist;&lt;BR /&gt;modify filelist;&lt;BR /&gt;rc1=filename('tmp',catx('/',dname,filename));&lt;/P&gt;
&lt;P&gt;rc2=dopen('tmp');&lt;BR /&gt;dir = not not rc2;&lt;BR /&gt;if not dir then do;&lt;BR /&gt;fid=fopen('tmp','i',0,'b');&lt;BR /&gt;lastmod=input(finfo(fid,foptname(fid,5)),NLDATM100.);&lt;BR /&gt;size=input(finfo(fid,foptname(fid,6)),32.);&lt;BR /&gt;fid=fclose(fid);&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;dname=catx('/',dname,filename);&lt;BR /&gt;filename=' ';&lt;BR /&gt;lastmod=input(dinfo(rc2,doptname(rc2,5)),NLDATM100.);&lt;BR /&gt;end;&lt;BR /&gt;replace;&lt;BR /&gt;if dir;&lt;BR /&gt;level=level+1;&lt;BR /&gt;do i=1 to dnum(rc2);&lt;BR /&gt;filename=dread(rc2,i);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;rc3=dclose(rc2);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 07:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773194#M245583</guid>
      <dc:creator>DarrylLawrence</dc:creator>
      <dc:date>2021-10-09T07:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773200#M245585</link>
      <description>&lt;P&gt;Add it here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if not dir then do;
  fid=fopen('tmp','i',0,'b');
  owner = finfo(fid,foptname(fid,2));
  lastmod=input(finfo(fid,foptname(fid,5)),NLDATM100.);
  size=input(finfo(fid,foptname(fid,6)),32.);
  fid=fclose(fid);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Set a proper length for the variable.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 08:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773200#M245585</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-09T08:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773231#M245597</link>
      <description>&lt;P&gt;Thank you, it worked!&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2021 19:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/773231#M245597</guid>
      <dc:creator>DarrylLawrence</dc:creator>
      <dc:date>2021-10-09T19:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/797459#M287552</link>
      <description>&lt;P&gt;How to use it?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;14815 data check;&lt;BR /&gt;14816 length fref $8;&lt;BR /&gt;14817 fid = filename(fref,"d:\");&lt;BR /&gt;14818 fid = fopen(fref);&lt;BR /&gt;14819 do i = 1 to foptnum(fid);&lt;BR /&gt;14820 opt = foptname(fid,i);&lt;BR /&gt;14821 value = finfo(fid,opt);&lt;BR /&gt;14822 output;&lt;BR /&gt;14823 end;&lt;BR /&gt;14824 fid = fclose(fid);&lt;BR /&gt;14825 fid = filename(fref);&lt;BR /&gt;14826 run;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 11:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/797459#M287552</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-02-20T11:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/837271#M331040</link>
      <description>&lt;P&gt;How does one use this code? Is it only replacing the "/folders/myfolders" with the desired directory?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 06 Oct 2022 20:08:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/837271#M331040</guid>
      <dc:creator>torvyle</dc:creator>
      <dc:date>2022-10-06T20:08:58Z</dc:date>
    </item>
    <item>
      <title>Re: List of Files in Directory and Subdirectories with File Attributes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/837319#M331047</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148102"&gt;@torvyle&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How does one use this code? Is it only replacing the "/folders/myfolders" with the desired directory?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You insert the root directories of the trees you want to search in in the DATALINES block of the first data step (replace /folders/myfolders, as that was the data directory used in the no longer available SAS University Edition).&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2022 06:20:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-of-Files-in-Directory-and-Subdirectories-with-File/m-p/837319#M331047</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-07T06:20:02Z</dc:date>
    </item>
  </channel>
</rss>

