Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Jagsfan82
Partner - Contributor III
Partner - Contributor III

Looping through Subfolders and Files

I was hoping to make a table of all files in the subfolders of a directory, but only if they are in a certain named folder.

As an example I need all excel files that are in a subfolder with a name of 'Templates'.  I can't quite grasp my head around the loops in the typically file list script to know if I could just write some sort of expression string as the extension (See below).

sub DoDir (Root)
   for each Ext in '\Templates\*.xlsx'

      for each File in filelist (Root&'\*' &Ext)

         LOAD 
            '$(File)' as Name, 
            FileSize( '$(File)' ) as Size, 
            FileTime( '$(File)' ) as FileTime
         autogenerate 1;

      next File

   next Ext
   for each Dir in dirlist (Root&'\*' )

      call DoDir (Dir)

   next Dir

end sub

 

In theory I could make a list of all files, but that will be quite a long list and was hoping I could tell it only to look in subfolders called 'Templates'

 

1 Solution

Accepted Solutions
Taoufiq_Zarra

Hi,

Attached is a proposal: the objective is

make a table of all files in the subfolders of a directory, but only if they are in a certain named folder (Example : "Templates")

Sans titre.png

 

sub DoDir (Root)



      for each File in filelist (Root&'\*' &Ext)
       let vmatch = wildmatch('$(File)','*Templates*');
       if '$(vmatch)'>0 then
         LOAD 
            '$(File)' as Name, 
            FileSize( '$(File)' ) as Size, 
            FileTime( '$(File)' ) as FileTime
         autogenerate 1;
	   end if
      next File


   for each Dir in dirlist (Root&'\*' )

      call DoDir (Dir)

   next Dir

end sub

Call DoDir('.\test\') ;

 

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉

View solution in original post

4 Replies
Taoufiq_Zarra

Hi,

Attached is a proposal: the objective is

make a table of all files in the subfolders of a directory, but only if they are in a certain named folder (Example : "Templates")

Sans titre.png

 

sub DoDir (Root)



      for each File in filelist (Root&'\*' &Ext)
       let vmatch = wildmatch('$(File)','*Templates*');
       if '$(vmatch)'>0 then
         LOAD 
            '$(File)' as Name, 
            FileSize( '$(File)' ) as Size, 
            FileTime( '$(File)' ) as FileTime
         autogenerate 1;
	   end if
      next File


   for each Dir in dirlist (Root&'\*' )

      call DoDir (Dir)

   next Dir

end sub

Call DoDir('.\test\') ;

 

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Jagsfan82
Partner - Contributor III
Partner - Contributor III
Author

Thanks for the suggestion.  I definitely think that will work and actually gives me other ideas.  I was hoping the wildcard search could be used in a way to speed up the search though.  I'll try to give it a test to make sure since I want the Templates to be in the folder directory, but that shouldn't matter based on your solution.  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Taoufiq_Zarra

Ok

waiting for your feedback !

Regards,
Taoufiq ZARRA

"Please LIKE posts and "Accept as Solution" if the provided solution is helpful "

(you can mark up to 3 "solutions") 😉
Brett_Bleess
Former Employee
Former Employee

Have a look at the following Design Blog post as there is a pretty related example there:

https://community.qlik.com/t5/Qlik-Design-Blog/Loops-in-the-Script/ba-p/1473543

If you wish to further search this area, use the following base URL:

https://community.qlik.com/t5/Qlik-Design-Blog/bg-p/qlik-design-blog

Be sure to return once you get something working and either use the Accept as Solution button on an existing post or if you did something different, write that up and then use the button on it after you post it, this way the other Community Members know what actually worked and if you do one of the other posts, that gives credit to them...

Regards,
Brett

To help users find verified answers, please do not forget to use the "Accept as Solution" button on any post(s) that helped you resolve your problem or question.
I now work a compressed schedule, Tuesday, Wednesday and Thursday, so those will be the days I will reply to any follow-up posts.