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


Share

8 Comments

  1. Andrew says:

    Thanks, very nice. Something different, instead of using the delete method of the file object.

  2. Larry says:

    I tried the code, that you mentioned but somehow I am getting Java class not instantiated error. Is there some setup needed for this?

    You have quite good tips and I saw some more at http://www.itwisesolutions.com/PsftTips.html website.

    thnks
    Larry

    • digitaleagle says:

      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.

  3. Madhan says:

    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.

  4. Tim says:

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

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

    Thank
    Tim

  5. Shyam says:

    Great solution… Thanks a lot.

  6. Jay says:

    RemoveDirectory(“c:\temp\mytextfile.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