Code run as batch does not delete files

Hello all,
I have a problem with my class running as batch, it does not delete files (code below). There are two locations in the code that delete the file(System.IO.File::Delete), the first call is OK (file is deleted), but once the file is opened the next code does not delete the file. In logs I get an RPC1702 error, but this is not a problem with directory access because the first delete command works fine.
What is wrong with my code, or what should I do to have working solution ?

This code was copied from msdn.microsoft.com/…/cc967403.aspx and converted to batch format.

private void fileList()
{

TextIo txIoRead,
txIoWrite;
FileIOPermission fioPermission;
container containFromRead;
int xx,
iConLength;
str sTempPath,
sFileName = “aa.csv”,
sOneRecord;
Filename sourcePath = @“C:\my_dir\aa.csv”;
;
// Get the temporary path.
//sTempPath = WINAPI::getTempPath();
info("File is at: " + sTempPath + sFileName);
[sTempPath, sFileName] = fileNameSplit(sourcePath);
sFileName = “aa.csv”;
// Assert permission.
fioPermission = new FileIOPermission
(sTempPath + sFileName ,“RW”);
fioPermission.assert();

// If the test file already exists, delete it.

if (System.IO.File::Exists(sourcePath))
{
System.IO.File::Delete(sTempPath + sFileName);
}

// Open a test file for writing.
// “W” mode overwrites existing content, or creates the file.
txIoWrite = new TextIo( sTempPath + sFileName ,“W”);

// Write records to the file.
txIoWrite.write(“Hello World.”);
txIoWrite.write(“The sky is blue.”);
txIoWrite.write("");
txIoWrite.write("// EOFile");

// Close the test file.
txIoWrite = null;

// Open the same file for reading.
txIoRead = new TextIo(sTempPath + sFileName ,“R”);
// Read the entire file into a container.
containFromRead = txIoRead.read();

// Loop through the container of file records.
while (containFromRead)
{
sOneRecord = “”;
iConLength = conLen(containFromRead);
// Loop through the token in the current record.
for (xx=1; xx <= iConLength; xx++)
{
if (xx > 1) sOneRecord += " ";
sOneRecord += conPeek(containFromRead ,xx);
}
info(sOneRecord);

// Read the next record from the container.
containFromRead = txIoRead.read();
}

// Close the test file.
txIoRead = null;
// Delete the test file.
System.IO.File::Delete(sTempPath + sFileName);

// revertAssert is not really necessary here,
// because the job (or method) is ending.
CodeAccessPermission::revertAssert();
}

Ps. Code run on client works fine ofcourse :).

I have the impression that AX in batch does not release the file. In the last line I put the path to a completely different file not previously opened in the code and this file is deleted!

new code in lasts line.

// Close the test file.
txIoRead = null;
// Delete the test file.
sourcePath = @“C:\my_dir\bb.csv”;
System.IO.File::Delete(sourcePath);

Pls help :slight_smile:

The solution to this problem is to add a method: txIoRead.finalize();

private void fileList()

{

TextIo txIoRead,

txIoWrite;

FileIOPermission fioPermission;

container containFromRead;

int xx,

iConLength;

str sTempPath,

sFileName = “aa.csv”,

sOneRecord;

Filename sourcePath = @“C:\my_dir\aa.csv”;

;

// Get the temporary path.

//sTempPath = WINAPI::getTempPath();

info("File is at: " + sTempPath + sFileName);

[sTempPath, sFileName] = fileNameSplit(sourcePath);

sFileName = “aa.csv”;

// Assert permission.

fioPermission = new FileIOPermission

(sTempPath + sFileName ,“RW”);

fioPermission.assert();

// If the test file already exists, delete it.

if (System.IO.File::Exists(sourcePath))

{

System.IO.File::Delete(sTempPath + sFileName);

}

// Open a test file for writing.

// “W” mode overwrites existing content, or creates the file.

txIoWrite = new TextIo( sTempPath + sFileName ,“W”);

// Write records to the file.

txIoWrite.write(“Hello World.”);

txIoWrite.write(“The sky is blue.”);

txIoWrite.write("");

txIoWrite.write("// EOFile");

// Close the test file.

txIoWrite.finalize();

txIoWrite = null;

// Open the same file for reading.

txIoRead = new TextIo(sTempPath + sFileName ,“R”);

// Read the entire file into a container.

containFromRead = txIoRead.read();

// Loop through the container of file records.

while (containFromRead)

{

sOneRecord = “”;

iConLength = conLen(containFromRead);

// Loop through the token in the current record.

for (xx=1; xx <= iConLength; xx++)

{

if (xx > 1) sOneRecord += " ";

sOneRecord += conPeek(containFromRead ,xx);

}

info(sOneRecord);

// Read the next record from the container.

containFromRead = txIoRead.read();

}

// Close the test file.

txIoRead.finalize();

txIoRead = null;

// Delete the test file.

System.IO.File::Delete(sTempPath + sFileName);

// revertAssert is not really necessary here,

// because the job (or method) is ending.

CodeAccessPermission::revertAssert();

}

It would probably (= not tested) get finalized when getting out of scope. It would happen if you correctly split your code into several methods, instead of putting everything into a single long method, so all variables are in scope until the very end (and the code is also harder to read and maintain).

Thanks for the answer. Because of the NDA contract I do not insert the production code. Please note that the example was taken from the MSDN website. I pointed the source at the beginning.