Thread: delete multiple files

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    delete multiple files

    hey I would like to delete all occurances of .o from a directory, the code below is able to delete any files that if the name ends with o for example if i hardcode hello.o it will be able to be deleted, but I would like to remove all .o suffixs without hardcoding their names. for example as long as the name ends with .o it will be deleted, anyone know any way i can do this? Is there a way to use the * to include all as you do in database queries?

    Code:
    void deleteObjects(struct direct *entry)
    {
            char *ptr;
    
            ptr = rindex(entry->d_name, '.');
    
            if(strcmp(ptr, ".o") == 0)
            {
                    remove(".o");
            }
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Kind of OS dependant. If your OS's command shell supports wildcards, then you can do the deletion with system("deletecommand arguments"); Otherwise, you can perform the serach using directory walking functions specific to your OS (FindFirstFile/FindNextFile, opendir/readdir)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Otherwise, you can perform the serach using directory walking functions specific to your OS (FindFirstFile/FindNextFile, opendir/readdir)
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    Code:
    ptr = rindex(entry->d_name, '.');
    A function called strrchr() in <string.h> searches for the last occurence of a character in a string.

    [offtopic] Wow, someone else who uses SciTE. [/offtopic]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > remove(".o");
    Perhaps remove( entry->d_name);
    Since that is the original file
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Once you've determined it is a .o file, with strrchr()+strcmp() or strlen()-2 + strcmp(). You wouldn't want to delete everything.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM