0

I am trying to rename a file using beanshell sampler in jmeter

I have simple code where I am trying to assign the path (dynamically change filename and append to the path) to a file func.

String filename=  "\"C:\\Users\\Thaneer_M\\Downloads\\apache-jmeter-2.13_save\\JmeterRecordings\\PerfIssues\\All Savers Insurance Company_PerformanceCheck"+024+".xlsx\"";
File file = new File(${filename});

File file2 = new File("C:\\Users\\Thaneer_M\\Downloads\\apache-jmeter-2.13_save\\JmeterRecordings\\PerfIssues\\All Savers Insurance Company_PerformanceCheck025.xlsx");

boolean success = file.renameTo(file2);
if (!success) {
log.info "file renamed successfully"
}

I am able to successfully renamed the file if I use a static filepath like

File file = new File("C:\\Users\\Thaneer_M\\Downloads\\apache-jmeter-2.13_save\\JmeterRecordings\\PerfIssues\\All Savers Insurance Company_PerformanceCheck025.xlsx");
File file2 = new File("C:\\Users\\Thaneer_M\\Downloads\\apache-jmeter-2.13_save\\JmeterRecordings\\PerfIssues\\All Savers Insurance Company_PerformanceCheck026.xlsx");
 boolean success = file.renameTo(file2);
if (!success) {    log.info "file renamed successfully"    }

error:

inline evaluation of: ``String filename=  ("C:\Users\Thaneer_M\Downloads\apache-jmeter-2.13_save\JmeterR . . . '' Token Parsing Error: Lexical error at line 1, column 24.  Encountered: "U" (85), after : "\"C:\\"

the files name change dynamically and I want to be able to create filepath string dynamically by appending integer to the file name.

Can some one please advise.

thank you

mo0206
  • 791
  • 5
  • 20
  • 36
  • I'd try using \\\\ instead of \\ – MarianP Dec 04 '15 at 22:40
  • @MarianP the path of the file is C:\Users\Thaneer_M\Downloads\apache-jmeter-2.13_save\JmeterRecordings\PerfIssues\All Savers Insurance Company_PerformanceCheck290.xlsx actually so I am escaping \ by using \\ is it right ? – mo0206 Dec 04 '15 at 22:47
  • first \\ escapes to a java string containing \, bean shell evaluates again so it might need another escaping. or just use / – MarianP Dec 04 '15 at 22:49
  • using just / did not work ..:( – mo0206 Dec 04 '15 at 23:12

2 Answers2

2

Few suggestions:

  1. Remove starting and ending \", they're not required
  2. Make sure you have double slashes everywhere. Alternative cross-platform option will be replacing slashes with File.separator like:

    "Users" + File.separator + "Thaneer_M" + File.separator + "..."
    
  3. Beanshell treats 024 is an Octal integer, make sure you use it correctly and know what you're doing. If you need exactly "024" value it's better to pass it as a string

Some debugging options:

  • log.info("something") will print the line to jmeter.log file. This way you can see variable values
  • Placing debug(); line at the very beginning of your Beanshell script will trigger debug output to stdout
  • surrounding your code with try/catch and printing exception stacktrace to jmeter.log provides more information on Beanshell errors, like:

    try {
        //your code here
    }
    catch (Throwable e) {
        log.error("Error in Beanshell", e);
    }
    

See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more detailed information on Beanshell scripting in JMeter.

Community
  • 1
  • 1
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Same thing happened for me. To solve the problem I performed the following thing in Beanshell code:

  1. Open the source file.
  2. Copy the contents to a temp file
  3. Delete the source file using file.delete()
  4. Create a new file with the same name as the source file.
  5. Copy contents of temp file in this new file.
  6. Delete temp file.

I know this is not the best approach but this worked in jmeter 3.0.

Thanks, Sumit Pal.

Sumit
  • 856
  • 5
  • 18
  • 38