Retrieving the Leaf Directory - c++

I've got some older versions of boost code that uses the member function leaf() of the path class in the boost filesystem library. However, when trying to compile it recently, I've noticed this has disappeared and deprecated.
The task it is doing is getting all the directories that are in a specified folder and storing them.
For the sake of this example, let's just say I was outputting them to the console.
For the example, the directories could be:
/home/user/dir/
/home/user/dir/one
/home/user/dir/two
/hone/user/dir/three
So I pass in /home/user/dir into a directory_iterator and check if it's a directory using is_directory(). What I would like would be to print the last part of the paths, so...
one
two
three
I know I could do some string manipulation to do this by searching for the last / etc. but I would like to know if there is still a way to do this using the path class.

As I can read from the Boost documentation, the leaf() method has been replaced with filename(). You can get all the elements using iterators. More in Boost Filesystem Tutorial, including working example of usage.

Related

Get a list of file paths created over a period of time in C++

Is there a way to get a list of file paths created over a period of time in C++?
If using the command find, it is possible like following:
$ find ./* -mmin -2880 -mmin +1440
C++17 provides std::filesystem.
It contains std::filesystem::recursive_directory_iterator which you can use to traverse all paths and filter them based on file time.
I do not see creation time, but there is std::filesystem::last_write_time. Try search documentation for alternatives. It may turn out that you have to use some system specific API.
If you are limited to C++11 then use boost or fall back to system specific API.

Getting specfic files in C++ code from *nix system

I want to get all the files of type A*.txt present in current directory via C++ code. My OS is *Nix system. Also, I want to get the names of such files.
Please suggest how can this be done.
I tried using system command but system doesn't return anything apart a integer which says if command was executed properly or not.
Thanks
There are basically three ways you can go about this.
One is to use basically what you tried before, but using the popen function, which allows you to read the output of the command(s) you run.
The second solution is to use e.g. opendir and readdir or scandir to manually filter and find the files you look for.
The third and easiest way is to use the glob function.
There is actually a fourth way as well, one which is platform independent and more C++-ish than the above methods: Using the Boost filesystem library.

Structuring a Library in SML

I'm currently building a testing library in Standard ML (using Poly/ML as the interpreter). I have the following directory structure:
project/a.sml
project/src/b.sml
project/src/c.sml
...
Where a.sml is just a bunch of calls to use
use "src/b.sml"
use "src/c.sml"
...
b.sml, c.sml etc. are all structure definitions like this
structure ComponentX
struct
...
end
which form nice, logically separated components of the library. I sometimes also create one module in one file, and then introduce a substructure within the same module in another file.
I can then use the testing library fine within the root directory of the project, by calling use "a.sml".
However, I can't seem to be able to use the code outside of its own directory, which is a bit of an issue. For example, say I'm in the parent directory of project. If I then call use "project/a.sml", the subsequent calls to use "src/x.sml" try to find a src directory in the parent (which doesn't exist).
Is there some way to do a relative use, or is there a better way to structure this altogether?
The use function itself in Poly/ML doesn't change the path when it is used recursively. You will need to change the path within the sub-directory explicitly using OS.FileSys.chDir. use is just a function so you could redefine it if you wanted. The OS.Path and OS.FileSys structures could be useful.
An alternative is to reorganise your code to make use of PolyML.make. You would have to rename your files to match the name of the structure that each file contains e.g. ComponentX.sml would contain structure ComponentX. For more on this see polyml.org/documentation/Reference/PolyMLMake.html or a this answer about Poly/ML with nested directory structures.

use boost to return file in a directory

I have a directory that should contain one file. it has an extension of .png but the name is semi-random. I've tried a dozen tutorials for using boost to get the filename but they all exit the console with no error. what is the simplest function to use boost to get this file name?
To access the file/directory names placed n a particular boost::path instance you have, use boosts directory_iterator, as shown in the linked example.
To check for particular file-/directory name signatures found from iterating, boost::regex might come in handy.

Is there any method to know whether a directory contain a sub directory?

I am woking in c++.
Is there any method to know whether a directory contain a sub directory?
CFileFind seems have to search through total files.
It is time consuming if the only subdirectory is at the end of the list and the there are lots of files.
for example: directory A contains 99995 files and one subdirectory at the end of FindNextFile List. had I try 99995 times, then say: yes, it contains subdirectory?
Raymond Chen from Microsoft has written a post that probably applies here: Computing the size of a directory is more than just adding file sizes. In essence, he explains that information like the size of a dir cannot be stored in the dir's entry, because different users might have different permissions, possibly making some of the files invisible to them. Therefore, the only way to get the size the user should see is to calculate it upon request from the user.
In your case, the answer probably stems from the same reasoning. The list of directories available to your app can only be determined when your app asks for it, as its view of the root directory might be different than another app's, running with different credentials. Why Windows store directories along with files I don't know, but that's a given.
Since Win32 is as close as you'll get to the file system in user mode, I'd avoid any higher level solutions such as .NET, as it might only simplify the interface. A driver might work quicker, but that out of the scope of my knowledge.
If you are using the .Net framework you could use Directory.GetDirectories and check is the size of the array is 0. Do not know how if this will give you speed.
If you have control over the directories you could apply a naming convention so that directories that have sub directories are named one way and directories with out sub directories are named another.
You can try using the boost filesystem library.
A class by name directory_iterator [ declared in boost/filesystem/operations.hpp ] has many functions which can be used for listing files, finding whether the file is a sub-directory ( is_directory -- I guess this is what you are looking for ) etc..
Refer the following link for more information.
link text
It seems you are using MFC [ just saw that you are using CFileFind ], didn't see that earlier.
Sorry, Didn't have much info. You may have to use FindFirstFile/FindNextFile.
Whether this can be done very fast is entirely platform-dependent.
On Win32 you use FindFirstFile/FindNextFile or wrappers on top of those like MFC CFileFind and they list items in some order that can't be forced to list directories first.