Java Snippet with mkdir

Hello ,

the purpose of the java snippet code is to create a new directory with a timestamp and to move
all datafiles into this directory. Then the csv writer should write his file into the empty dir.
While developing in windows everthing work fine , deploying
it on the knime server executing on Red Hat the directories are not created, the csv writer does not
write his output file , even though the write permissions for the linux user ‘knime’ are set.
What am I missing?

import java.io.File;
import javax.swing.JOptionPane;

// system variables
public class JSnippet extends AbstractJSnippet {
// Fields for input flow variables
/** Input flow variable: “context.workflow.absolute-path” */
public String c_WAP;

// Fields for output flow variables
/** Output flow variable: “Backup_Success” */
public String out_Backup_Success;

// Your custom variables:
Calendar c_now = Calendar.getInstance();;
String s_now = String.format(“%1$tY-%1$tm-%1$td %1$tH %1$tM %1$tS”, c_now);
File f1,f2,f3,f4;
StringBuilder sb_msg = new StringBuilder();
int move_err_count;
{
out_Backup_Success = “bottom”;
}
// expression start
public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:

if (ROWINDEX == (ROWCOUNT -1)) {
move_err_count = 0;
out_Backup_Success = “bottom”;
f1 = new File(c_WAP);
f4 = null;
sb_msg.setLength(0);
if (f1.exists() && f1.isDirectory()){
while (true){
f2= new File(f1,“data”);
if (!f2.exists() || !f2.isDirectory()){sb_msg.append(“data Verzeichnis existiert nicht!\n”);break;}

		f3= new File(f2,"/data_verified");
		if (!f3.exists()){
			if (!f3.mkdir()){sb_msg.append("data_verified Verzeichnis konnte nicht angelegt werden!\n");break;}
		}

		f4= new File(f3,"/"+s_now);
		if (!f4.exists()){
			if (!f4.mkdir()){sb_msg.append(s_now);sb_msg.append(" Verzeichnis konnte nicht angelegt werden!\n");break;}
		}

		for (File x : f4.getParentFile().listFiles()){
			if (x.isFile()){
				if (!x.renameTo(new File(f4,x.getName().toString()))){
					sb_msg.append(x.getName().toString());
					sb_msg.append(" Datei konnte nicht verschoben werden!\n");
					move_err_count++;
				}
			}
		}
	
		if (move_err_count > 0){break;}
	
		out_Backup_Success="top";
	
		break;
	}
	if (!out_Backup_Success.equalsIgnoreCase("top")){
		JOptionPane.showMessageDialog(null, sb_msg.toString(), "Backup-Info", JOptionPane.INFORMATION_MESSAGE);
	}
}

}

// expression end
}
}

Maybe it is influenced by this setting:
/instance/org.knime.filehandling.core/allow_local_fs_access_on_server=true

… I use Client Profiles for loading the jdbc driver , or determine an alternative temp-directory

→ Is it possible to use this setting only with a specific workflow? so that not all workflows have the right to move files or write access.

1 Like

Hi @b_kochs , any reason why you need to do this in Java?

Why not use the Transfer Files node? You can generate the directory name using the date nodes (you can get today’s date using the Create Date&Time Range node) and String Manipulation, and convert as a variable, and then pass that variable to the Transfer Files node.

The Transfer Files node has the option to create missing folders, so it can create the folder if it’s a new folder.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.