# Best way to find latest file having filename pattern?

**URL:** https://www.finalbuilder.com/forums/t/best-way-to-find-latest-file-having-filename-pattern/7118
**Category:** Discussion
**Created:** [September 22, 2021, 12:52pm UTC](https://www.finalbuilder.com/forums/t/best-way-to-find-latest-file-having-filename-pattern/7118 "2021-09-22T12:52:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jonathanmeltzer](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jonathanmeltzer](https://www.finalbuilder.com/forums/u/jonathanmeltzer)
#### Post date: [September 22, 2021, 12:52pm UTC](https://www.finalbuilder.com/forums/t/best-way-to-find-latest-file-having-filename-pattern/7118/1 "2021-09-22T12:52:36Z")

</div>

I have several action lists that start by finding files with a certain pattern (like ‘FilePatternYYYYMMDD.csv’, where YYYYMMDD is the date when the file was created). I want to work with the latest file. I have been creating a fileset based on the pattern and sorting it by filename…but I am not sure of the most efficient way to get the first (or last, depending on sort) filename in the list. I just iterate through, and grab the first (or last) file in the iteration for the variable I want, but it seems like there should be an easier way to just grab the one filename I need.

Would using FileSet to Variable help at all? That would create a text list - could I easily get the first item in a variable list without an iterator? Or is there a way to “break out” of the iteration once I have the file I need?

---

<div class="post-metadata">

### Author: ![Vincent](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/vincent/32/938_2.png) [@Vincent](https://www.finalbuilder.com/forums/u/Vincent)
#### Post date: [September 22, 2021, 11:45pm UTC](https://www.finalbuilder.com/forums/t/best-way-to-find-latest-file-having-filename-pattern/7118/2 "2021-09-22T23:45:10Z")

</div>

Hi Jonathan

You can use the Exit Loop action to break out of the iterator loop.

---

<div class="post-metadata">

### Author: ![jonathanmeltzer](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jonathanmeltzer](https://www.finalbuilder.com/forums/u/jonathanmeltzer)
#### Post date: [September 23, 2021, 11:46am UTC](https://www.finalbuilder.com/forums/t/best-way-to-find-latest-file-having-filename-pattern/7118/3 "2021-09-23T11:46:32Z")

</div>

So the idea would be to sort descending (to grab the latest file first), and as soon as I assign the variable the first time I use Exit Loop? I will try that, thanks.
