Thread: Deleting contents of a folder

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Deleting contents of a folder

    I need some help on how to delete all contents of a folder. I wanted to make a simple program that deletes everything in my startup folder but not the folder itself.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What OS are you on?
    What part are you having trouble with?
    Post the code you have so far.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can use the system() command in stdlib.h. It's use is normally discouraged because it isn;t portable, but if you're just making a program for personal use, simpler is better, I think. You pass system() a string, and the string is excuted as a shell command. So in windows, you can just do
    Code:
    system("cd c:\startup"); // Or wherever it is
    system("del *.*");
    or on a UNIX system you could use
    Code:
    system("rm *.*");
    instead of 'del'. There is no method in standard c that will do this programmatically - you'll either need to get a system-specific API, or use shell commands like I do above.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Run strace on the rm command and see that happens. This will give a clue on how the system actually removes a file....ie unlink.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Perhaps you can also look at this FAQ which gives much information on how to handle files and directories both on Windows and Unix platform.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    There we go i got it now

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sean View Post
    You can use the system() command in stdlib.h. It's use is normally discouraged because it isn;t portable, but if you're just making a program for personal use, simpler is better, I think. You pass system() a string, and the string is excuted as a shell command. So in windows, you can just do
    Code:
    system("cd c:\startup"); // Or wherever it is
    system("del *.*");

    instead of 'del'. There is no method in standard c that will do this programmatically - you'll either need to get a system-specific API, or use shell commands like I do above.
    suppose i've a notepad file in my desktop(windows OS).then what should i do in order to delete it.more specifically does ur first call to system() takes the path of the file to be deleted.i haven't used system() function ever.asking just in curiosity.

  9. #9
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by BEN10 View Post
    suppose i've a notepad file in my desktop(windows OS).then what should i do in order to delete it.more specifically does ur first call to system() takes the path of the file to be deleted.i haven't used system() function ever.asking just in curiosity.
    you need not...you can specify the absolute path to del DOS command.
    Last edited by creeping death; 04-14-2009 at 07:43 AM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2009, 12:46 PM
  2. deleting a folder AND copying a folder
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2004, 08:48 AM
  3. Replies: 12
    Last Post: 01-23-2002, 07:56 PM
  4. Deleting contents of already opened file?
    By DanMan in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2001, 01:14 AM
  5. removing contents of a folder
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 05:02 AM