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

Load latest file - filetype can be either .txt or .csv

Hi all,

I have a script which will load the latest file in a stated folder.   Currently it is the latest .CSV but sometimes a .txt file is saved.....

I need help on what changes is required in my load script, to now take into account the .txt files.  So basically I require to load the latest file, no matter what file extension it has.

This is my current code:


If FileSize('F:\file_location\*.csv')>0 then
for each File in filelist ('F:\file_location\*.csv')
Folder:
Load '$(File)' as Name,
FileTime( '$(File)' ) as FileTime
autogenerate 1;
next File

LatestFile:
first 1
Load
Name,
FileTime,
1
as dummy
Resident Folder
Order By FileTime DESC;
drop table Folder;

LOAD [@1:n] as 'Image Data'
FROM
'$(File)'
(
fix, utf8, no labels);
ELSE 

Temp:
Load MsgBox ('No files found in the folder!','Status','OKCANCEL','ICONHAND') AutoGenerate 1;
DROP Table Temp;
ENDIF

7 Replies
oknotsen
Master III
Master III

Replace the extention (.csv or .txt) by a star. That should do the trick.

I suggest you avoid putting other files in that folder else it might get messy.

May you live in interesting times!
Not applicable
Author

‌that was the 1st thing I tried but it didn't work

oknotsen
Master III
Master III

Did you replace it in both locations?

Let me put it this way:

On a frequent basis I have been loading multiple files from a folder that matches "emp*.*" and it worked every time. Maybe "*.*" is not allowed, but that somehow sounds very strange.

May you live in interesting times!
Not applicable
Author

‌I did replace in both locations.

I'm starting to think the script to load the most recent file is not working - can you / anyone spot a mistake In the code?

thanks

oknotsen
Master III
Master III

So are you saying the initial script does not work in the current situation under the current requirements?

I was replying under the assumption that the initial script worked and you only were looking for a way to change it to meet changed requirements.

May you live in interesting times!
Not applicable
Author

I got it to work by moving all the exiting files to an archive folder.  So the folder will always contain only the latest file.

Thanks for your help

oknotsen
Master III
Master III

Happy you figured it out yourself .

May you live in interesting times!