Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Loop read in using sequential filenames

Team,

I am attempting to create an automatic load which will count the number of files in a folder and then read in each of those files using a FOR/NEXT loop, and sequential filenames (eg. MyFile_1, MyFile_2, etc...).

There is one section of the code that does appear to work. The count of the filenames returns the correct value for "n", however the line of code struck out below in which I attempt to set a macro equal to the value of the field "n" will not work (this is needed so that I can end my loop at $(N) which would equal the final "MyFile_x").

If anyone can provide any insight on how I can accomplish that goal it would be greatly appreciated.

LET N=0;

filenames:

load

Filename() as filename

FROM

[\\PATH\datafiles\*.csv]

;

load count(DISTINCT(filename)) as n

resident filenames;

load n,

$(N)=sum(n)

resident [filenames-1];

FOR a = 2 to $(N);

Let FileName = [\\PATH\datafiles\MyFile_$(a).csv]'; // FileName is a variable representing the csv file name aka MyFile_1

fact_Pre:

Load

*

FROM

$(FileName)

(txt, codepage is 1252, embedded labels, delimiter is ',', msq);

Next



Thank you,

Michael

1 Solution

Accepted Solutions
Not applicable
Author

Hello Michael,

as far as I understand you I would try a "for each in filelist" instead of a for-loop (similar to the code below):

for each File in filelist ([\\PATH\datafiles\*.csv]) // or [\\PATH\datafiles\MyFile_*.csv]
LOAD
*
From
$(File) (txt, codepage is 1252, no labels, delimiter is '\t', msq);



Regards, Roland

View solution in original post

3 Replies
Not applicable
Author

Hello Michael,

as far as I understand you I would try a "for each in filelist" instead of a for-loop (similar to the code below):

for each File in filelist ([\\PATH\datafiles\*.csv]) // or [\\PATH\datafiles\MyFile_*.csv]
LOAD
*
From
$(File) (txt, codepage is 1252, no labels, delimiter is '\t', msq);



Regards, Roland

Anonymous
Not applicable
Author

Michael,
I'm sure that the simpler way will work, no need for the FileName varable:
LOAD
...
FROM \\PATH\datafiles\MyFile_$(a).csv ...

Anonymous
Not applicable
Author

Yes, even simpler from Ronald