2

I am passing a string representing a path to a folder as a parameter to a constructor.

Folder name, the files in it and their names can vary but the extensions of two files is always the same: *.caffemodel and *.prototxt.

How can I refer to these two files in the specified directory using their extension without having to scan the entire folder to get their complete names?

As an example here is the code of my class:

#include "NeuralClassifier.hpp"


NeuralClassifier::NeuralClassifier(std::string path)
{
  std::string modelName = path + "/*.caffemodel";
  std::string protoName = path + "/*.prototxt";
  cv::dnn::Net net;
  try {
    net = dnn::readNetFromCaffe(protoName, modelName);
  }
  catch (cv::Exception& e) {
    std::cerr << "Exception: " << e.what() << std::endl;
    if (net.empty())
    {
      std::cerr << "Can't load network by using the following files: " << std::endl;
      std::cerr << "prototxt:   " << protoName << std::endl;
      std::cerr << "caffemodel: " << modelName << std::endl;
      std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
      std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
      exit(-1);
    }
  }
}
roschach
  • 8,390
  • 14
  • 74
  • 124
  • This is not a C++ specific issue, even if you were to make a system call to some *nix utility; I don't think there's any filesystem that natively supports this without scanning the nodes in the directory; I may be wrong though, I'll be glad to be corrected on this... – WhiZTiM Oct 22 '18 at 10:43
  • Just curious, if you already have the `path`, what's stopping you from specifying the `fileName` as some fields from the class, using getter/setter and/or extra parameters in the constructor, or better yet, using whole paths as parameters in the constructor? – TuanDT Oct 22 '18 at 10:46
  • Thanks for your replies. Suppose I have many different folders and the names of files inside those folders change. I would like to test the code with different models (loading different folders basically) so instead of rewriting each time both the path and the file names that sometimes are long and boring, I was thinking it was possible to change just the path. Apparently from what you are saying it is not possible without scanning the directory or using regular expressions somehow. – roschach Oct 22 '18 at 10:51
  • @FrancescoBoi I see. Then I suppose the folder paths are also generated by some code elsewhere. Perhaps it's neater if you just enhance that piece of code which generates the folder paths to generate the full path of the files, rather than trying to figure out some special handling for file names by extensions. – TuanDT Oct 23 '18 at 08:42
  • The folders and models are not generated by code of mine but it is generated by another work. I want to take models trained by other people. – roschach Oct 23 '18 at 08:57

1 Answers1

1

Well at some point the operating system needs to know the exact filename that your application wants to open. In your case you specify a wildcard '*' which could match none to several files.

If you just want to open the first file matching the wild card you could use boost::filesystem to ease file handling:

Can I use a mask to iterate files in a directory with Boost?

uceumern
  • 877
  • 10
  • 14