C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-04-2007, 01:31 PM   #1
Slime
 
Dragoon_42's Avatar
 
Join Date: Feb 2003
Location: Seattle
Posts: 86
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;
}
Dragoon_42 is offline   Reply With Quote
Old 05-04-2007, 01:47 PM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
Quote:
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
CornedBee is offline   Reply With Quote
Old 05-04-2007, 02:47 PM   #3
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 05-04-2007, 03:26 PM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 05-15-2007, 08:21 AM   #5
Slime
 
Dragoon_42's Avatar
 
Join Date: Feb 2003
Location: Seattle
Posts: 86
Thanks CornedBee! That worked. And althought I'd love to do it in bash it's going to run on a Windows server.
Dragoon_42 is offline   Reply With Quote
Old 05-15-2007, 09:32 AM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 05-16-2007, 07:40 PM   #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.
pjeremy is offline   Reply With Quote
Old 05-17-2007, 01:45 AM   #8
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 05-17-2007, 04:32 AM   #9
Registered User
 
Join Date: May 2006
Posts: 28
Yes, it may contain single/double quotes, spaces and newlines because of -print0 and -0.
pjeremy is offline   Reply With Quote
Old 05-17-2007, 03:00 PM   #10
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 05-17-2007, 04:13 PM   #11
Registered User
 
Join Date: May 2006
Posts: 28
Yes.
pjeremy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Program Deployment and DLL/OCX Files? dfghjk C++ Programming 5 06-16-2008 02:47 AM
added start menu crashes game avgprogamerjoe Game Programming 6 08-29-2007 01:30 PM
*.cpp and *.h files understanding ElastoManiac C++ Programming 4 06-11-2006 04:45 AM
How to Delete files wyvern C++ Programming 6 12-01-2005 01:42 PM
why delete[] and delete? dude543 C++ Programming 4 11-26-2005 11:55 PM


All times are GMT -6. The time now is 01:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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