Custom Script- Cannot create file object using input variable

When I create a string variable within the script to hold the filepath (test_path), everything works fine. But when I use the input recorder variable (which needs to be a string, not RString, so “logfile_path.toString()”), the logfiles array is null. test_path and logfile_path are the same path. Here’s the simplified script:

    input = ['logfile_path'],
    output = 'oldest_logfile'

def customScript() {
    String test_path = "C:/Users/Q1230046/Desktop"

    //this doesn't work, but if I use test_path it does
    File[] logFiles = new File(logfile_path.toString()).listFiles()

Hi @madelineb,

I guess you’d like to find the earliest file in your Custom Script. I can suggest to have a look at this example: Find last modified file. It returns the name of the latest modified file without path.
Please don’t forget to create Recorder variables and set default value for folder variable if you would like to test this sample on your local PC.

Thanks for showing me the “find last modified file” code sample. However, even when I use it, the File[] array is still null if I use the input variable to create it. However, when I use a variable defined within the script it works. I’ve created recorder variables for both my input and output variables. They are both strings and I’ve set a default value for the input variable.

Hello @madelineb,

Please show your input variable in Recorder Variable window.

After further debugging my code, I realized that the error only happens when I get the number of files using the .size() method. I changed my code to manually count the number of files, and now everything works as expected (I don’t know why, but it works!).

def num_files = 0
for(File f: logFiles) {
1 Like

Hi @madelineb,

Thanks for the update and sharing the information. Great that you solved your issue! :muscle: