Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
joey_lutes
Partner - Creator
Partner - Creator

Qlik Sense loop sub script not finding directories or files

Hello,

I'm trying to create a list of filenames and directories using methods described here:
Loop through root directory (including subfolders) and list filenames of *.csv

During Debug, I find that the variables 'SubDirectory' and 'FoundFile' are producing <NULL> which subsequently doesn't create a table.

There are indeed many directories which each contain multiple csv files in the specified location.  I have changed the location of them to a new directory and re-pointed the path as well with the same results.  I've tried with and without '\' in the CALL statement.

No errors are produced.

Any advice?

sub GetCSVFileNames(Root)

    for each FoundFile in filelist( Root & '\*.csv')

        FileList:

            Load

                 filename('$(FoundFile)') as FileName,

                 filedir('$(FoundFile)') as FileDir

                 Autogenerate(1);

    next FoundFile

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

        call GetCSVFileNames(SubDirectory)

    next SubDirectory

end sub

Call GetCSVFileNames('C:\datafiles') ;

gwassenaar

This is related to Loop Through Directory Structure to load data however I'm trying to start by simplifying things.

Thank you!

1 Solution

Accepted Solutions
joey_lutes
Partner - Creator
Partner - Creator
Author

Ok, I think I'm under control mostly.  I changed the following which seemed to help thing:

Instead of a relative path, Qlik Sense doesn't like those much, so I used a lib: and it is much happier.

call DoDir ('lib://datafiles')

Full code below:

sub DoDir (Root)

   for each Ext in 'csv'

      for each FoundFile in filelist (Root&'/*.' &Ext)

         FileList:

         LOAD

            '$(FoundFile)' as FullPathName,

            '$(Dir)' as FileDir,

            right('$(Dir)', 😎 as FileDate,

            Subfield(Subfield('$(File)', '\', SubstringCount('$(File)', '\') + 1), '.', 1) as FileBase,

            FileSize('$(FoundFile)') as  Size           

         autogenerate 1;

      next FoundFile

   next Ext

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

      call DoDir (Dir)

   next Dir

end sub

call DoDir ('lib://datafiles')

View solution in original post

4 Replies
Gysbert_Wassenaar

Could be related to this: Qlik Sense Legacy mode


talk is cheap, supply exceeds demand
joey_lutes
Partner - Creator
Partner - Creator
Author

Nothing like spending 5 days spinning your wheels trying to figure out what you're doing wrong in a simple sub statement only to find it's an .ini setting!!

Thanks!!  I added the StandardReload=0 line and POOF, data.

Ugh.  Thanks again - hopefully headway may occur now!!

joey_lutes
Partner - Creator
Partner - Creator
Author

Ok - maybe I spoke too soon.   It does in fact perform the loop and cycle through the data the appropriate amount of times, however no data is populated in the ensuing table:

Shows 54 rows and 3 fields (correct), however there is no data populated.  I inserted a table in the UI just to double-check and nope, no data.  Any idea why it wouldn't be populating what it's obviously finding?

FileList.JPG

joey_lutes
Partner - Creator
Partner - Creator
Author

Ok, I think I'm under control mostly.  I changed the following which seemed to help thing:

Instead of a relative path, Qlik Sense doesn't like those much, so I used a lib: and it is much happier.

call DoDir ('lib://datafiles')

Full code below:

sub DoDir (Root)

   for each Ext in 'csv'

      for each FoundFile in filelist (Root&'/*.' &Ext)

         FileList:

         LOAD

            '$(FoundFile)' as FullPathName,

            '$(Dir)' as FileDir,

            right('$(Dir)', 😎 as FileDate,

            Subfield(Subfield('$(File)', '\', SubstringCount('$(File)', '\') + 1), '.', 1) as FileBase,

            FileSize('$(FoundFile)') as  Size           

         autogenerate 1;

      next FoundFile

   next Ext

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

      call DoDir (Dir)

   next Dir

end sub

call DoDir ('lib://datafiles')