[C++] Quickly finding directory files using boostfilesystem

  • C/C++
  • Thread starter PPeter
  • Start date
  • Tags
    files
In summary: I'm definitely going to look into this.In summary, the conversation discusses the use of boost filesystem to copy PDF files from one directory to another. The main issue is the large number of files in the directory, making it impractical to iterate through each one to find a specific file. Suggestions are made, including using directory iterators and splitting the directory into smaller segments for searching. It is also suggested to use boost::regex to filter the files. The conversation ends with a helpful API reference for boost software.
  • #1
PPeter
3
1
I'm working on a program which copies pdf's from one directory to another using boost filesystem. My problem is that the directory I'm grabbing files from contained about 14000 files (not including files in sub-directories). So iterating through each one to find something isn't very practical.

I just started using the library, so I don't know the ins and outs. As far as I can see, the only way to get information from the directory is through the directory iterators (and I believe it automatically sorts entries in alphanumeric order from what I've seen). I've thought about incrementing the iterator by something more than 1, but anything I read about when doing that (for iterators in general) warns against trouble when it gets close to the end.

My initial plan was to keep breaking all the directory entries into smaller segments to find what I'm looking for. IE: jump to the middle entry between 2 limits, compare that filename with what I'm looking for, then split the appropriate segment into half again. Then keep going until it's found. I'm not sure if something like this is even possible, or suggested when using iterators. So if anyone has any suggestions or ideas, I'm open to hearing them.Thanks,

Peter
 
Technology news on Phys.org
  • #2
So you're searching for a file by name from a list of 14000 files. If this is a once only search then the iterator is the only way unless boost handles file masks so you could ask for a list of files matching the mask and then search the reduced list iteratively.

If it's a repeated operation then you could iterate through the list and place each file name in an array where you can apply the binary search scheme you mentioned earlier. Although this only works if you know the first few letters of the file name not if the search string is embedded inside the filename.

I found this API reference for boost software:

http://www.boost.org/doc/libs/1_34_0/libs/filesystem/doc/index.htm
 
  • #3
jedishrfu said:
So you're searching for a file by name from a list of 14000 files. If this is a once only search then the iterator is the only way unless boost handles file masks so you could ask for a list of files matching the mask and then search the reduced list iteratively.

If it's a repeated operation then you could iterate through the list and place each file name in an array where you can apply the binary search scheme you mentioned earlier. Although this only works if you know the first few letters of the file name not if the search string is embedded inside the filename.

I found this API reference for boost software:

http://www.boost.org/doc/libs/1_34_0/libs/filesystem/doc/index.htm
Ah, this is very helpful. It doesn't seem that boost::filesystem has the functionality to do that on it's own, but by using boost:regex to filter, it should be able to speed things up dramatically.

Thanks
 
  • Like
Likes Medicol

1. How can I quickly find directory files using boostfilesystem in C++?

To quickly find directory files using boostfilesystem in C++, you can use the boost::filesystem::directory_iterator function. This function allows you to iterate over all the files in a directory and access their names, sizes, and other attributes.

2. Can boostfilesystem be used to search for specific files in a directory?

Yes, boostfilesystem provides several functions that allow you to search for specific files in a directory. These include boost::filesystem::exists to check if a file exists, boost::filesystem::is_regular_file to check if a file is a regular file, and boost::filesystem::is_directory to check if a file is a directory.

3. How can I filter the files returned by the directory iterator?

You can use the boost::filesystem::directory_iterator constructor to specify a filter that will be applied to the files returned by the iterator. This filter can be a regular expression or a function that checks certain criteria for each file.

4. Can I use boostfilesystem to access files in a remote directory?

Yes, boostfilesystem supports accessing files in remote directories using the boost::filesystem::path class. You can specify a remote path as \\server\directory\file and use the same functions as for local files.

5. Is boostfilesystem cross-platform compatible?

Yes, boostfilesystem is designed to be cross-platform compatible and works on various operating systems such as Windows, Linux, and macOS. However, you may need to make some adjustments in your code for specific file system differences.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Computing and Technology
Replies
18
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Replies
14
Views
2K
  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
3K
Back
Top