Thread: deleting all files in a directory using c..

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool deleting all files in a directory using c..

    ..here's a code i made to delete all files in a specified directory....

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    int main()
    {
    	clrscr();
    	system("cd\\");
    	system("cd c:\\test_dir");
    	system("del *.* -f -s -q");
    	getch();
    }
    ...but sadly, it won't work, and i don't know why. Whenever i run the program it outputs:

    Too many parameters - -f

    ...i tried it in dos, it worked.. how come it won't work on c?

    ...is there a way to delete all files in a directory using c? thanks in advance... ^.^

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I would be surprised if it worked. My command prompt works like this:

    C:\Documents and Settings\JCK>cd foo

    C:\Documents and Settings\JCK\foo>del * /q

    C:\Documents and Settings\JCK\foo>cd ../

    C:\Documents and Settings\JCK>rmdir foo

    Leading me to believe you are using arguments your computer doesn't understand.
    And if this is all your program will do, then please acquaint yourself with some of the other options that will do this besides system.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    system("cd\\");
    system("cd c:\\test_dir");
    Unfortunately, these create another process, and all that does is change directory in the sub-process (which then dies), leaving this process in the SAME directory.

    If you want to string lots of system() calls together, then write a batch file.
    Otherwise, learn to use the equivalent API calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    ...if that's the case, then i would use the remove() function..

    Code:
    .
    .
    .
    remove("c:\\test_dir\\*.*");
    .
    .
    .
    i tried it in a test directory i made with some files in it, but still, it won't work...

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by ShadeS_07 View Post
    ...if that's the case, then i would use the remove() function..

    Code:
    remove("c:\\test_dir\\*.*");
    i tried it in a test directory i made with some files in it, but still, it won't work...
    That probably won't work for two reasons:
    1) remove() calls rmdir(), and the directory for rmdir() must be empty.
    2) There's likely no file/folder named *.*. You'll probably need to iterate through the directory yourself. (I doubt it'll fill the *.* part in for you.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    ..so how am i going to delete all files in a directory? is there a way? thank you very much for the helps... ^.^

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't believe remove calls rmdir. However, remove will delete the filename specified, and if you don't have a file with the name "*.*", it won't work. If you want it semi-portable, you would have to find the files [there's a FAQ on finding files in a directory], then delete each file using remove or similar.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting multiple files using C
    By dj.turkmaster in forum C Programming
    Replies: 8
    Last Post: 01-26-2009, 12:31 PM
  2. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 02:02 PM
  3. Copying all files in a directory
    By rak1986 in forum C Programming
    Replies: 2
    Last Post: 08-25-2008, 01:02 AM
  4. Replies: 4
    Last Post: 07-24-2008, 09:02 AM
  5. Permanently deleting files
    By Yasir_Malik in forum Windows Programming
    Replies: 1
    Last Post: 08-01-2006, 10:07 PM