SASUnit Examples  Version 1.5.0
windows/_dir.sas
Go to the documentation of this file.
1 
28 %MACRO _dir (i_path=
29  ,i_recursive=0
30  ,o_out=dir
31  );
32 
33  %local dirindicator_en dirindicator_de encoding s dirfile xwait xsync xmin l_i_path;
34  %let dirindicator_en=Directory of;
35  %let dirindicator_de=Verzeichnis von;
36  %let encoding=pcoem850;
37  %let l_i_path = %sysfunc(translate(&i_path,\,/));
38 
39  proc sql noprint;
40  create table &o_out (filename char(255));
41  quit;
42  %IF &syserr NE 0 %THEN %GOTO errexit;
43 
44  %let xwait=%sysfunc(getoption(xwait));
45  %let xsync=%sysfunc(getoption(xsync));
46  %let xmin =%sysfunc(getoption(xmin));
47 
48  options noxwait xsync xmin;
49 
50  %let dirfile=%sysfunc(pathname(work))\___dir.txt;
51  filename _dirfile "&dirfile" encoding=&encoding;
52 
53  /* %put &g_note.(SASUNIT): Directory search is: &l_i_path; */
54 
55  %IF &i_recursive %then %let s=/S;
56 
57  %if &g_verbose. %then %do;
58  %put ======== OS Command Start ========;
59  /* Evaluate sysexec´s return code */
60  %SYSEXEC(dir &s /a-d "&l_i_path" > "&dirfile" 2>&1);
61  %if &sysrc. = 0 %then %put &g_note.(SASUNIT): Sysrc : 0 -> SYSEXEC SUCCESSFUL;
62  %else %put &g_error.(SASUNIT): Sysrc : &sysrc -> An Error occured;
63 
64  /* put sysexec command to log*/
65  %put &g_note.(SASUNIT): SYSEXEC COMMAND IS: dir &s /a-d "&l_i_path" > "&dirfile";
66 
67  /* write &dirfile to the log*/
68  data _null_;
69  infile "&dirfile" truncover lrecl=512;
70  input line $512.;
71  putlog line;
72  run;
73  %put ======== OS Command End ========;
74  %end;
75 
76  %SYSEXEC(dir &s /a-d "&l_i_path" > "&dirfile");
77  options &xwait &xsync &xmin;
78 
79  data &o_out (keep=membername filename changed);
80  length membername dir filename $255 language $2 tstring dateformat timeformat $40;
81  retain language "__" dir FilePos dateformat timeformat Detect_AM_PM;
82  infile _dirfile truncover;
83  input line $char255.;
84  if index (line, "&dirindicator_en") or index (line, "&dirindicator_de") then do;
85  if index (line, "&dirindicator_en") then do;
86  dir = substr(line, index (line, "&dirindicator_en")+length("&dirindicator_en")+1);
87  end;
88  else do;
89  dir = substr(line, index (line, "&dirindicator_de")+length("&dirindicator_de")+1);
90  end;
91  end;
92  if substr(line,1,1) ne ' ' then do;
93  * Check for presence of AM/PM in time value, because you can specify AM/PM timeformat in German Windows *;
94  if (language = "__") then do;
95  Detect_AM_PM = upcase (scan (line, 3, " "));
96  if (Detect_AM_PM in ("AM", "PM")) then do;
97  Filenamepart = scan (line,5, " ");
98  Filepos = index (line, trim(Filenamepart));
99  language = "EN";
100  dateformat = "mmddyy10.";
101  timeformat = "time9.";
102  end;
103  else do;
104  Filenamepart = scan (line,4, " ");
105  Filepos = index (line, trim(Filenamepart));
106  language = "DE";
107  dateformat = "ddmmyy10.";
108  timeformat = "time5.";
109  end;
110  end;
111  if ("&G_DATEFORMAT." ne "_NONE_") then do;
112  dateformat = "&G_DATEFORMAT.";
113  line = tranwrd (line, "Mrz", "Mär");
114  end;
115  d = inputn (scan (line,1,' '), dateformat);
116  tstring = trim (scan (line,2,' '));
117  if (Detect_AM_PM in ("AM", "PM")) then do;
118  tstring = trim (scan (line,2,' ')) !! ' ' !! trim (scan (line, 3, ' '));
119  end;
120  t = inputn (tstring, timeformat);
121  changed = dhms (d, hour(t), minute(t), 0);
122  format changed datetime20.;
123  membername = translate(substr (line,FilePos),'/','\');
124  filename = translate(trim(dir),'/','\') !! '/' !! membername;
125 
126  output;
127  end;
128  run;
129 
130  filename _dirfile;
131 
132 %errexit:
133 %MEND _dir;