Deleting Files from PeopleCode

We have an interface file that we want to delete for security reasons once we have processed the file.  But, deleting it from the Application  Engine was not as straight-forward as I would have thought.

This does not work:

RemoveDirectory("c:\temp\mytextfile.txt", %FilePath_Absolute);

So, Java to the rescue — this does:

Local JavaObject &javaFile;
&javaFile = CreateJavaObject("java.io.File", "c:\temp\mytextfile.txt");
&javaFile.delete();

Update (8/20/2014):

Back when I wrote this post, I missed seeing the .Delete() method of the File API object.  That would be the better way to make this work.  Here’s an example:

Local File &file;
&file = GetFile("c:\temp\mytextfile.txt", "W", "A", %FilePath_Absolute);
&file.Delete();

(Thanks Tim and Andrew for pointing this out.)

Resources

8 thoughts on “Deleting Files from PeopleCode

    1. Larry,

      I am so sorry that I have taken so long to respond. I don’t know if you figured this out, but I could make a guess.

      I get Java errors when I am running AE programs directly from App Designer. When I run them from online (via the batch server), they seem to run with no problem.

      Is it possible that you are running it from App Designer? I hope that helps.

  1. Excellent solution. It worked great for me. I used EXEC to delete a file and I was not able to. Not sure why it was not working but Java option worked well..

    Thanks a lot digitaleagle.

  2. Thanks for the tip. I’ve used the following and it work well to.

    &tmpfile = GetFile(“c:tempfile.txt”, “W”, “A”, %FilePath_Absolute);
    &tmpfile.Delete();

    Thank
    Tim

  3. RemoveDirectory(“c:tempmytextfile.txt”, %FilePath_Absolute);

    – would definitely not work because it looks like you want to delete a directory whereas you set the path up to a filepath… RemoveDirectory only deletes the Directory assuming no files exists in it.

Leave a Comment

Your email address will not be published. Required fields are marked *