delete multiple files

This is a discussion on delete multiple files within the C Programming forums, part of the General Programming Boards category; hey I would like to delete all occurances of .o from a directory, the code below is able to delete ...

  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)

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, 07:47 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21