Thread: how to delete several files at once?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    how to delete several files at once?

    i need to delete several files at one without knowing how many are there.

    but they all start with :f ile

    ex:
    file001
    file007
    file010
    etc

    can i delete them all at once using ansi C? maybe with remove? somethig like remove file*?

    tks

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could do this:
    Code:
    system( "rm -f file*" ); // on UNIX
    // or
    system( "del -f -q file*" ); // on Windows
    I don't think there is any other standard C way of doing it.
    There might be an OS API function that can do it, and it would be much more efficient than using system().
    Otherwise, find each file yourself and call remove() on each one you find...
    "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
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    how about this one

    Code:
    [c_d@localhost c scratchpad]$ cat temp4.c
    #include<stdio.h>
    
    int main()
    {
       int status=0;
       int i=1;
       char buf[10];
       while(status != -1)
       {
          sprintf(buf,"file%d",i++);
          status=remove(buf);      
       }
       return 0;
    }
    OP:
    Code:
    [c_d@localhost c scratchpad]$ gcc temp4.c
    [c_d@localhost c scratchpad]$ touch file1 file2 file3 file4
    [c_d@localhost c scratchpad]$ ls -1 | grep file
    file1
    file2
    file3
    file4
    [c_d@localhost c scratchpad]$ ./a.out
    [c_d@localhost c scratchpad]$ ls -1 | grep file
    [c_d@localhost c scratchpad]$
    you get the basic idea right...
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    pretty much as i am doing. just hoping to find a better way. tks anyway

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    ANSI and many operating systems in themselves (e.g. Linux/Unix) does not know how to perform multiple deletes. I'm not sure if Windows functions know how to delete multiple files or not - I think DOS and CP/M did know.

    --
    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.

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by Ironic View Post
    pretty much as i am doing. just hoping to find a better way. tks anyway
    the simplest way would be to call system() as cpjust demostrated
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by creeping death View Post
    the simplest way would be to call system() as cpjust demostrated
    Perhaps the simplest in the way of how much code to write. But it certainly isn't the simplest for the machine to perform, since it involves starting a whole new process and expand the * into the valid filenames. Doing the same work within the application will be much lighter weight in terms of actual work in the machine.

    [I'm not saying you are wrong - I'm just clarifying which form of "simplest" we are describing - as it can have several meanings].

    --
    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. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. How to Delete files
    By wyvern in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2005, 01:42 PM
  4. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 11:55 PM
  5. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM