<?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 List all files in a directory and dates they were last modified and originally created in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867416#M342589</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used part of this program to create a file to read a directory and list all the files and their paths. However, I would like to add a part which will add a column which tells me when the file was created and also when it was last modified.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would someone be able to help me amend this please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dirs_found (compress=no);
 length Root $120.;
 root = "/my directory";
 output;
run;
data
 dirs_found /* Updated list of directories searched */
 files_found (compress=no); /* Names of files found. */
 keep Path FileName FileType modified;
 length fref $8 Filename $120 FileType $16 ;
 /* Read the name of a directory to search. */
 modify dirs_found;
 /* Make a copy of the name, because we might reset root. */
 Path = root;
 /* For the use and meaning of the FILENAME, DOPEN, DREAD, MOPEN, and */
 /* DCLOSE functions, see the SAS OnlineDocs. */
 rc = filename(fref, path);
 if rc = 0 then
 do;
 did = dopen(fref);
 rc = filename(fref);
 end;
 else
 do;
 length msg $200.;
 msg = sysmsg();
 putlog msg=;
 did = .;
 end;
 if did &amp;lt;= 0
 then
 do;
 putlog 'ERR' 'OR: Unable to open ' Path=;
 return;
 end;
 dnum = dnum(did);
 do i = 1 to dnum;
 filename = dread(did, i);
 fid = mopen(did, filename);
 /* It's not explicitly documented, but the SAS online */
 /* examples show that a return value of 0 from mopen */
 /* means a directory name, and anything else means */
 /* a file name. */
 if fid &amp;gt; 0
 then
 do;
 /* FileType is everything after the last dot. If */
 /* no dot, then no extension. */
 FileType = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
 if filename = filetype then filetype = ' ';
 output files_found;
 end;
 else
 do;
 /* A directory name was found; calculate the complete */
 /* path, and add it to the dirs_found data set, */
 /* where it will be read in the next iteration of this */
 /* data step. */
 root = catt(path, "\", filename);
 output dirs_found;
 end;
 end;
 rc = dclose(did);
run;&lt;/PRE&gt;</description>
    <pubDate>Fri, 31 Mar 2023 10:34:38 GMT</pubDate>
    <dc:creator>Sean_OConnor</dc:creator>
    <dc:date>2023-03-31T10:34:38Z</dc:date>
    <item>
      <title>List all files in a directory and dates they were last modified and originally created</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867416#M342589</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used part of this program to create a file to read a directory and list all the files and their paths. However, I would like to add a part which will add a column which tells me when the file was created and also when it was last modified.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would someone be able to help me amend this please?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dirs_found (compress=no);
 length Root $120.;
 root = "/my directory";
 output;
run;
data
 dirs_found /* Updated list of directories searched */
 files_found (compress=no); /* Names of files found. */
 keep Path FileName FileType modified;
 length fref $8 Filename $120 FileType $16 ;
 /* Read the name of a directory to search. */
 modify dirs_found;
 /* Make a copy of the name, because we might reset root. */
 Path = root;
 /* For the use and meaning of the FILENAME, DOPEN, DREAD, MOPEN, and */
 /* DCLOSE functions, see the SAS OnlineDocs. */
 rc = filename(fref, path);
 if rc = 0 then
 do;
 did = dopen(fref);
 rc = filename(fref);
 end;
 else
 do;
 length msg $200.;
 msg = sysmsg();
 putlog msg=;
 did = .;
 end;
 if did &amp;lt;= 0
 then
 do;
 putlog 'ERR' 'OR: Unable to open ' Path=;
 return;
 end;
 dnum = dnum(did);
 do i = 1 to dnum;
 filename = dread(did, i);
 fid = mopen(did, filename);
 /* It's not explicitly documented, but the SAS online */
 /* examples show that a return value of 0 from mopen */
 /* means a directory name, and anything else means */
 /* a file name. */
 if fid &amp;gt; 0
 then
 do;
 /* FileType is everything after the last dot. If */
 /* no dot, then no extension. */
 FileType = prxchange('s/.*\.{1,1}(.*)/$1/', 1, filename);
 if filename = filetype then filetype = ' ';
 output files_found;
 end;
 else
 do;
 /* A directory name was found; calculate the complete */
 /* path, and add it to the dirs_found data set, */
 /* where it will be read in the next iteration of this */
 /* data step. */
 root = catt(path, "\", filename);
 output dirs_found;
 end;
 end;
 rc = dclose(did);
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Mar 2023 10:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867416#M342589</guid>
      <dc:creator>Sean_OConnor</dc:creator>
      <dc:date>2023-03-31T10:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: List all files in a directory and dates they were last modified and originally created</title>
      <link>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867418#M342591</link>
      <description>&lt;P&gt;I suggest you to use code from&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank" rel="noopener"&gt;@Kurt_Bremser&lt;/A&gt;'s&amp;nbsp;"Talking to Your Host" article from WUSS 2022 (&lt;A href="https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/838344" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/8...&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;The code would be like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let filesLocation=C:\SAS_WORK\;

/* get full list of files from &amp;amp;filesLocation. (including all subdirectories) */
data files_to_get; /* just to be sure the file "files"  is "empty" */
run;

data files_to_get;
  length root dname $ 2048 filename $ 256 dir level 8;
  root = "&amp;amp;filesLocation.";
  retain filename dname ' ' level 0 dir 1;
  label 
    filename = "file"
    dname = "folder"
    ;
run;

data files_to_get;
  modify files_to_get;
  rc1=filename('tmp',catx('/',root,dname,filename));
  rc2=dopen('tmp');
  dir = 1 &amp;amp; rc2;
  if dir then 
    do;
      dname=catx('/',dname,filename);
      filename=' ';
    end;
  replace;

  if dir;

  level=level+1;

  do i=1 to dnum(rc2);
    filename=dread(rc2,i);
    output;
  end;
  rc3=dclose(rc2); 
run;

/* get files info in "Long" format */
data files_to_get_dates;
  set files_to_get(where=(dir=0));
  fname="tempfile";
  rc=filename(fname, catx('/',root,dname,filename));
  if rc = 0 and fexist(fname) then
     do;
        fid=fopen(fname);
        infonum=foptnum(fid);
           do i=1 to infonum;
              infoname=foptname(fid, i);
              infoval=finfo(fid, infoname);
              output;
           end;

        fid=fclose(fid);
     end;
  rc=filename(fname);

  drop dir--i;
run;

/* transforms files info into "Wide" format */
proc transpose 
  data = files_to_get_dates(rename=(filename=fn)) 
  out=files_to_get_dates_T(drop =_name_);
  by root dname fn notsorted;
  id infoname;
  var infoval;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 11:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/List-all-files-in-a-directory-and-dates-they-were-last-modified/m-p/867418#M342591</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-03-31T11:16:50Z</dc:date>
    </item>
  </channel>
</rss>

