Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
JackMcDonald
Contributor
Contributor

Looping through folders and subfolders to find a list of file_names

I have been having an issue when looping through folders in Qlik, so far this is my script: 

LIB CONNECT TO 'FOLDERS';
 
FOLDERS:
LOAD 
Name as Folder_Name,
ServerRelativeUrl;
SELECT 
Name,
ServerRelativeUrl
FROM ListFolders
WITH PROPERTIES (
subSite='',
folder='',
maxResults=''
);
 
FOR i = 0 TO NOOFROWS('FOLDERS') - 1;
 
LET vFolder = PEEK('Folder_Name', i, 'FOLDERS');  
    
    
FOLDERS_FILES:
LOAD 
'$(vFolder)' as Folder_Name,
Name as File_Name,
    filepath/$(vFolder)'&Name as File_URL;
SELECT Name
FROM ListFiles
WITH PROPERTIES (
subSite='',
folder='filepath/$(vFolder)',
maxResults=''
);
 
Next i;
 
I effectively want to create a loop or loops which go through the main folder and then each subfolder within it to find file_names and extract them into one field. 
Labels (3)
1 Reply
Øystein_Kolsrud
Employee
Employee

I do pretty much exactly that in the app I use for log analysis for Qlik Sense. Have a look in the load script of the app named "LogAnalysis_V8.qvf" that you can download from here:

https://community.qlik.com/t5/Official-Support-Articles/LogAnalysis-App-The-Qlik-Sense-app-for-troub...

The key section is the one named "File loading" where the following two functions are defined:

Sub GetFiles(dir)
for each Ext in 'log', 'txt'
  for each vFile in FileList('$(dir)/*.$(Ext)')
FilesRaw:
Concatenate LOAD
'$(vFile)' AS FileName,
SubField(Left('$(vFile)', Len('$(vFile)') - Len('Z.$(Ext)')),'_',-1) as FileTimeRaw
AUTOGENERATE 1;
next vFile;
next Ext;
end sub;

sub GetFilesRecursive(dir)
trace GetFilesRecursive: $(dir);
call GetFiles('$(dir)');
for each vDir in DirList('$(dir)/*')
call GetFilesRecursive('$(vDir)');
next vDir;
End Sub;

 The function "GetFilesRecursive" traverses the whole folder structure, while the function "GetFiles" loads all file names in a specific folder into a table called "FilesRaw".