LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 03: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, 03:49 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,389

Rep: Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774Reputation: 2774
Code:
>mkdir tmp
>ls tmp
>ls tmp|wc -l
0
 
Old 08-11-2008, 04: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, 04:37 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 04: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, 05:12 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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 05: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 08:33 PM
empty home folder sigmacunfu Linux - Newbie 4 09-24-2006 04:41 PM
script to empty /tmp folder capnp72 Linux - General 8 02-15-2006 12:50 PM
Empty Folder ? wlaw Linux - Networking 1 09-07-2004 03:42 AM
rmdir for non-empty folder Zaius Linux - General 2 01-09-2004 10:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:44 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
Open Source Consulting | Domain Registration