delete file(s) using path and name

This is a discussion on delete file(s) using path and name within the C Programming forums, part of the General Programming Boards category; hello, i m from Bangladesh and i m new here . can anyone tell me how can i delete a ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Smile delete file(s) using path and name

    hello, i m from Bangladesh and i m new here . can anyone tell me how can i delete a file using the path and the name of the file???
    thnx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Use remove() from <stdio.h>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This might be what you're looking for:

    Code:
    
    #include <stdio.h>   
    #include <io.h>
    
    int main(void)
    {
       FILE *fp = fopen("junk.jnk","w");
       int status;
    
       fprintf(fp,"junk");
    
       status = access("junk.jnk",0);
       if (status == 0)
          printf("File exists\n");
       else
          printf("File doesn't exist\n");
    
       fclose(fp);
       unlink("junk.jnk");
       status = access("junk.jnk",0);
       if (status == 0)
          printf("File exists\n");
       else
          printf("File doesn't exist\n");
    
    
       return 0;
    }
    Some compilers also have a macro command remove(filename), which then calls unlink.

    Edit: Read only files need to have their file attribute changed with chmod before they can be deleted with unlink, and wildcards are not supported usually.

    And Welcome to the Forum, Ashraf24!
    Last edited by Adak; 02-02-2009 at 01:28 PM.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Adak View Post
    Some compilers also have a macro command remove(filename), which then calls unlink.
    Unless the compiler isn't conforming to the C Standard it should have a remove() function.
    "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

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    using DeleteFile(filePath) my code can not delete all "newfolder.exe" file.where is the bug?
    it produces the following output:

    C:\Documents and Settings\maruf\Desktop\newfolder.exe
    file delete failed

    D:\newfolder.exe
    file delete success

    E:\newfolder.exe
    file delete success

    F:\newfolder.exe
    file delete success

    G:\newfolder.exe
    file delete success

    H:\newfolder.exe
    file delete failed

    K:\newfolder.exe
    file delete success

    total files 7


    i can not delete those two undeleted file manually

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Did you check the Security attributes on the files?
    Are the files locked?
    "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

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    Question

    this seems a strange problem
    this code sometimes delete all newfoder.exe files and sometimes not
    whats the prob?
    if it deletes all newfoder.exe file once , why it is failed for the next time?

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    And what does GetLastError() tell you?
    "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

Popular pages Recent additions subscribe to a feed

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