Renaming a file from within Dynamics NAV

I’m trying to rename a file just after I have created it with an XMLPort. But when I do it I’m getting the following error message:

You cannot use the file C:\temp\file_20111026_152844.xml because it is already in use.

The code I’m using is:


FileNamePath := "File Location" + FileName;
IF EXISTS(FileNamePath) THEN
ERASE(FileNamePath);

TmpFileNamePath := "Temporary File Location" + FileName;
IF EXISTS(TmpFileNamePath) THEN
ERASE(TmpFileNamePath);
ExportXMLFile.CREATE(TmpFileNamePath);
RENAME(TmpFileNamePath,FileNamePath);
END ELSE

Is there a way I must release it first or something? Or shouldn’t it release it automatically?

The Navision version is 4.0sp3.

I don’t think it will release until the XMLPort finishes. What’s the point of creating a file in one place, then immediately moving it somewhere else, though? Why not just create it where you want to begin with?

I’m not creating it in the place where it’s supposed to be, because if I do, then the system supposed to import the file into the other system will start reading it, even before it has completed, causing that system to fail. So instead of waiting for them to release a fix to their system, then I rather just do it from within NAV. If it was possible! [:)]

Ok I just see a “minor” mistake in the code example above.

The actual code I’m using is:


```
FileNamePath := "File Location" + FileName;

  IF EXISTS(FileNamePath) THEN
    ERASE(FileNamePath);

    TmpFileNamePath := "Temporary File Location" + FileName;
    IF EXISTS(TmpFileNamePath) THEN
      ERASE(TmpFileNamePath);
    ExportXMLFile.CREATE(TmpFileNamePath);

    ExportXMLFile.CREATEOUTSTREAM(OutXMLStream);
    XMLPort.SETDESTINATION(OutXMLStream);
    XMLPort.EXPORT;

    RENAME(TmpFileNamePath,FileNamePath);

```

And it the problem was solved by adding a line: ExportXMLFile.close;

Just before the rename.