You should consider solving your problem in a different way.
By using the
opendir and
readdir functions to get the entries in a directory,
you automatically have the filename separate from the directory name,
and you can do it
all without spawning an external process.