How to replace the value inside a file that was located in local using javascript?

326 views Asked by At

I have a scenario where after reading the file, it needs the value inside that file to be replaced.

We have this script from our JMeter where we based our script from. (Please refer to the code below)

def file = new File('C:/Peak2020/China/${__time(YMMdd)}-085644-336_000101-plant 8956.xml')
def newConfig = file.text.replace('596791365558876095', '000101')
file.text = newConfig
def newConfig2 = file.text.replace('C6D-CN-NBB2829A', 'C7D-CN-NBB$4568792B')
file.text = newConfig2
def sku = file.text.replace('323094-01', '45903-01')
file.text = sku

I tried doing it in the Neoload, using the replace() but it's not working. It does copy the file from sourcefolder to destinationfolder but the value was not changed. (Please refer to the code below)

var file = new java.io.BufferedReader(new java.io.FileReader("C:\\Peak2020\\China\\testSource1.xml"));

var line = file.readLine();
var id = line.replace(new RegExp("596791365558876095", "12345678"), "");
var destFile = line;

var writer = new java.io.FileWriter("C:\\Peak2020\\Teemp\\TestDestination3.xml",true);
writer.write(destFile);
writer.close();

Does anyone knows what right javascript code to use? Thank you.

1

There are 1 answers

0
Calixto González On

String is immutable in Javascript and Java you are replacing it in

var id = line.replace(new RegExp("596791365558876095", "12345678"), "");

but then you use the var line again for the new variable.

var destFile = line;

it should be

var destFile = id;

because replace will return a new string with the replaced value.