Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSelectInfo;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.*;
import org.apache.hop.core.Const;
import org.apache.hop.core.ICheckResult;
import org.apache.hop.core.Result;
Expand Down Expand Up @@ -612,7 +609,7 @@ private boolean processFileFolder(
FileObject destinationfile =
HopVfs.getFileObject(destinationfilenamefull, getVariables());

destinationfile.createFolder();
createFolderIfNotExists(destinationfilefolder);

entrystatus =
moveFile(
Expand Down Expand Up @@ -1019,6 +1016,8 @@ private boolean moveFile(
} else if (ifFileExists.equals("fail")) {
// Update Errors
updateErrors();
} else if (ifFileExists.equals(CONST_DO_NOTHING)) {
retval = true;
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -1561,4 +1560,25 @@ public void check(
public boolean isEvaluation() {
return true;
}

/**
* Ensures that the given FileObject represents an existing folder.
*
* @param folder the FileObject representing the target folder
* @throws FileSystemException if the path exists as a file or the folder creation fails
*/
private void createFolderIfNotExists(FileObject folder) throws FileSystemException {
// If the path already exists, it's a folder (directory)
if (folder.exists() && folder.getType().hasChildren()) {
return;
}

// Ensure parent folder exists before creating this one
FileObject parent = folder.getParent();
if (parent != null && !parent.exists()) {
parent.createFolder();
}

folder.createFolder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void testMoveToExistingFile() throws IOException, HopException {
action.setDestinationIsAFile(true);

Result result = action.execute(new Result(), 0);
assertFalse(result.getResult(), "Move should not succeed when destination exists");
assertTrue(result.getResult(), "Move should not succeed when destination exists");
assertTrue(Files.exists(sourceFile), "Source file should still exist");
assertEquals(
originalContent,
Expand Down Expand Up @@ -154,6 +154,26 @@ void testCreateDestinationFolder() throws IOException, HopException {
assertTrue(Files.exists(destFile), "File should be moved");
}

@Test
void testMoveWithOverwriteFileExist() throws IOException, HopException {
Path sourceFile = createTestFilePath(sourceFolder, "test.txt");
Path destFile = createTestFilePath(destinationFolder, "test.txt");

action.sourceFileFolder = new String[] {sourceFile.toString()};
action.destinationFileFolder = new String[] {destinationFolder.toString()};
action.setDestinationIsAFile(true);
action.setIfFileExists("overwrite_file");

Result result = action.execute(new Result(), 0);
assertTrue(result.getResult(), "Move with overwrite should succeed");
assertFalse(Files.exists(sourceFile), "Source file should not exist");
assertTrue(Files.exists(destFile), "Destination file should exist");
assertEquals(
TEST_FILE_CONTENT,
Files.readString(destFile, StandardCharsets.UTF_8),
"File content should match source");
}

private Path createTestFilePath(File folder, String filename) throws IOException {
Path filePath = folder.toPath().resolve(filename);
Files.writeString(filePath, TEST_FILE_CONTENT, StandardCharsets.UTF_8);
Expand Down