-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlistfiles_example.lua
More file actions
76 lines (70 loc) · 2.54 KB
/
Copy pathlistfiles_example.lua
File metadata and controls
76 lines (70 loc) · 2.54 KB
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
-- Example usage of the listfiles() function
-- This function securely lists files in directories within the plugin path
-- All paths are relative to the plugin directory (pluginPath)
-- Example 1: List all files in the plugin root directory
print("=== Files in plugin root directory ===")
local files = listfiles("") -- Empty string or "." for root
if files then
for i = 1, #files do
print("File " .. i .. ": " .. files[i])
end
else
print("No files found or directory doesn't exist")
end
-- Example 2: List files in a subdirectory
print("\n=== Files in 'scripts' subdirectory ===")
local scriptFiles = listfiles("scripts") -- Relative to pluginPath
if scriptFiles then
for i = 1, #scriptFiles do
print("Script file " .. i .. ": " .. scriptFiles[i])
end
else
print("No script files found or 'scripts' directory doesn't exist")
end
-- Example 3: Check if specific files exist
print("\n=== Checking for specific files ===")
local allFiles = listfiles(".")
if allFiles then
local foundFiles = {}
for i = 1, #allFiles do
local filename = allFiles[i]
-- Check for .lua files
if string.match(filename, "%.lua$") then
table.insert(foundFiles, filename)
end
end
if #foundFiles > 0 then
print("Found " .. #foundFiles .. " Lua files:")
for i = 1, #foundFiles do
print(" " .. foundFiles[i])
end
else
print("No Lua files found")
end
end
-- Example 4: Process files with readfile()
print("\n=== Processing Lua files ===")
local luaFiles = listfiles("") -- List files in plugin root
if luaFiles then
for i = 1, #luaFiles do
local filename = luaFiles[i]
if string.match(filename, "%.lua$") then
print("Processing: " .. filename)
local content = readfile(filename) -- filename is already relative to pluginPath
if content then
local lineCount = select(2, string.gsub(content, '\n', '\n')) + 1
print(" Lines: " .. lineCount)
else
print(" Could not read file")
end
end
end
end
-- Updated usage notes:
-- - All paths are now relative to pluginPath automatically
-- - Use "" or "." for the plugin root directory
-- - Use "subfolder" to access subdirectories
-- - Both functions automatically prepend pluginPath for security
-- - Trying to use "../" will be blocked by security checks
-- - Example: readfile("config.txt") reads pluginPath/config.txt
-- - Example: listfiles("data") lists files in pluginPath/data/