473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Check if a directory is empty and empty it

Hi,

I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

Please if you know how could you state what statements in C would accomplish this?

Thanks,
Marcia Hon
Nov 14 '05 #1
5 15710
"Marcia Hon" <Ma********@hotmail.com> wrote in message
news:52*************************@posting.google.co m...
I would like to know how the following may be accomplished in C:

1. Check if a directory is empty.
2. Empty the directory.

Please if you know how could you state what statements in C would

accomplish this?

Marcia,

Please be aware that this newsgroup discusses the C language as defined by
the ISO standard, sometimes also refered to as ANSI C. In standard C, there
are no such things like directories. True, there are files in C, but to do
anything with them, you must know their names, so standard C is not good for
your purpose. You will need to use some language extension for your
platform. These may be different depending on whether you want to program
for DOS, Windows, OS/2, Open VM, Macintosh, Linux, Unix, DeathStation 9000
OS (in increasing order of preference).

Please consult the manual coming with your implementation or ask in a
newsgroup relevant to your compiler and/or operating system.

HTH,

Peter
Nov 14 '05 #2
Marcia Hon <Ma********@hotmail.com> scribbled the following:
Hi, I would like to know how the following may be accomplished in C: 1. Check if a directory is empty.
2. Empty the directory. Please if you know how could you state what statements in C would accomplish this?


None. ISO standard C provides no direct support for directories at all.
You'll have to ask in a newsgroup dedicated to your own implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
- Bob Wiley
Nov 14 '05 #3
Ma********@hotmail.com (Marcia Hon) wrote:
# Hi,
#
# I would like to know how the following may be accomplished in C:
#
# 1. Check if a directory is empty.
# 2. Empty the directory.
#
# Please if you know how could you state what statements in C would accomplish this?

If you want to unconditionally destroy a directory, then you can do something
like

static int deleteDirectory(char *path) {
#ifdef __some-define-name-that-is-only-defined-for-unix__
char deleteFormat = "rm -rf '%s'";
#endif
#ifdef __some-define-name-that-is-only-defined-for-windows__
char deleteFormat = "the windows equivalent of rm -rf '%s'";
#endif
#ifdef etc
...
#endif
char *command = malloc(strlen(path)+strlen(deleteFormat)+1);
int rc;
sprintf(command,deleteFormat,path);
rc = system(command);
free(command);
return rc;
}

Otherwise, you will need some system specific interface to examine a
directory contents, define what 'empty' means, and a system specific
way to delete files and directories.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
If you plan to shoplift, let us know.
Thanks
Nov 14 '05 #4
Thanks!!!

"Derk Gwen" <de******@HotPOP.com> wrote in message
news:10*************@corp.supernews.com...
Ma********@hotmail.com (Marcia Hon) wrote:
# Hi,
#
# I would like to know how the following may be accomplished in C:
#
# 1. Check if a directory is empty.
# 2. Empty the directory.
#
# Please if you know how could you state what statements in C would accomplish this?
If you want to unconditionally destroy a directory, then you can do something like

static int deleteDirectory(char *path) {
#ifdef __some-define-name-that-is-only-defined-for-unix__
char deleteFormat = "rm -rf '%s'";
#endif
#ifdef __some-define-name-that-is-only-defined-for-windows__
char deleteFormat = "the windows equivalent of rm -rf '%s'";
#endif
#ifdef etc
...
#endif
char *command = malloc(strlen(path)+strlen(deleteFormat)+1);
int rc;
sprintf(command,deleteFormat,path);
rc = system(command);
free(command);
return rc;
}

Otherwise, you will need some system specific interface to examine a
directory contents, define what 'empty' means, and a system specific
way to delete files and directories.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
If you plan to shoplift, let us know.
Thanks

Nov 14 '05 #5
On Sun, 8 Feb 2004 18:35:49 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote:
<snip>
[directories] may be different depending on whether you want to program
for DOS, Windows, OS/2, Open VM, Macintosh, Linux, Unix, DeathStation 9000
OS (in increasing order of preference).

Is there really an Open VM, or do you mean OpenVMS? And you might
want to separate classic MacOS and MacOS X -- probably in that order.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
6
by: Tung Wai Yip | last post by:
Can I add empty directory using zipfile? When I try to add a directory it complains that it is not a file. tung
1
by: Dennis | last post by:
Hi, I am wondering that the Application Name in Virtual Directory is compulsory be non-empty field when configuring Virtual Directory for ASP.NET application? I realized some of my ASP.NET...
2
by: DC Gringo | last post by:
How do I check to see if a file directory is empty? If (System.IO.File.Exists(*.*)) Then pnlMyPanel.Visible = True End If -- _____ DC G
3
by: Bill nguyen | last post by:
Is there a quick way to determine if a directory is empty. Directory.Getfiles() doesn't tell me how many files without having to loop thru the list. Thanks Bill
7
by: Anil Gupte | last post by:
Private Sub mnu2Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu2Exit.Click Dim fDir As String = Path.GetDirectoryName(L3Global.VideoFileName) ...
16
by: mtuller | last post by:
I am creating a page for nomination and want to let the information pass if any field are filled out, but if none are filled out, a message will appear. I can't get the check to happen on multiple...
5
by: Gordon | last post by:
I'm trying to remove a directory and all its contents from within a script. I wrote a recursive function to take care of it, but when I run it I get random "Directory not empty" error messages. ...
2
by: lairpr74 | last post by:
I am trying to update the managers name in active directory with an application I got that updates active directory. I been strugling with this for a few weeks now and couldn't find the best way...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.