Delete Files Older than 2 years

This is a discussion on Delete Files Older than 2 years within the C++ Programming forums, part of the General Programming Boards category; I'm writing this to delete PDF files that are older than two years. I have it right now so that ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question Delete Files Older than 2 years

    I'm writing this to delete PDF files that are older than two years. I have it right now so that it deletes all PDFs. It runs, but when the program finishes I get a segmentation fault. How do I fix this? Then how to I subtract 2 years from my variables st or ft?

    Code:
    #include <iostream>
    #include <windows.h>
    /*
        Deletes all PDF files older than 2 years
    */
    using namespace std;
    
    int main()
    {    
        char delme[]="";
        int ErrorCode;
        SYSTEMTIME st;
        FILETIME ft;
        LARGE_INTEGER mine, file;
        HANDLE hFind;
        WIN32_FIND_DATA FindData;
        
        GetSystemTime(&st);
        SystemTimeToFileTime(&st, &ft);
        mine.LowPart=ft.dwLowDateTime;
        mine.HighPart=ft.dwHighDateTime;
    
        hFind=FindFirstFile("S:\\*.pdf", &FindData);
        
        if(hFind==INVALID_HANDLE_VALUE)
        {
            FindClose(hFind);
            return 0;
        }
    
        file.LowPart=FindData.ftCreationTime.dwLowDateTime;
        file.HighPart=FindData.ftCreationTime.dwHighDateTime;
        if(mine.QuadPart>file.QuadPart){
            strcat(delme,"del S:\\");
            strcat(delme,FindData.cFileName);
            system(delme);
        }
        
        while(FindNextFile(hFind, &FindData))
        {
            ErrorCode=GetLastError();
            if(ErrorCode==ERROR_NO_MORE_FILES)
                return 0;
            file.LowPart=FindData.ftCreationTime.dwLowDateTime;
            file.HighPart=FindData.ftCreationTime.dwHighDateTime;
            if(mine.QuadPart>file.QuadPart){
               strcat(delme,"del S:\\");
                strcat(delme,FindData.cFileName);
                system(delme);
            }
        }
        
        FindClose(hFind);
        return 0;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    strcat(delme,"del S:\\");
    This corrupts your stack. When main() wants to return, it jumps into limbo.

    Use std::string.

    Also, don't use system(). Use remove()/unlink() or the appropriate API call - DeleteFile, I think.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    With a decent shell, it's just one line
    Code:
    find . -name "*.pdf" -mtime +730 -exec rm {} \;
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't you have to escape the braces too, in bash?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thanks CornedBee! That worked. And althought I'd love to do it in bash it's going to run on a Windows server.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Can't you load cygwin or MinGW MSYS onto it?
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    28
    Quote Originally Posted by Salem View Post
    With a decent shell, it's just one line
    Code:
    find . -name "*.pdf" -mtime +730 -exec rm {} \;
    Code:
    find . -iname "*.pdf" -mtime +730 -type f  -print0 | xargs -0 rm -f
    exec is slower than xargs, since it forks a process for every single matching file.
    iname is case-insensitive, thus matching *.PDF etc as well.
    Besides that, find has a -delete action.

    CornedBee:
    You don't have to, but you should wrap the {} with single-quotes to ensure
    that they're not interpreted as belonging to a shell script.
    Last edited by pjeremy; 05-16-2007 at 07:47 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    pjeremy, does your method work for spaces in the file name?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    28
    Yes, it may contain single/double quotes, spaces and newlines because of -print0 and -0.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    So -print0 makes them separated by NUL, and -0 makes xargs split on the NUL?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    28
    Yes.

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. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. How to Delete files
    By wyvern in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2005, 12:42 PM
  5. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 10:55 PM

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