![]() |
| | #1 |
| Slime Join Date: Feb 2003 Location: Seattle
Posts: 86
| 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 | |
| | #2 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| Quote:
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 | |
| | #3 |
| and the hat of Jobseeking 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 {} \;
|
| Salem is offline | |
| | #4 |
| Cat without Hat 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 | |
| | #5 |
| Slime 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 | |
| | #6 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,706
| Can't you load cygwin or MinGW MSYS onto it? |
| Salem is offline | |
| | #7 | |
| Registered User Join Date: May 2006
Posts: 28
| Quote:
Code: find . -iname "*.pdf" -mtime +730 -type f -print0 | xargs -0 rm -f 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 | |
| | #8 |
| Cat without Hat 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 | |
| | #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 | |
| | #10 |
| Cat without Hat 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 | |
| | #11 |
| Registered User Join Date: May 2006
Posts: 28
| Yes. |
| pjeremy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |