dear sir,
one more
Get All FilesFunction Syntax | (LM:GetAllFiles <Dir> <Subs> [Filetype]) | Current Version | 1.0 |
Arguments | Symbol | Type | Description | Dir | String | Directory to search | Subs | Boolean | If True, subdirectories of 'Dir' are included | Filetype | String | [Optional] Filter for filetype (DOS pattern e.g. "*.dwg") | Returns | List of filenames, else nil if none are found |
Program DescriptionRetrieves a list of all filenames or those of a specific type residing in the target directory (and, optionally, subdirectories of such directory). [pcode=lisp,true];;--------------------=={ Get All Files }==-------------------;;
;; ;;
;; Retrieves all files or those of a specified filetype that ;;
;; reside in a directory (and, optionally, subdirectories) ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;; Arguments: ;;
;; Dir - Directory to search ;;
;; Subs - Boolean, if T, subdirectories are included ;;
;; Filetype - (optional) Filter for filetype (DOS pattern) ;;
;;------------------------------------------------------------;;
;; Returns: List of filenames, else nil if none are found ;;
;;------------------------------------------------------------;;
(defun LM:GetAllFiles ( Dir Subs Filetype / _GetSubFolders )
(defun _GetSubFolders ( folder )
(apply 'append
(mapcar
(function
(lambda ( f )
(cons (setq f (strcat folder "\\" f)) (_GetSubFolders f))
)
)
(vl-remove "." (vl-remove ".." (vl-directory-files folder nil -1)))
)
)
)
(apply 'append
(mapcar
(function
(lambda ( Filepath )
(mapcar
(function
(lambda ( Filename ) (strcat Filepath "\\" Filename))
)
(vl-directory-files Filepath Filetype 1)
)
)
)
(cons Dir (if subs (_GetSubFolders Dir)))
)
)
)[/pcode]
Example Calling Function
This example will list all DWG files in the working directory. [pcode=lisp,true](mapcar 'print (LM:GetAllFiles (vl-string-right-trim "\\" (getvar 'DWGPREFIX)) nil "*.dwg"))[/pcode]
Compact Version
This is a concise version of the above and will include subdirectories by default. [pcode=lisp,true](defun LM:GetAllFiles ( dir typ )
(append (mapcar '(lambda ( x ) (strcat dir "\\" x)) (vl-directory-files dir typ 1))
(apply 'append
(mapcar '(lambda ( x ) (LM:GetAllFiles (strcat dir "\\" x) typ))
(vl-remove-if '(lambda ( x ) (wcmatch x "`.,`.`.")) (vl-directory-files dir "*" -1))
)
)
)
)[/pcode]
|