How to delete a list of files

How-to(s), solutions to common problems, pearls of wisdom

avackova
Posts: 841
Joined: Fri Jul 20, 2007 9:28 am

How to delete a list of files

Postby avackova » Wed May 05, 2010 9:06 am

How to configure the SystemExecute component to delete a list of files? I want to use an input port with a list of files that the SystemExecute component will then delete. Pretty straight forward but I cannot figure out how to reference the input port field within the system command.
Agata Vackova
Javlin a.s.
agata.vackova@javlin.eu

avackova
Posts: 841
Joined: Fri Jul 20, 2007 9:28 am

Re: How to delete a list of files

Postby avackova » Wed May 05, 2010 9:10 am

Input port in SystemExecute component is just redirected stdin. So on Linux the issue can be resolved by setting "xargs rm" as command attribute. Then values from input records
are treated as arguments for rm command.
The issue can be also hacked by Reformat component. Just for each input record create file with name provided by input data record, delete it and return SKIP from transform method:

Code: Select all

import java.io.File;

import org.jetel.component.DataRecordTransform;
import org.jetel.data.DataRecord;
import org.jetel.exception.TransformException;


public class Rm extends DataRecordTransform {
   
   File toRemove;

   public int transform(DataRecord[] arg0, DataRecord[] arg1)
         throws TransformException {
      toRemove = new File(arg0[0].getField(0).toString());
      if (!toRemove.delete()) {
         errorMessage = "Can't remove " + toRemove.getPath();
         return -2;
      }
      return SKIP;
   }

}
Agata Vackova

Javlin a.s.

agata.vackova@javlin.eu


cron