Reading files from a directory path

0
In Mendix, how can I read a list of XML data files from a path in a local directory to load them into Mendix entities? We were able to import one XML data file at a time into Mendix using a microflow providing the file name and the path, but there are about 6000+ XML data files that we'll get every night and so we were trying to get a process that reads all the file names from a directory path so we can try to automate this process. Any help appreciated..
asked
1 answers
0

You could read the file names from the directory and store them in an entity, then loop over the retrieved list of file names. For getting the filenames from the directory use a java action like below. This takes the import directory as string argument and will create all filenames in an entity called fileNames and returns the list of fileNames objects in the microflow. The commit action might not even be needed and then you just have a list of names in memory. Be aware when the list gets large you might run into memory issues

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import myfirstmodule.proxies.fileNames;

import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
    private String dirName;

    public Java_action(IContext context, String dirName)
    {
        super(context);
        this.dirName = dirName;
    }

    @Override
    public java.util.List<IMendixObject> executeAction() throws Exception
    {
        // BEGIN USER CODE
        try {
            File folder = new File(dirName);
            File[] listOfFiles = folder.listFiles();
            List<IMendixObject> fileList = new ArrayList<>();
                for (int i = 0; i < listOfFiles.length; i++) {
                  if (listOfFiles[i].isFile()) {
                    Core.getLogger("Files").debug("File " + listOfFiles[i].getName());
                    fileNames filename = new fileNames(getContext());
                    filename.setFileName(listOfFiles[i].getName());
                    filename.commit();
                    fileList.add(filename.getMendixObject());
                  } else if (listOfFiles[i].isDirectory()) {
                    Core.getLogger("Files").debug("Directory " + listOfFiles[i].getName());
                  }
                }
                return fileList;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        // END USER CODE
    }
answered