Dynamic File Name

Hello Everyone,
I want to read a generated file in my directory, but i face an issue.
The name of the file change every execution and he takes random numbers.
Ex: Test_1652225700072_201.txt

Can any one help to found a way to read it PLZ.

Regards,
Morad

You can use List Directory from the OperatingSystem library to get files matching the pattern and then use the returned path with Get File. Something like this:

${paths} =    List Directory    ${directory}    Test_*.txt
Length Should Be    ${paths}    1
${file} =    Get File    ${paths}[0]

We could also consider enhancing Get File so that it would work with patterns directly. Basically we could allow using it with paths like Test_*.txt as long as the pattern matches exactly one file. If you think it would be a useful enhancement, please submit an issue.

Hi Morad,

Are you sure it’s “random numbers”?

That filename looks like epoch time in milliseconds (13 digits)

1652225700072 = GMT : Tuesday, May 10, 2022 11:35:00.072 PM

Some epoch time functions return the time as a float so the 201 is probably thousands of a millisecond and the dev just replaced the decimal point with an underscore.

if you know what time the file is generated (is it always the same time) then you could just generate a 10 digit epoch time (time in seconds) and use a file pattern like Test_${epoch}*.txt. That would look something like this:

${epoch} =	Get Time	epoch
${file} =    Get File    Test_${epoch}*.txt

if you don’t know the exact time the file will be created then you could try truncating an epoch time for the current day to get something near the file time. 3600 seconds is an hour, so truncating a 10 digit epoch time to the first 6 digits gives you within an ~2.5 hour window (3600 * 2.5 = 9000), adjust the, knowing this you can adjust the example below to the precision you need.

${epoch} =	Get Time	epoch
${timeprefix}		Set Variable		${epoch}[0:6]
${file} =    Get File    Test_${timeprefix}*.txt

Dave.

Hello,
Thanks for all of you to helping me, I resolved It.

Regards,
Morad

1 Like