set exact on local lPosSeen as Boolean Scan && Scans for New_bills up to EOF() * cPsfilepath=Alltrim(Fullpath) && used only once, can be moved to the related instruction, aliased with 'New_bills' * Newpath=Alltrim(Newpath) && looks like you use this nowhere Create Cursor Cursor_AllBills (POS varchar(254),PAGENUM I,posR N(8,3),posC N(8,3)) && Create Temporary Cursor For Cursor_AllBills * Select Cursor_AllBills && useless - Create Cursor automatically selects the alias * Setting a flag saying whether POS has been encountered should be faster than checking recno() - just a thought lPosSeen = .F. Append From (Alltrim(New_bills.Fullpath)) sdf for lPosSeen(@m.lPosSeen) && moved from above * we can apply the same logic to the records before 'POS' create CURSOR Cursor_Header (POS varchar(254)) lPosSeen = .F. Append From (Alltrim(New_bills.Fullpath)) sdf for !lPosSeen(@m.lPosSeen) && the above code replaces these lines * Locate For POS = '%%EndSetup:' && you can avoid Alltrim() with SET EXACT ON * HeaderRecordNumber=Recno() && Finds the Header Record Number for Each PS File * SELECT POS FROM Cursor_AllBills INTO CURSOR Cursor_Header Where Recno()<= HeaderRecordNumber * Delete From Cursor_AllBills Where Recno()<= HeaderRecordNumber Do Split endscan function lPosSeen(lPosSeen) lPosSeen = m.lPosSeen or POS = '%%EndSetup:' && Alltrim() is useless with SET EXACT ON return m.lPosSeen && update to first post