# Using Katalon to move directories

Hi.

I’m trying to write a Katalon script which can move directories on my local computer from one parent directory to another. In particular, I wish to use this to move the auto-generated Reports directory upon completion of a suite run. I have tried multiple things but nothing works.

I wish to move folder1 into folder2.

I wrote a command line script in a file moveFolder.cmd.

move c:\users\jdoe\downloads\folder1 c:\users\jdoe\downloads\folder2

When I run it from my Windows Command Line, it works correctly and folder1 disappears and appears in folder2.

So, I try to write a Java script in Katalon.
Runtime runtime = Runtime.getRuntime()
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\jdoe\\Downloads\\moveFolder.cmd")
Process p = pb.start()

This runs through without any errors, but with no effect on the folders.

So I try instead to use the FileUtils library with this Katalon script.
File dir1 = new File("C:\\Users\\jdoe\\Downloads\\folder1");
File dir2 = new File("C:\\Users\\jdoe\\Downloads\\folder2");
FileUtils.copyDirectory(dir1, dir2, DirectoryFileFilter.DIRECTORY);

It passes without an error but no change to the folders.

So I try the Files library with this Katalon script.
File dir1 = new File("C:\\Users\\jdoe\\Downloads\\folder1");
File dir2 = new File("C:\\Users\\jdoe\\Downloads\\folder2");
Files.move(dir1, dir2)

But I get an “Access is denied” error this time.

My theory is that this is all some “user access rights” issue.
Windows 10 for some reason sets all my computer folders to “Read Only”, which I cannot remove. So, I gave all folders “Full Control” rights to users but this affected nothing.

I would appreciate any advice. If running my script from command line works fine, then should I conclude that Katalon somehow blocks certain admin rights when accessing folders?

Ilya

You have this:

This will copy only directories; files will not be copied.

FileUtils.copyDirectory(dir1, dir2);


I guess, when you executed the script (Files.move(dir1, dir2)) accidentally you had Windows Explorer or some other process opening the dir1. That process locked the “dir1” directory so that your script process failed to delete “dir1”.

Windows’ move command is designed to be used interactively in GUI. Windows’ batch commands are very difficult for Java/Groovy app to control.

In case the destination location is existing, the “move” command will show a prompt and ask your Y/N response.

When you use Java’s ProcessBuilder, the prompt emitted by “move” will go into somewhere not obvious. You will not see the prompt, you would have no chance to reply to the prompt. In that case the “move” command would do nothing.

Yes, you should be able to write a Java/Groovy program that can retrieve the prompt from “move”, but it would requires a difficult programming (i have ever done before) that would involve multi-threading. In short, you should not use Windows’ bat command from Groovy.

You just want to move a directory. Then, Apache Commons IO or java.nio.Files are far easier.

1. You said…

This will copy only directories; files will not be copied.

But then why did it not copy the directory or its contents?
Nothing happened, even when both directories were empty of files (as a test)

1. I try this and now it works to copy all files in folder2 to folder 1.
But why the differences?

String dir1 = "C:\\Users\\jdoe\\Downloads\\folder1"
String dir2 = "C:\\Users\\jdoe\\Downloads\\folder2"
File file1 = new File(dir1)
File file2 = new File(dir2)
FileUtils.copyDirectory(file1, file2)

You said…

I guess, when you executed the script ( Files.move(dir1, dir2) ) accidentally you had Windows Explorer or some other process opening the dir1. That process locked the “dir1” directory so that your script process failed to delete “dir1”.

I first tried running the script with both directories open in Windows File Explorer and then with Windows File Explorer closed. In both cases, same error occurs.

I did an experiment. Have a look at the following.

The following statement
FileUtils.copyDirectory(dir1, dir2, DirectoryFileFilter.DIRECTORY);
did copy a set of directories (‘subfolder’), while it did not copy files (‘hello.txt’, ‘subhello.txt’).

What use does DirectoryFileFilter.DIRECTORY have? I have no idea how it can be useful in practice. I have never used it up until know.

Now I found your code was wrong.

I will show you a running example how to use java.nio.file.Files to move a directory.

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

Path dir1 = Paths.get("./tmp/folder1")
Path dir2 = Paths.get("./tmp/folder2")
assert Files.exists(dir1)
Files.move(dir1, dir2)


Before running the code:

After I ran the code:

You seem to know java.io.File but do not know java.nio.file.Files, java.nio.file.Path and java.nio.file.Paths.

You should learn the java.nio.package, which is one of the basic knowledge of Java/Groovy programming since Java 7. Baeldung, “Java Path vs File” is a short and nice read. In this article you would find why you should learn the java.nio.file package. I personally always prefer the java.nio.file package to java.io.File.

My current results are as follows.

1. com.google.common.io.Files seems to work fine with handling individual files. I use it in conjunction with the the built in java File type
2. FileUtils seems to work fine copying multiple file contents of folders around.
3. When FileUtils is used inside a Test Listener’s @AfterTestSuite function in order to move a test suite’s Reports contents, it only seems to select some of the file to move, not all.

I have the following test listener to be run after any test suite completion.

I run a test suite to completion and open my Downloads folder.
Some files are correctly moved.

I look at the reports folder and it has other files too, including the important HTML file, which were not transferred.

I look at the test suite’s console printout and I realize that the HTML report is generated only after the test listener is run!
Does this mean there is no way to extract it without manually going into the Windows File Explorer?

You are right.

To be more precise, Katalon Studio generates the HTML report after the @AfterTestSuite-annotated method in the Test Listener is done. Therefore the Test Listener of a Test Suite misses a chance to operate (copy, move, etc) the HTML generated by Katalon Studio.

You can make a Test Suite Collection (namely “TSC”) in which you will include your current Test Suite (namely “TS1”). And you want to create a new Test Suite namely “TS2”. Obviously the HTML report of TS1 has been generated when the TS2 starts. So you can make TS2 to read the HTML report of TS1 and do whatever you want with it. Cumbersome, untidy, but it works.

One more alternative is possible. In the @AfterTestSuite-annotated method of the TS1, you can directly call a Katalon-builtin class com.kms.katalon.core.reporting.ReportUtil to generate the HTML into the location you want . See the following post of mine:

1 Like

Thank you for all the help. I hope this thread will help others in the future.