Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to figure out how to subtract 20 seconds from this time, as well as the placement. I am deleteing files that are 20 seconds or less. Thank you. Help is apprecitiated!

C++
using namespace std; 
typedef vector tFoundFilesVector; 
std::wstring LastWriteTime;   
//int getFileList(wstring filespec, tFoundFilesVector &foundFiles) //uni
int getFileList(const char * filespec, tFoundFilesVector &foundFiles) //ansi
{ 
    WIN32_FIND_DATA findData; 
    HANDLE h; 
    int validResult=true; 
 
    int numFoundFiles = 0; 
    //h = FindFirstFile(filespec.c_str(), &findData); //uni 
	h = FindFirstFile((LPCSTR)filespec, &findData); //ansi 
    if (h == INVALID_HANDLE_VALUE) 
        return 0; 
 
    while (validResult) 
    { 
        numFoundFiles++; 
        foundFiles.push_back(findData); 
        validResult = FindNextFile(h, &findData); 
    } 
    return numFoundFiles; 
} 
 
void showFileAge(tFoundFilesVector &fileList) 
{ 
    unsigned _int64 fileTime, curTime, age; 
    tFoundFilesVector::iterator iter; 
    FILETIME ftNow; 
    CoFileTimeNow(&ftNow); 
          curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime; 
 
          for (iter=fileList.begin(); iterftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		if (age <= (_int64)age/10000000UL-20)
		{
			remove("*.*");
		}
		else
		{
			return;
		}

 
        wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
    } 
} 
 
int main() 
{ 
    string fileSpec = "*.*"; 
    tFoundFilesVector foundFiles; 
    tFoundFilesVector::iterator iter; 
 
    int foundCount = 0; 
 
    getFileList("c:\\Mapper\\*.txt", foundFiles); 
    getFileList("c:\\Mapper\\*.jpg", foundFiles);
	     foundCount = foundFiles.size(); 
    if (foundCount) 
    { 
        wcout << "Found "<</pre>
Posted

1 solution

In general I use this function,
C++
char *DateDiffer(char *Date, int DayToSub)
{
	struct tm time,*tmt;
	time_t mtime;
	int year,month,date;
	char *buffer;
	buffer=(char *)malloc(sizeof(char)*10);
	if(Date!=NULL)
	{
		sscanf(Date,"%d-%d-%d",&year,&month,&date);
	}	

	memset((char*)(&time),0,sizeof(struct tm));
	time.tm_year=year-1900;
	time.tm_mon=month-1;
	time.tm_mday=date-DayToSub;
	mtime=mktime(&time);
	tmt=localtime(&mtime);
	sprintf(buffer,"%04d-%02d-%02d\n",(tmt->tm_year+1900),(tmt->tm_mon+1),tmt->tm_mday);
	return buffer;
}
 
Share this answer
 
Comments
Member 7766180 3-Oct-11 3:39am    
Thank You.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900