A directory listing may be a useful tool in documenting the work performed on a project. A list of all the directories and files involved could be annotated with a text editor so that new personnel assigned to a project would not have to excavate through countless directories to gain an understanding of the code upon which a project was built.
It is straightforward to create a directory listing of all files in directories and subdirectories (folders and subfolders) using SAS for Windows. Simply by using the pipe device type on a FILENAME statement, you can invoke the DOS DIR command and process the records returned in a DATA step. For example, the following code shows how this might be accomplished.
filename DIRLIST pipe 'dir "C:\Documents and Settings" /s';
data dirlist ;
length buffer $256 ;
infile dirlist length=reclen ;
input buffer $varying256. reclen ;
run ;
|
The DOS DIR command will be executed when the DATA step is run. The "/s" parameter indicates that subdirectories (subfolders) will be recursively processed. All of the records returned by the DIR command will be input to the DATA step and will be available for processing. The complete listing for the path c:\Documents and Settings\robett\My Documents\My SAS Files\V8\RandD\DirlistWin is shown here:
1 Volume in drive C is WXP 2 Volume Serial Number is 18C2-3BAA 3 4 Directory of C:\Documents and Settings\robett\My Documents\My SAS Files\V8\RandD\DirlistWin 5 6 03/05/04 08:53 AM <DIR> . 7 03/05/04 08:53 AM <DIR> .. 8 02/13/02 10:09 AM 41,472 Backup of Creating A Directory Listing Using SAS for Win 9 03/05/04 08:53 AM 24,064 Creating A Directory Listing Using SAS for Windows is No 10 03/05/04 08:40 AM 7,589 dirlistWin.sas 11 03/05/04 08:40 AM 3,387 testDirlistWin.log 12 03/05/04 08:40 AM 125,152 testDirlistWin.lst 13 03/05/04 08:39 AM 149 testDirlistWin.sas 14 6 File(s) 201,813 bytes 15 2 Dir(s) 16,306,119,168 bytes free
There are many records returned that may be of no interest to you, such as the initial and final results returned by the DIR command:
1 Volume in drive C is WXP 2 Volume Serial Number is 18C2-3BAA ... 14 6 File(s) 201,813 bytes 15 2 Dir(s) 16,306,119,168 bytes free
Additional DATA step processing is required to parse out the wheat from the chaff and yield useful information.
The %DIRLISTWIN macro is designed to produce a directory listing of a Windows folder and subfolders and to (optionally) produce a report and/or a SAS dataset containing the files in each folder processed. Also, it can filter files based on their size and/or date, including or excluding them as specified. The macro parameters are given below.
%macro DIRLISTWIN( PATH /* Windows path of directory to examine */ , MAXDATE= /* [optional] maximum date/time of file to report */ , MINDATE= /* [optional] minimum date/time of file to report */ , MAXSIZE= /* [optional] maximum size of file to report */ , MINSIZE= /* [optional] minimum size of file to report */ , OUT= /* [optional] name of output file */ , REPORT=Y /* [optional] flag controlling report creation */ , SUBDIR=Y /* [optional] include subdirectories in processing */ ) |
Note: click the Downloads tab to get the %DIRLISTWIN macro code.
The macro will take varying amounts of time to execute based on the path: if you choose c:\, then you must be prepared to wait a while for processing to be completed. If your path does not contain many subdirectories or you specify SUBDIR=N (no recursive processing of subdirectories), then execution time will be short.
If you specify a SAS dataset name for OUT= processing, the following variables will be available for postprocessing:
You can use %DIRLISTWIN more than once in the same job. If you do, then the results captured in the report will be cumulative (a temporary SAS dataset named WORK.REPORT is created which accumulates the results of multiple %DIRLISTWIN invocations). The dataset created by the OUT= option will contain only the results returned for the particular path specified, however.
%DIRLISTWIN may be useful in its filtering capacity if you are searching for a file whose size you know but not its location. The MAXSIZE= and MINSIZE= parameters are designed for this purpose. For example,
%DIRLISTWIN( C:\, MAXSIZE=823000, MINSIZE=822000 ) |
would return all files on the C: hard drive that were at least 822,000 but no more than 823,000 bytes long. In this respect, it is more flexible than the Windows Explorer search options. You can also specify a date/time interval using SAS date and time constants. For example,
%DIRLISTWIN( C:\ , MINDATE='01jan04:00:00'dt , MAXDATE='16mar04:23:59'dt, , MINSIZE=1000000 ) |
would create a listing of all files created on or after New Year's Day for 2004 up to midnight on March 16, 2004 that were at least one million bytes in size.
About the Author:
Ross Bettinger is a SAS Analytical Consultant. He provides support for Enterprise Miner and has been involved with data mining projects for 9 years. He has been a SAS user for 17 years. His professional interests are related to data mining, statistical analysis of data, feature selection and transformation, model building, and algorithm development.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Macro SAS Reference ==> DATA Step |
Date Modified: | 2006-05-23 03:03:06 |
Date Created: | 2004-10-11 14:40:49 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Windows | n/a | n/a |