function getfilesindirectory(directory, files) files = files or {} local idx = 0 local…
Private research annotation
Sign in to add a private research label or note.
- User
function get_files_in_directory(directory, files) files = files or {} local idx = 0 local file = reaper.EnumerateFiles(directory, idx)
codewhile file do table.insert(files, directory .. "\\" .. file) idx = idx + 1 file = reaper.EnumerateFiles(directory, idx) end idx = 0 local subdirectory = reaper.EnumerateSubdirectories(directory, idx) while subdirectory do get_files_in_directory(directory .. "\\" .. subdirectory, files) idx = idx + 1 subdirectory = reaper.EnumerateSubdirectories(directory, idx) end return filesend
will this get every file of every sub directory (and sub sub directory and so on)? shortest explanation please
- Assistant
Yes, the function
get_files_in_directorywill recursively traverse through all subdirectories and collect every file from the specified directory and its subdirectories. The recursion is achieved by calling the function itself with subdirectory paths. - User
but i want every subdirectory of subdirectories of subdirectories (and so on)
- Assistant
Yes, the function you provided will indeed collect every file from the specified directory and from all its nested subdirectories (including subdirectories of subdirectories, and so on). The recursion ensures that every subdirectory level is visited and processed.
Here's a step-by-step explanation of how it works:
- Initialize
files: Iffilesis not provided, an empty table is initialized. - Enumerate Files: It starts by enumerating files in the current directory using
reaper.EnumerateFiles. - Collect Files: Each file found is added to the
filestable with its full path. - Enumerate Subdirectories: It then enumerates subdirectories in the current directory using
reaper.EnumerateSubdirectories. - : For each subdirectory found, the function calls itself recursively, which processes the subdirectory in the same way (collect files, enumerate subdirectories, and recurse).
- Initialize
- User
- Assistant
The issue lies with the
get_files_in_directoryfunction in your script. Specifically, the recursive call toget_files_in_directorydoes not append found files to thefilestable because thefilesparameter is not passed properly during recursion.Here's a revised version of the
get_files_in_directoryfunction that correctly handles nested subdirectories:lua - User
what was i doing wrong?
- Assistant