How Do I Read A JSON File Path And Ignore Certain Characters?

Viewed 42

How do I read a JSON file path and ignore the timestamp or any other characters in the file name? Any help is appreciated, thank you!

Goal:
Read a JSON file starting with ex: Example Data Document and ignore the rest of the time stamp.

Current workflow:
The file path has a common name that it always starts with (ex:Example Data Document.)
At the end of the common name, is usually a time stamp. (I download this file repeatedly on a daily basis in my web automation and the file name always has an updated timestamp in the file name)

How I'm currently reading the file path:

Boolean filePath = JsonPath.from(new File("/User/myName/Desktop/Example Data Document-768-8_23_2022\,\ 4_5102\ PM.json"))

Research I've done:
Looked into Filters and Query but nothing in here seems to indicate what I need.
https://support.smartbear.com/

EDIT: ANSWERED BY:
@tgdavies in comment section of this post.

Resource: listFiles Java method

I am able to read from a directory using the fileList method and filter through only the needed file path name by getting the pre-fix('Example Data Document') and suffix ('.json') and ignoring any other characters.

    import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("/Users/yourUser/Downloads");
  
            // Create a FilenameFilter
            FilenameFilter filter = new FilenameFilter() {
  
                public boolean accept(File f, String name)
                {
                    return name.startsWith("Example Data Document");
                }
            };
  
            // Get all the names of the files present
            // in the given directory
            // and whose names start with "Example Data Document"
            File[] files = f.listFiles(filter);

            // Display the names of the files
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
0 Answers
Related