I used a dataport to export some data and another one to import the generated file.
I need to rename the file after importing the data.
I tried the following code in the OnPostDataport trigger:
FILE.RENAME(C:\Users\aaa\bbb\ddd.txt ,C:\Users\aaa\bbb\yyy.txt );
I got the following error:
You cannot use the file C:\Users\aaa\bbb\ddd.txt because it is already in use.
What shall I do?
Thanks in advance
You’ll need to create a separate object (form, page, codeunit) that 1) defines or establishes the import file name, 2) passes the import file name to the dataport variable and then runs it, and then 3) calls a function like the one you’ve shown to rename the file.
The problem is that from the dataport doesn’t release the import file until after the dataport has closed.
You didn’t close the file before Rename I guess.
While there is a CURRFILE variable inherent in the dataport object, and I’m certainly no expert on its use, I did play around with it to see whether it could be used to allow you to close the export file and run your rename code all within the confines of the dataport. I wasn’t able to make that work, and I did give it a good solid try. My initial conclusion would be that, although you do have a method available to you for touching the file that the dataport is using for import/export, the method isn’t sufficient to allow you to completely close the pointers to the file such that you can rename it while still in the dataport. I’m not declaring it to be impossible, I’m only saying that I wasn’t able to pull it off using the tools at hand.
I hope you’re able to work something out.
Basically this needs to happen:
CurrFile.CLOSE; <<Close the file
SavedFileName := FORMAT(WORKDATE,0,’’+’-’+’<Month Text,3>’+’-’+’<Day,2>’+’-’+DELCHR((FORMAT(TIME)),’=’,’:’))+’.txt’;
FILE.COPY(CurrDataport.FILENAME,FilePath+SavedFileName); <<Copt the file to whatever destination
ERASE(CurrDataport.FILENAME); <<delete the orig file (if you want)