I use a Dataport to Import from a txtfile to a table.
In "Dataport - OnPostDataport()" I remove the file I have Imported, like this :IF EXISTS(CurrFile.NAME) THEN BEGIN FileName := CurrFile.NAME; CurrFile.CLOSE; ERASE(FileName);END;
FileName is a Global Variable of Text - 255
I only wish to remove the file if the Import have been successfull, that is all data is imported to the table.Is there a property on the Dataport to see if the Import was successfull ?
Ussually you should get error message if import is not successful. After error you will not get to OnPostDataport, so file will not be deleted.
Before calling ERASE I would do like this (in OnPostDataport):
IF NOT RENAME(CurrentFilePathName, NewFilePathName) THEN ERROR('SomeErrorMessage');COMMIT; //if this one fails, the file only have been moved, not erasedERASE(NewLocation); //syntax may be "IF NOT ERASE(..."
Thankyou for your time to answer my question.
Unfortunately the code in OnPostDataport is performed even if I recieve an error message.So the file gets deleted even if an error occur.
anfinnur: Before calling ERASE I would do like this (in OnPostDataport): IF NOT RENAME(CurrentFilePathName, NewFilePathName) THEN ERROR('SomeErrorMessage');COMMIT; //if this one fails, the file only have been moved, not erasedERASE(NewLocation); //syntax may be "IF NOT ERASE(..."
Thankyou for spending time answering my question.
Yes, this code is much better.I agree it would be wiser to rename the file and not delete it, so recovery of the file is possible.
Still I would like to be able to abort the file rename operation if the transaction have failed.Is there a way to detect weather the transaction was successfull ?
I found a solution :-)If I perform a COMMIT before doing the ERASE, then the Error appear before I do the file operation.
The Code in Dataport - OnPostDataport() Trigger will the look like this:IF EXISTS(CurrFile.NAME) THEN BEGIN FileName := CurrFile.NAME; CurrFile.CLOSE; COMMIT(); //Any Error appear here, before File operation is performed IF NOT ERASE(FileName + '.bak') THEN BEGIN //Unable to delete .bak file is not critical END; IF NOT RENAME(FileName, FileName + '.bak') THEN ERROR(Unable to rename : ' + FileName);END;