Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
FLASH SALE: Save $500! Use code FLASH2026 at checkout until Feb 14th at 11:59PM ET. Register Now!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Loading latest file in folder

Hi,

I have Oracle reports that are saved once a day on a local FTP server. I need to load the latest report based on the date the report was printed. I have written the following load statement;

sub readWIP(root)
for each File in filelist(root & '\*.txt')
tmpWIP:
LOAD @1:45 as wo_key,
if(@1:5='Date:',@13:21,peek('tmpReportDate')) as tmpReportDate,
@1:45 as tmpWO,
@242:260 as tmpStatus
FROM [$(File)] (fix, codepage is 1252, header is 1 lines);
next File
end sub

call readWIP('\\oer1euappw01\ftproot\reports\Discrete_Job_Value');

WIP:
Load wo_key,
tmpReportDate,
tmpWO,
tmpStatus
RESIDENT tmpWIP
WHERE tmpStatus = 'Released' and tmpReportDate = LATEST;

drop table tmpWIP;

---

Any ideas on how to write the code in bold or am I going about this entirely wrong?

Best Regards,
Jonas

1 Solution

Accepted Solutions
prieper
Master II
Master II

You should include the filename in the tmpWIP, then create a table with the filename and the latest date, take this into a variable and load this file.
Script might look:

sub readWIP(root)
for each File in filelist(root & '\*.txt')
tmpWIP:
LOAD @1:45 as wo_key,
if(@1:5='Date:',@13:21,peek('tmpReportDate')) as tmpReportDate,
@1:45 as tmpWO, @242:260 as tmpStatus,
FILENAME() AS FileName,
FILETIME() AS FileTime
FROM [$(File)] (fix, codepage is 1252, header is 1 lines);
next File
end sub

call readWIP('\\oer1euappw01\ftproot\reports\Discrete_Job_Value');
xLastFile:
LOAD
FIRSTSORTEDVALUE(FileName, -FileTime) AS LastFile
RESIDENT
tmpWIP
GROUP BY
FileName;
LET sLastFile = PEEK('LastFile, 0, 'xLastFile');
LOAD * FROM $(sLastFile);


Alternatively you may also sort the tmpWIP by the date in descending order and pick with the PEEK-command the first filename in the first record

HTH
Peter

View solution in original post

1 Reply
prieper
Master II
Master II

You should include the filename in the tmpWIP, then create a table with the filename and the latest date, take this into a variable and load this file.
Script might look:

sub readWIP(root)
for each File in filelist(root & '\*.txt')
tmpWIP:
LOAD @1:45 as wo_key,
if(@1:5='Date:',@13:21,peek('tmpReportDate')) as tmpReportDate,
@1:45 as tmpWO, @242:260 as tmpStatus,
FILENAME() AS FileName,
FILETIME() AS FileTime
FROM [$(File)] (fix, codepage is 1252, header is 1 lines);
next File
end sub

call readWIP('\\oer1euappw01\ftproot\reports\Discrete_Job_Value');
xLastFile:
LOAD
FIRSTSORTEDVALUE(FileName, -FileTime) AS LastFile
RESIDENT
tmpWIP
GROUP BY
FileName;
LET sLastFile = PEEK('LastFile, 0, 'xLastFile');
LOAD * FROM $(sLastFile);


Alternatively you may also sort the tmpWIP by the date in descending order and pick with the PEEK-command the first filename in the first record

HTH
Peter