Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_dirlist.sas File Reference

Returns all files and subdirectories within a specified parent. More...

Go to the source code of this file.

Detailed Description

When used with getattrs=NO, is not OS specific (uses dopen / dread).

Usage:

%mp_dirlist(path=/some/location, outds=myTable, maxdepth=MAX)

%mp_dirlist(outds=cwdfileprops, getattrs=YES)

%mp_dirlist(fref=MYFREF)

Entries are classified with the dopen function alone. The previous implementation also used fopen - which blocks when the entry is a named pipe (fifo), so a single fifo in a folder was enough to hang the whole session. With getattrs=NO that hazard is gone; with getattrs=YES entries are opened to read their attributes, so a fifo can still block that mode.

Note
Symlinks are followed. A symlink that points at one of its own parents produces an endless path (a/b/link/b/link/...), so a browse service must always pass a finite maxdepth=.
Parameters
[in]path=(sysfunc(pathname(work))) Path for which to return contents. Pass a blank value to list the platform roots instead - the drives that exist on Windows, / on Unix.
[in]fref=(0) Provide a DISK engine fileref as an alternative to PATH
[in]maxdepth=(0) Set to a positive integer to indicate the level of subdirectory scan recursion - eg 3, to go ./3/levels/deep. For unlimited recursion, set to MAX.
[in]maxobs=(0) Maximum number of rows to return. Zero means no limit. Useful when the folder is expected to be huge and the caller only needs a slice of it - no further levels are scanned once the cap is hit.
[in]showparent=(NO) By default, the initial parent directory is not part of the results. Set to YES to include it. For this record only, directory=filepath.
[out]outds=(work.mp_dirlist) The output dataset to create
[out]getattrs=(NO) If getattrs=YES then the doptname / foptname functions are used to scan all properties - any characters that are not valid in a SAS name (v7) are simply stripped, and the table is transposed so theat each property is a column and there is one file per row. An attempt is made to get all properties whether a file or folder, but some files/folders cannot be accessed, and so not all properties can / will be populated.
[in]level=(0) Ignored - retained so that callers of previous versions do not break. Recursion is now done in a loop rather than by re-invoking the macro.
Returns
outds contains the following variables:
  • directory (containing folder)
  • file_or_folder. folder when it can be opened as a directory, file when it is not a directory (or exists but is not readable, which the copy, hash and zip tools treat as a file), and locked when it is listed but cannot be opened at all - a broken link, for instance
  • filepath (path/to/file.name)
  • filename (just the file name)
  • ext (.extension)
  • msg (system message if any issues)
  • level (depth of folder)
  • OS SPECIFIC variables, if getattrs= is used.

SAS Macros

Related Macros

Version
9.2 @sasjslint {"maxLineLength": 128}

Definition in file mp_dirlist.sas.