3

Is there any way to open files which have common name beginning without specifying their full names and amount? They should be open one by one, not all at once. For example, I have files: certainstr_123.txt, certainstr_5329764.txt, certainstr_1323852.txt.

Or, maybe, it can be more easily done in some another language? Thank you.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Notes: the term for this is globbing (or "shell globs"). Also, you don't want just "common name beginning" (which, by the way, is know as _prefix_). After all, there's also the common _suffix_. – sehe Oct 27 '14 at 10:28

2 Answers2

2

C++ doesn't define a standard way for listing files in that way.

The best cross platform approach is to use a library such as the boost filesystem module. I don't think boost::filesystem has wildcard search, you have to filter files yourself but it isn't difficult.

You could use regular expressions, like in the other answer (it's the perfect-fit solution). Probably it could be enough to check file extension (i->path().extension()) and filename starting with "certainstr_" (boost::starts_with or std::string::substr). If you choose C++11 standard regex library make sure to have a recent libstdc++.

There are a lot of system specific functions. E.g. see:

for some Unix/Windows examples.

You could also try something like (Windows):

std::system("dir /b certainstr_*.txt > list.txt");

or (Unix):

std::system("ls -m1 certainstr_*.txt > list.txt");

parsing the list.txt output file (of course this is a hack).

Anyway, depending on your needs, Python-based (or script-based) solutions could be simpler (see also How to list all files of a directory?):

[glob.glob('certainstr_*.txt')][3]

or also:

files = [f for f in os.listdir('.') if re.match(r'certainstr_\d+.txt', f)]

This is the Python equivalent of https://stackoverflow.com/a/26585425/3235496

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
  • It's a lot of "close references", but nothing that actually answers (the `simple_ls` sample doesn't filter for patterns, the linked answers are decidedly platform specific, the hacks... wild hacks). Referring to python is... Well, I guess the OP asked for that :/ – sehe Oct 27 '14 at 10:27
  • @sehe Thank you for your for your observations. I've added some code and changed a link. – manlio Oct 27 '14 at 16:02
  • Wait a second. What happened here? You add the code sample I posted 5 hours ago and... I get a downvote. You know, I'm happy you amended your answer. +1 from me. – sehe Oct 27 '14 at 16:08
  • @sehe Doubly sorry: the downvote is not mine and reading your answer I was sure you had used C++11 regex, not boost. Time for a pause ;) – manlio Oct 27 '14 at 16:28
1

I suggest just using a regular expression. Pseudo code:

boost::regex reg("^certainstr_\\d+.txt$");
for(recursive_directory_iterator it("."); it != recursive_directory_iterator(); ++it)
{
    if(boost::regex_search(it->string(), reg))
    {
        cout << *it << endl;
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633