File name is not the same from day to day

#1

Hi,

Is it possible to “Launch application” choosing today’s file when the file name is not exactly the same?
In the picture, you can see that the 6 digits are different from day to day. We have created a string using Today but the last 6 digits are random. Can they be sorted out in some way in the process so the Robot will recognize today’s file?

#2

Hello Tomas.
I think that you can use “Get Folder Contents” to read all files from a folder and then sort out your file with the specific mask using regexp. Also you can use option “Date Modified” to filter out your file. Please pay attention that result of this action is List, but you should receive the only element that you can assign to the String variable.

#3

Hi again,

How would you recommend us to handle this file library? It is not every day that we receive the file. Today for example we received the file on saturday 11th of May, so we cannot use a string with date “today” or “yesterday” for example. And the file have a different name each day. (last 6 digits). Can the process somehow be defined to get the last “Data modified” file? Because it will always be the latest file that we want to use.

#4

Thank you. Unfortunately, there is no option to define the latest modified date via simple action. We already have suggestion to add this to the product: Get Folder Contents - Order List Chronologically.
I see 2 possible variants:

1. To use Mouse click actions to order file list by “Date modified” column and then get the first file name from this list.
2. Create custom script to get the latest modified file and return its name as script result.
1 Like
#5

Hi @tomas.axelsson. I’ve created a sample custom script that can help you to find the latest modified file. But please pay attention that you need to define Recorder variables folder and file_name in your script. For example:

@CustomScriptAction(
input = ['folder'],
output = 'file_name'
)

def customScript() {

def File[] files = new File(folder.toString()).listFiles()
Date dt = new Date(0);

for (File file : files) {
if (file.isFile())
{
Date date = new Date(file.lastModified());

if (date.after(dt))
{
dt = date;
file_name = RString.of(file.getName().toString());
}
}
}
}


Hope this helps you.

1 Like
#6

Hi,

We received an error message trying to run the script. Please see pictures below of the script and error message.

Error executing CustomScriptAction
com.workfusion.studio.rpa.recorder.playback.PlaybackException: Error executing TemplateAction[templateName=CustomScriptAction.ftl,id=3,name=Optional[CustomScriptAction],parent=-1,nextSibling=4,arguments=ActionArguments[delay=[0],xsi:type=[recorder:CustomScriptAction, recorder:CustomScriptAction],name=[Find latest CSV-file],pollingInterval=[300],active=[true],actionDetails=[(Find latest CSV-file)],script=[@CustomScriptAction(
input = [‘Z:\CSV\in’],
output = ‘CSVSaldo’
)

def customScript() {

    def File[] files = new File(folder.toString()).listFiles()
Date dt = new Date(0);

for (File file : files) {
if (file.isFile())
{
Date date = new Date(file.lastModified());

if (date.after(dt))
{
dt = date;
file_name = RString.of(file.getName().toString());
}
}
}


}],awaitTimeout=[5000]]]
at com.workfusion.studio.rpa.recorder.playback.flow.StandardControlFlow.execute(StandardControlFlow.java:54)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:28)
at com.workfusion.studio.rpa.recorder.playback.action.template.TemplateAction.execute(TemplateAction.java:15)
at com.workfusion.studio.rpa.recorder.playback.player.ActionPlayer.next(ActionPlayer.java:64)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.playNextAction(PlaybackLogic.java:152)
at com.workfusion.studio.rpa.recorder.player.PlaybackLogic.run(PlaybackLogic.java:112)
Caused by: com.google.common.util.concurrent.UncheckedExecutionException: com.workfusion.studio.rpa.recorder.utils.CustomActionCompilationFailedException: startup failed:
methodSource: 2: unexpected char: ‘’ @ line 2, column 16.
input = [‘Z:\CSV\in’],
^

#7

You don’t need to put values into the script, you need just put only names of your Recorder variables. Script will return name of file without path, like test.txt in the variable file_name (or how you’d like to name your Recorder variable).

1 Like
#8

Hi,

We have now changed back to folder and file_name. As you can see the robot goes through the step without error, however it does not open the file we want.

#9

How do you open this file? Perhaps, you need to have full file name with path to it. You can do it via Join Strings action.

#10

Hi,

We don´t understand your question. We thought the custom script was designed to help us open the file?
Could you show us how you would set this up so that the robot goes in to the folder and open the newest file based on date modified or date as part of the file name?

#11

Hello @tomas.axelsson.
This script helps to get the required file name - name of the latest file by modified date. Then you can use this file name further in your script, for example, in “Open spreadsheet” action. Your initial question was

So “Launch application” doesn’t help you in this case, but you can find the file name with the help of custom script and then use it as parameter in further actions.

#12

Hi,

This is the file location in our “Launch Application” for this file. But tomorrow, the file name will be different. In this case it means that the date and the last 6 digits will be differrent. How can the script help us to find the correct file in the folder tomorrow?

“c:\openfile.bat” “Z:\CSV\in\CSVSaldo_20190510.021015.xls”

#13

Not sure about “openfile.bat”, but I would do as follows:

1. Get the latest file name via script above.
2. Create full file name (folder + file_name) via Join Strings.
3. Set “file_name” (result of p.2) as parameter in “Launch application” or in “Open spreadsheet” depending on what needs to be done with this file. You can find how to do it for “Launch application” in our guide: https://kb.workfusion.com/display/RPAe/Application#Application-LaunchApplication. In video you can see the example of usage variables in this action.
4. Add necessary actions to process file further.

Hope this helps.

#14

Sorry, we don´t understand how to set this up. Could you please send a screenshot of how you would set this up in a process?

#15

The sample of flow is simple, but step 4 can differ depending on how you process this file. In this case I open it via Excel. Please see screenshot:

Pay attention that parameters in “Executable command” are separated by space.

1 Like
#16

I have added the Joint Strings, but the only file that opens is the one we have requested in Launch Application, not the latest data modified file in the folder. It does not seem that the script is working, nothing happens, it just goes to the Launch Application step.

We have also tried this with your settings with Excel 2016, but it doesn´t work either. Please see printscreen 2 for this.

Is it possible to share screens with you in a Microsoft Team session or something to look at the process live?

#17

Yes, because you need to set up in command line your Recorder variable, not the exact file name. It’s best practice to use variables in your script instead of hardcoded values. More information about Recorder Variables you can find in our Knowledge Base: Recorder Variables
You should change your action settings as I showed in my previous post to use variables:

#18

We tried again on the Excel 2016 version and now it looks better. It is trying to open the last data modified file but generates an error. The error is the "in"CSVSaldo. Why is “in” added the file location in the process?

#19

Please add slash in the end of path in the variable folder:

1 Like
#20

Works fine now! Even with excel 2013.
Many thanks for your help, and patience

2 Likes