Skip to content
Advertisement

JMeter: Groovy code for moving files from one directory to another stopped working. Any better approaches?

I had used the below mentioned Groovy code in the JSR223 Sampler to move my file from one directory to another. This was working until a few days back, now it no longer does. Any suggestions on how to get it working? Also, I tried using a Beanshell sampler to perform the same task, but was not successful with that as well. Open to alternative approaches. Thanks in advance.

Groovy Code in JSR223 Sampler:

def sourceFile = new File('C:/Work/test.xml')
def destinationFile = new File('M:/temp/test.xml')
destinationFile << sourceFile.text

Java code in Beanshell Sampler:

import org.apache.commons.io.FileUtils;

File sourceFile = new File('C:/Work/test.xml');
File destinationFile = new File('M:/temp/test.xml');
FileUtils.copyFile(sourceFile, destinationFile);

Advertisement

Answer

If you want to move the file I would rather suggest using File.renameTo() function like:

def sourceFile = new File('C:/Work/test.xml')
def destinationFile = new File('M:/temp/test.xml')

def success = sourceFile.renameTo(destinationFile)

if (success) {
    log.info('File has been successfully moved')
}
else {
    log.error('Failed to move the file')
}

If you’re looking for an alternative approach it could be using OS Process Sampler and invoking move command from there

Also check jmeter.log file for any suspicious entries, if Groovy script fails somewhere somehow most probably you will be able to find the reason or explanation there

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement