-
Notifications
You must be signed in to change notification settings - Fork 71
/
DirContents.ahk
102 lines (93 loc) · 3.9 KB
/
DirContents.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
;by LogicDaemon <www.logicdaemon.ru>
;This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License <http://creativecommons.org/licenses/by-sa/4.0/>.
DirContents(dir, mask := "*.*", attribFilter := 0) { ; attribFilter = 0x10 to only return directories; -0x10 to return everything but directories
local ; Force-local mode
; for non-recursive listing, use "" for mask
; beware! byref params are empty in recursive calls
VarSetCapacity(vFINDDATA, A_IsUnicode ? 592 : 520, 0)
;sizeof(WIN32_FIND_DATA) = 592 (Unicode) / 520 (ASCII)
;typedef struct _WIN32_FIND_DATA {
; DWORD dwFileAttributes; // 4
; FILETIME ftCreationTime; // 8
; FILETIME ftLastAccessTime; // 8
; FILETIME ftLastWriteTime; // 8
; DWORD nFileSizeHigh; // 4
; DWORD nFileSizeLow; // 4
; DWORD dwReserved0; // 4
; DWORD dwReserved1; // 4
; TCHAR cFileName[MAX_PATH]; // MAX_PATH*(A_IsUnicode+1)
; TCHAR cAlternateFileName[14]; // 14*(A_IsUnicode+1)
;} WIN32_FIND_DATA, *PWIN32_FIND_DATA, *LPWIN32_FIND_DATA;
;MAX_PATH=260
;#ifdef _UNICODE
;typedef wchar_t TCHAR;
;#else
;typedef char TCHAR;
;#endif
;https://habrahabr.ru/post/164193/
;Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
;typedef struct _FILETIME {
; DWORD dwLowDateTime;
; DWORD dwHighDateTime;
;} FILETIME, *PFILETIME;
;dwFileAttributes:
; https://msdn.microsoft.com/ru-ru/library/windows/desktop/gg258117.aspx
;attReadOnly := vAttrib & 0x1
;attHidden := vAttrib & 0x2
;attSystem := vAttrib & 0x4
;attDirectory := vAttrib & 0x10
;attArchive := vAttrib & 0x20
;attNormal := vAttrib & 0x80
;attTemporary := vAttrib & 0x100
;attSparse := vAttrib & 0x200
;attReparsePoint := vAttrib & 0x400 ; A file or directory that has an associated reparse point, or a file that is a symbolic link
;attCompressed := vAttrib & 0x800
;attUnindexed := vAttrib & 0x2000
;attEncrypted := vAttrib & 0x4000
If (!(SubStr(dir, 1, 2) == "\\")) {
If ((SubStr(dir, 1, 1) == "\")) {
;SplitPath, InputVar [, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive]
SplitPath A_WorkingDir, , , , , OutDrive
dir := "\\?\" OutDrive ":" dir
} Else If (!(SubStr(dir, 2, 1) == ":"))
dir := "\\?\" A_WorkingDir "\" dir
Else
dir := "\\?\" dir
}
If (mask && SubStr(dir, 0) != "\")
dir .= "\"
hFile := DllCall("Kernel32.dll\FindFirstFile", "str", dir . mask, "ptr", &vFINDDATA)
if (hFile = -1) {
If (!mask) ; if recursing, it's fine when one of dirs does not contain files
return ; Exception(A_LastError, "Kernel32.dll\FindFirstFile", dir . mask)
} Else {
out := []
Loop
{
fname := StrGet(&vFINDDATA+44)
if fname not in .,..
{
vAttrib := NumGet(&vFINDDATA)
If (attribFilter == 0 || (attribFilter > 0 && vAttrib & attribFilter) || (attribFilter < 0 && !(vAttrib & -attribFilter)))
out[fname] := {vAttrib: vAttrib}
}
} Until !DllCall("Kernel32.dll\FindNextFile", "ptr", hFile, "ptr", &vFINDDATA)
DllCall("Kernel32.dll\FindClose", "ptr", hFile)
}
If (mask) {
If (!IsObject(out))
out := [], outEmpty := 1
For subdirName in DirContents(dir "*.*" , "", 0x10) {
contents := DirContents(dir . subdirName "\", mask, attribFilter)
If (IsObject(contents)) {
If (!out.HasKey(subdirName))
out[subdirName] := {}
out[subdirName].contents := contents
outEmpty := 0
}
}
If (outEmpty)
out := ""
}
return out
}