Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GetFilesInFolder sort options
I'd like some flags for the function GetFilesInFolder to sort the array returned by time modified ascending/descending.

Probably would want same for created and other criteria such as size.

Will add getProp parameter in next QM.

Function GetFilesInFolder2
Select All      Help
function ARRAY(str)&a $parentFolder [$filenamePattern] [flags] [getProp] ;;flags: 0 files, 1 folders, 2 any, 4 include subfolders, 8 only subfolders, 0x10000 regular expression, 0x20000 evaluate relative path.  getProp: 1 size, 2 time modified, 3 time created, 4 time accessed, 0x100 (flag) don't need folder size

;Gets full paths of files that are in a folder.

;a - variable (array) for results.
;parentFolder - full path of the folder. Can optionally end with \.
;filenamePattern - if used and not "", gets only files that match filenamePattern. If not, gets all files.
;;;By default, filenamePattern is interpreted as string containing wildcard characters * an/or ?.
;;;If flag 0x10000 used, filenamePattern is interpreted as regular expression.
;;;Case insensitive.
;flags - see above.
;;;If used flag 0x20000, evaluated is whole relative path followed by parentFolder and \. That is, it includes subfolder names (assuming that flags 4 or 8 used). If this flag is not used, evaluated is only filename.
;;;If used flags 0x10000 and 0x20000, \ characters in filenamePattern must be \\, because filenamePattern is regular expression.
;getProp (QM 2.3.5) - prepend a file property to the path, like "1000 c:\file.txt".
;;;File size is in bytes.
;;;File times are in FILETIME/DateTime format, UTC.
;;;To sort the array by a file property (or just by name, if getProp not used): a.sort(8)
;;;To get the number and path string: see example.

;See also: <FE_Dir>.

;ARRAY(str) a; int i
;GetFilesInFolder a "$desktop$"
;for i 0 a.len
,;out a[i]
,;Dir d.dir(a[i])
,;out d.FileSize

;GetFilesInFolder a "$system$" "*.exe"
;GetFilesInFolder a "$system$" "^.*\.exe$" 0x10000

;;get file time and path, sort by file time, and display all in readable format
;ARRAY(str) a; int i
;GetFilesInFolder2 a "$my qm$" "" 0 2
;for i 0 a.len
,;DateTime t=val(a[i] 1 _i) ;;use DateTime for times. Use long for size.
,;str sPath=a[i]+_i+1
,;out F"{t.ToStr(4)}    {sPath}"


str s.expandpath(parentFolder)
s.dospath(s 1)
if(!s.end("\")) s+"\"
int isfp=!empty(filenamePattern)
int getPropFlags=getProp&~255; getProp&255

Dir d; lpstr sPath ss
foreach(d s FE_Dir flags&0xffff)
,,if(flags&0x20000) ss=sPath+s.len-1; else ss=d.FileName
,,if(flags&0x10000) if(findrx(ss filenamePattern 0 1)<0) continue
,,else if(!matchw(ss filenamePattern 1)) continue
,if getProp
,,long x
,,sel getProp
,,,case 1 if(getPropFlags&0x100 and d.IsFolder) x=0; else x=d.FileSize
,,,case 2 x=d.TimeModified(0 0 0 1)
,,,case 3 x=d.TimeCreated(0 0 0 1)
,,,case 4 x=d.TimeAccessed(0 0 0 1)
,,,case else end ERR_BADARG
,,a[].from(x " " sPath)
,else a[]=sPath

err+ end _error ;;error in rx

Forum Jump:

Users browsing this thread: 1 Guest(s)