[resolved] How to copy files with keeping folder structure ?

Six Stars

[resolved] How to copy files with keeping folder structure ?

Hi,
I have a folder structure with files scattered across the folders and I want to copy all files to another destination keeping the folder structure.
How can I do that ?
Regards,
Antoine

Accepted Solutions
Sixteen Stars

Re: [resolved] How to copy files with keeping folder structure ?

This is an interesting question and one that I had very recently. However, it appears to be harder than it actually is. The following will sort this for you as long as every folder of the structure you want to recreate has a file in it. The job will look like below..


The tFileList is set up like below.....

The tFixedFlowInput outputs one column which uses one of the variables from the tFileList....

((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))

The tJavaRow has the following code....

//Get file path
String fileToCopy = input_row.fileToCopy;
//Replace back slashes to forward slashes - Windows
fileToCopy = fileToCopy.replace('\\', '/');
//Set fileToCopy value
output_row.fileToCopy = fileToCopy;
//Replace the start of the path to change it to the new start or root
String outputPath = fileToCopy.toUpperCase().replaceFirst("C:/TEST", "E:/TEST/NEW_LOCATION");
//Remove the file name from the path so that only the path is left
outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
//Output the new file path
output_row.outputFilePath = outputPath;

In the tFileCopy you need to set the "File Name" as the "fileToCopy" column and the "Desitination directory" as the "outputFilePath" column. 
This should do it for you.

All Replies
Sixteen Stars

Re: [resolved] How to copy files with keeping folder structure ?

This is an interesting question and one that I had very recently. However, it appears to be harder than it actually is. The following will sort this for you as long as every folder of the structure you want to recreate has a file in it. The job will look like below..


The tFileList is set up like below.....

The tFixedFlowInput outputs one column which uses one of the variables from the tFileList....

((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))

The tJavaRow has the following code....

//Get file path
String fileToCopy = input_row.fileToCopy;
//Replace back slashes to forward slashes - Windows
fileToCopy = fileToCopy.replace('\\', '/');
//Set fileToCopy value
output_row.fileToCopy = fileToCopy;
//Replace the start of the path to change it to the new start or root
String outputPath = fileToCopy.toUpperCase().replaceFirst("C:/TEST", "E:/TEST/NEW_LOCATION");
//Remove the file name from the path so that only the path is left
outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
//Output the new file path
output_row.outputFilePath = outputPath;

In the tFileCopy you need to set the "File Name" as the "fileToCopy" column and the "Desitination directory" as the "outputFilePath" column. 
This should do it for you.
Six Stars

Re: [resolved] How to copy files with keeping folder structure ?

It's running fine.
Many thanks.
Antoine