Return only integers from filename?

I need help on a little snippet to return only the numbers from a filename. For example:

Say I have “fileName.0003.ext”, I need something that will return only the numbers from that filename.

So I can then swap whichever numbers are pulled from the filename to “0001”, which I do know how to do with a replaceText function i have found.


set text item delimiters to "."
return text item 2 of "fileName.001.txt" as integer

Remove “as integer” to get “001” instead of 1.

Fantastic thanks! Really appreciate the help.

This is for a droplet script to open rendered image sequences in a viewing app through the command line.

I actually also found a good way to also list all the file names in the dropped items directory so I can work from there.

(btw: You mean “as string” correct? )

If you say “as string” it will return it as string, but it was going to, anyways, if I didn’t specify it. So it could be left blank to return string.

A gotcha.

I was also wondering how to get a count of files in a list that contain a certain string:

say my list was {“fileA.0001.ext”, “fileA.0002.ext”, “fileA.0003.ext”, “fileB.0001.ext”, “fileB.0002.ext”, “fileB.0003.ext”}

how could I get a count of only the files that contain the string “fileA” using text item 1 from the technique you showed me above?

It’s not the same technique, but…

set mylist to {"fileA.0001.ext", "fileA.0002.ext", "fileA.0003.ext", "fileB.0001.ext", "fileB.0002.ext", "fileB.0003.ext"}
set thelist to {}
repeat with i in mylist
	if i begins with "fileA" then set end of thelist to i as string
end repeat

Fantastic! Really appreciate it.