Finding text files in folder and processing them

Hi All,

Firstly I have to admit that I have not found the key to getting started with my problem even after much searching.

The requirement overview:

  • A web app sends files x01.txt, x02.txt etc to *c:\temp* on the NAV server.
  • The folder may contain many files (x01.txt, x02.txt etc)
  • Each file is Fixed Length text with many lines, (to create an invoice with new or existing customer, invoice header, lines and document comments).

Objective: Read each txt file, parse fields within each line, update NAV table ‘WebImport’ with each line in the file.

I need to do the following:

  1. Test if files exist in **c:\temp**. How? syntax for using a File filter?, The File table or Var using the File record does not have any obvious options for this.
  2. I have created a form for the File table and applied Form filters which show the files but I cannot see how to do that in the codeunit, can I run the form (NAS) and trigger the codeunit?
  3. Import files into temp File table Global var ‘newFiles’ ‘Record’ ‘File’ Temp
  4. Then process the text string as normal.

I have not worked with Files using a NAS service before and although the job is a simple one for a user and a brain running a Dataport, the method of getting the Files into NAV this way is not something I have done before so a somewhat desperate need for your help to get started.

I am sure I can parse the data into an array once I can read it.

Example files:

X01.txt (file name)

Rec No. | Type | date | G/L Acc | Unit Price | etc (field labels not included in file)

ABC0001|invoice|240212| 4234 | 1234.56 | etc

ABC0001|invoice|240212| 4235 | 224.56 | etc


ABC0002|invoice|240212| 4234 | 2347.56 | etc

ABC0002|invoice|240212| 4235 | 1224.56 | etc

(where | shows field separator in place of TAB or fixed length)

Thanks for any hints,


Hi Colin,

You can use the file (variable) type of record to get files in a certain path.

then filter that record for t.txt to get the files you need.

The contents could be processed with a dataport.


This can help you

Sort of helpful but tried the code suggested and get errors.

reply added to that thread.


I am using a variable type record sub type File.

I use that to find what text files are in the folder.

That works OK. (see below). The problem then is how to read the data in each file into a temp table and then process each line.



lvfile.setfilter(name, ‘%1’ , ‘test*.txt’);

if lvfile.findset then begin

Use LVFile1 instead of LVFile

LvFile1 is of type FIle not Record Type File…

I have tried both, neither work.[:(]

And the error message is?

Sorry, I have tested it and you are correct.

You are correct, these work.
LVFile1.OPEN(lvFile.path +;

next question is how do I read the data within lvFile1 into a table?

May be the code in this post can guide you

I now have the answers to all the questions I asked.

DataItem File

Global Variables:

FileName Text 100

vFile File

vFieldT Text 100 (array)

VTextLine Text 1000

- OnPreDataItem()


SETRANGE(“Is a file”,TRUE);





- OnAfterGetRecord()





FileName := Path + Name;






vFieldT[1] := COPYSTR(VTextLine,1,20); //record set No.

vFieldT[2] := COPYSTR(VTextLine,21,20); //Record type

vFieldT[3] := COPYSTR(VTextLine,41,20); //Cust. No.


UNTIL vFile.POS = vFile.LEN;

After the above, insert vField[n] to target table.

Did you check the link I gave above…It has example code how to insert into Sales Header and Line…follow the same…

please dont use FINDFIRST directly…

If there is no file in above filter then it will show error message…


or IF FINDSET THEN REPEAT for multiple files…