Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
surendra_masupa
Contributor III
Contributor III

Loop through subfolders and generate a output file for each

Hi folks,

I've a small requirement to loop through the files in a sub folder and generate a output file for each folder.

Folder.PNG

As shown above, I need to load all the files under Australia folder , generate a concatenated table and store it into a csv. And it should loop through India, UK, USA folders......

Pls can you suggest a way to achieve this?

Many Thanks in Advance!

16 Replies
surendra_masupa
Contributor III
Contributor III
Author

Multiple sheets, but will be reading only one.

Clever_Anjos
Employee
Employee

If possible, please enable log file (Document Properties / General / Generate logfile) and post it here too

surendra_masupa
Contributor III
Contributor III
Author

Hi Clever,

It works. But it throws an error for the folders which don't contain any file. Is there a way we can handler this?

Many thanks for your help.

Clever_Anjos
Employee
Employee

It´s pretty simple

  1. for Each dir in DirList('yourpathgoeshere') 
  2.   for Each file in FileList('$(dir)\*.csv') // or .txt
  3.    if filesize('$(file)') > 0 then
  4.       tmp: 
  5.       LOAD * 
  6.       FROM 
  7.       [$(file)](txt, codepage is 1252, no labels, delimiter is ',', msq);
  8.    end if
  9.   next; 
  10.   store tmp into [$(dir).csv](txt); 
  11.   drop table tmp; 
  12. next
surendra_masupa
Contributor III
Contributor III
Author

Hi Clever

Sorry, It didn't work. It's still unable to store the tmp table for the folder which don't have any files.

I tried to set ErrorMode=0 and i works, but don't think this is the best way to achieve this.

Thank you!

Clever_Anjos
Employee
Employee

Would you mind posting the log file here?

Let me check

paulo_pinto
Contributor
Contributor

Had some issues trying to run this solution because line 1.

It did not seem clear to me. I think it is a bit better to understand

1.for Each dir in DirList('yourpathgoeshere\*')