Both are acceptable. The difference is very subtle and is a matter of style.
end in is a phrasal verb
- end in [something]
- end [something] in [something]
end with is a phrasal verb
- end with [something]
- end [something] with [something]
Notice that end in has a specific usage "the meeting ends in disaster" to specify the result, while "the meeting ends with disaster" states the sequence of unrelated events, "the show ended with another famous song".
So if there is a logical connection you may use end in like in
Plural usually ends in -s.
This is making a small logical connection between ending -s and something being plural. You have established the cause in both directions.
If you just want to state how a word ends, with has no such connection "elephant ends with -phant". But: "aluminum ends in -um", because other chemical elements end in -um as well.
The difference is really subtle, and is a matter of style.
So in your example, if you really want to be pedantic and follow some common style, you would use in in case "foo" has some particular meaning, otherwise you use with. This is nothing like a rule, just a point into a direction of making difference between the two, showing the logical layer where they may differ.
Find the first file with the name ending in .txt (suggesting it is a recognizable extension, additionally avoiding double with ... with)