LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2008, 04:34 AM   #1
twistadias
LQ Newbie
 
Registered: Aug 2008
Posts: 5

Rep: Reputation: 0
how to check if a folder is empty


i need to check if a folder is empty. The folder is Desktop/uh
it should return a value like 0(empty) 1(1 or more files)

i need to assign a variable to this value in c++

is this possible?

currently im using
Code:
find -empty
which gives me "."
 
Old 08-11-2008, 04:49 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Code:
>mkdir tmp
>ls tmp
>ls tmp|wc -l
0
 
Old 08-11-2008, 05:08 AM   #3
twistadias
LQ Newbie
 
Registered: Aug 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Code:
>mkdir tmp
>ls tmp
>ls tmp|wc -l
0
Thank you, that worked perfectly
Now how do i set a variable in c++ to the value returned by
Code:
ls tmp|wc -l
i know that i have to use
Code:
system("BASH code in here");
to run linux commands in c++
 
Old 08-11-2008, 05:37 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Shelling out to an external program is rather in efficient - a new process has to be created and all that sort of thing. Better to use existing methods from the c library:

Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <sys/types.h>
#include <dirent.h>

int countEntriesInDir(const char* dirname)
{
        int n=0;
        dirent* d;
        DIR* dir = opendir(dirname);
        if (dir == NULL) return 0;
        while((d = readdir(dir))!=NULL) n++;
        closedir(dir);
        return n;
}

int main(int argc, char* argv[])
{
        if (argc!=2) exit(1);
        std::cout << "entries in " << argv[1] << ": " << countEntriesInDir(argv[1]) << std::endl;
        return 0;
}
The only thing you need to know is that a directory will always contain 2 entries: "." and ".." - yes even the / directory contains "..". You can easily make a "directoryIsEmpty" function by checking if countEntriesInDir(dir) is two or more than two.
 
Old 08-11-2008, 05:58 AM   #5
twistadias
LQ Newbie
 
Registered: Aug 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Where can i read about <sys/types.h> and <dirent.h>
it doesnt show up on http://home.att.net/~gobruen/progs/cpp/headers.html

i got it working using this:

Code:
int check;
char is_empty[100];
FILE * output;

output = popen("cd ~/Desktop; ls Data | wc -l","r"); 
fgets (is_empty, 100, output); //write to the char
pclose (output);

check = atoi(is_empty);

if (check == 0){
cout << "The folder is empty" << endl;
exit(0);
is this efficient though?
 
Old 08-11-2008, 06:12 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
They're part of the the C library, not the C++ standard libraries. See the GNU libc documentation.

Shelling out will work, but it's much harder on the machine. Creating a new shell process is much much more resource hungry than calling the libc opendir readdir and closedir functions.

Last edited by matthewg42; 08-11-2008 at 06:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MailScanner quarantine folder empty justkeny2k Linux - Software 0 12-08-2006 09:33 PM
empty home folder sigmacunfu Linux - Newbie 4 09-24-2006 05:41 PM
script to empty /tmp folder capnp72 Linux - General 8 02-15-2006 01:50 PM
Empty Folder ? wlaw Linux - Networking 1 09-07-2004 04:42 AM
rmdir for non-empty folder Zaius Linux - General 2 01-09-2004 11:09 PM


All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration