Don't bother with the system call(s):
opendirreaddirclosedir
grepand maybe:
File::Basename (ok, maybe not)
and you can strip off a fixed string like '.txt' with
substr even though a regex looks cooler (and I admit is
more self-documenting) as others have shown. I don't get whether
you just want to process '.txt' files or you want to strip
extentions off of all files. I'd build a list of all the files (grep'ing just the '.txt' files?)
then grep the $count from the list each time, or order the list by the implied $count in
the files, and process the list in order.