menzi的函数
- ; == Function MeGetFolders
- ; Scans from current folder (recursive) for folders (and files).
- ; Arguments [Type]:
- ; Fol = Root folder [STR]
- ; Pat = File name pattern
- ; - Not False: eg. *.*, *.dwg [STR]
- ; - False: Folders only [BOOLEAN]
- ; Rec = Recursive flag [BOOLEAN]
- ; - True: scan folders recursive
- ; - False: scan first folder level
- ; Return [Type]:
- ; > If pattern argument False:
- ; '("Pth1" "Pth2"...) [LIST]
- ; > If pattern argument not False:
- ; '(("Pth1" '("Fil1" "Fil2"...)) ("Pth2" '(...))...) [LIST]
- ; Notes:
- ; - If no files found in a folder, the file list returns a '("").
- ; - Deep folder structure slow down the function.
- ;
- (defun MeGetFolders (Fol Pat Rec / FolLst TmpFol)
- (setq TmpFol (if (wcmatch Fol "*\") (substr Fol 1 (1- (strlen Fol))) Fol)
- FolLst (cons TmpFol (apply 'append (MeGetFoldersRec TmpFol Rec)))
- )
- (if Pat
- (mapcar
- '(lambda (l)
- (cons l (cond ((vl-directory-files l Pat 1)) ('(""))))
- ) FolLst
- )
- FolLst
- )
- )
- ;
- ; == Function MeGetFoldersRec
- ; Recursive function for MeGetFolders.
- ; Arguments [Type]:
- ; Fol = Folder [STR]
- ; Rec = Recursive flag [BOOLEAN]
- ; - True: scan folders recursive
- ; - False: scan first folder level
- ; Return [Type]:
- ; > Folder list '((Pth1) (Pth2)) [LIST]
- ; Notes:
- ; Return value contain nil atoms.
- ;
- (defun MeGetFoldersRec (Fol Rec / TmpFol)
- (mapcar
- '(lambda (l)
- (if (not (wcmatch l "`.*"))
- (cons
- (setq TmpFol (strcat Fol "\" l))
- (if Rec (apply 'append (MeGetFoldersRec TmpFol Rec)))
- )
- )
- ) (vl-directory-files Fol nil -1)
- )
- )
用法
(Megetfolders "c:\\vtcl" "*.*" True)
三个参数
第一个,路径
第二个,后缀名
第三个,是否递归得到子目录内所有文件
刚才测试了一下,大部分没有问题,有时候有些怪,请仔细测试 |