0

I am trying to get the current date and add it as part of a string, but it is not working. This is what I have:

In User Defined Variables

currentDate    ${__time(dd/MM/yyyy)}

And then later:

CurrentDate = vars.get("currentDate");
TestFile = vars.get("testFile-" + CurrentDate + ".txt");

f = new FileOutputStream(TestFile, true);
user3871995
  • 983
  • 1
  • 10
  • 19

2 Answers2

1
  1. Do you really have variable called i.e. testFile-31/03/2016.txt?
  2. Do you expect to write something into file called like above?

Actually there are some reserved characters which cannot be used in file names, i.e.

< (less than)

> (greater than)

: (colon)

" (double quote)

/ (forward slash)

\ (backslash)

| (vertical bar or pipe)

? (question mark)

* (asterisk)

I would suggest to:

  1. Reconsider your pattern

  2. Remove vars.get at all so your code would look like:

    import java.text.SimpleDateFormat;
    
    sdf = new SimpleDateFormat("dd-MM-yyyy");
    TestFile = "testFile-" + sdf.format(new Date()) + ".txt";
    f = new FileOutputStream(TestFile, true);
    

References:

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

You're missing the $ in front of the {.

See

This Question

Jmeter Functions

Community
  • 1
  • 1
RowlandB
  • 563
  • 5
  • 13
  • Thank you. I've changed that now and it still doesn't seem to be working though. – user3871995 Mar 31 '16 at 12:18
  • is currentDate getting set? Check with a `Debug Sampler` or `log.info(CurrentDate)` in what I assume is BeanShell – RowlandB Mar 31 '16 at 12:51
  • The second part of the code there is in a beanshell. – user3871995 Mar 31 '16 at 12:54
  • Ok, the date is getting set correctly in the debug processer, but the TestFile is not, so must be an issue with: TestFile = vars.get("testFile-" + CurrentDate + ".txt"); – user3871995 Mar 31 '16 at 13:08
  • do you actually have a variable in that or are you simply trying to make a filename? Remember that `vars.get` will only find a variable that has already been declared. I **think** what you're trying to do is just `TestFile = "testFile-" + CurrentDate + ".txt";` – RowlandB Mar 31 '16 at 13:12
  • yes I think that might be what I want. The "testFile-" part is a var of a file path location, which is also in UDV. So I separated that into it's own vars.get and have tried: 'TFile = testFile + CurrentDate + ".txt' but I'm finding the file still is not being created – user3871995 Mar 31 '16 at 14:04
  • ah. Try `TestFile = vars.get("testFile") + "-" + CurrentDate + "".txt";` – RowlandB Mar 31 '16 at 14:31