Results 1 to 2 of 2

Thread: deleting a file

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default deleting a file

    I'm trying to write an application that checks all files within a specified directory, and if the files last modified date is older than the current date, I want to delete the file.

    I'm running into a problem with getting the difference between the time to determine how old the file is.

    I tried difftime() and checking against > (60 * 60 * 24 seconds in a day), strcmp and I can't think of any other working way

    Here is what I'm working with currently.

    Code:
    #include "stdafx.h"
    #include <string>
    #include <vector>
    #include <windows.h>
    #include <time.h>
    #include <map>
    #include <iostream>
    #include <ATLComTime.h>
    
    
    struct file_data
    {
        std::wstring sLastAccessTime;
        __int64 nFileSize;
    };
    
    
    int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map)
    { 
        WIN32_FIND_DATA fd;
        HANDLE h = FindFirstFile(searchkey,&fd);
    
    
        if(h == INVALID_HANDLE_VALUE)
        {
            return 0;
        }
    
    
        while(1)
        {
    		//Get current date & time info
    		time_t currentDate;
    		//time(&currentDate);
    
    
    		std::wcout << "Current Time: " << time(&currentDate) << std::endl;
    
    
    		//-------------------------------------------------
    		//File Time Info
    		wchar_t bufA[128];
    		FILETIME ft = fd.ftLastWriteTime;
    		//SYSTEMTIME sysTime;
    		struct tm sysTime;
            FileTimeToSystemTime(&ft, (LPSYSTEMTIME)&sysTime);
    		wsprintf(bufA, L"%02d/%02d/%02d\n", sysTime.tm_mon, sysTime.tm_mday, sysTime.tm_year);
    
    
    		std::wcout << "File Time: " << bufA << "\n" << std::endl;
    		
    		//seconds = difftime(mktime(ptm),sysTime);
    		double seconds = difftime(currentDate, mktime(&sysTime));
    		std::wcout << "Seconds: " << seconds << std::endl;
    
    
            file_data filedata;
            filedata.sLastAccessTime = bufA;
            filedata.nFileSize = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
     
            map[fd.cFileName]= filedata; 
     
            if(FindNextFile(h, &fd) == FALSE) 
                break; 
        } 
        return map.size(); 
    } 
     
    int main() 
    { 
        std::map<std::wstring, file_data> map; 
    
    
    	GetFileList(L"C:\\temp\\*.*", map);
    
    
        for(std::map<std::wstring, file_data>::const_iterator it = map.begin(); it != map.end(); ++it) 
        { 
    		std::wcout << it->first.c_str() << std::endl;
        }
    
    
    	Sleep(10000);
    	return 0;
    }
    Any ideas?

  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Figured it out. Here is the code if anyone is interested. Not sure if there is a better way, but this does what I need it to do so I'll stick with it.

    Code:
    #include "stdafx.h"
    #include <string>
    #include <windows.h>
    #include <time.h>
    #include <map>
    #include <iostream>
    
    
    #define path "C:\\temp\\"
    
    
    struct file_data
    {
        std::wstring sLastAccessTime;
        __int64 nFileSize;
    };
    
    
    _int64 Delta(const SYSTEMTIME st1, const SYSTEMTIME st2)
    {
        union timeunion {
            FILETIME fileTime;
            ULARGE_INTEGER ul;
        } ;
        
        timeunion ft1;
        timeunion ft2;
    
    
        SystemTimeToFileTime(&st1, &ft1.fileTime);
        SystemTimeToFileTime(&st2, &ft2.fileTime);
    
    
        return ft2.ul.QuadPart - ft1.ul.QuadPart;
    }
    
    
    SYSTEMTIME convertTM2SYSTEMTIME(struct tm *tmTime, SYSTEMTIME *systemTime)
    {
    	systemTime->wDay			= tmTime->tm_mday;
    	systemTime->wDayOfWeek		= tmTime->tm_wday;
    	systemTime->wMonth			= tmTime->tm_mon + 1;
    	systemTime->wYear			= tmTime->tm_year + 1900;
    	systemTime->wHour			= tmTime->tm_hour;
    	systemTime->wMinute			= tmTime->tm_min;
    	systemTime->wSecond			= tmTime->tm_sec;
    	systemTime->wMilliseconds	= 0;
    	return *systemTime;
    }
    
    
    int GetFileList(const wchar_t *searchkey, std::map<std::wstring, file_data> &map)
    { 
        WIN32_FIND_DATA fd;
        HANDLE h = FindFirstFile(searchkey,&fd);
    	wchar_t bufA[128], szBuff[128];
    
    
        if(h == INVALID_HANDLE_VALUE)
        {
            return 0;
        }
    
    
    	while(1)
        {
    		char szBuffer[512];
    
    
    		//Get current date & time info
    		unsigned int value;
    		time_t rawtime;
    		struct tm * ptm;
    		time(&rawtime);
    		ptm = gmtime(&rawtime);
    		SYSTEMTIME mySysTime;
    		convertTM2SYSTEMTIME(ptm ,&mySysTime);
    
    
    		//File Time Info
            FILETIME ft = fd.ftLastWriteTime;
            SYSTEMTIME sysTime;
            FileTimeToSystemTime(&ft, &sysTime);
    
    
    		//Difference in times
    		_int64 i = Delta(mySysTime, sysTime);
    
    
    		//Convert to positive integer
    		i = ((i / 10000000) * -1);
    	
            file_data filedata;
            filedata.sLastAccessTime = bufA;
            filedata.nFileSize = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
     
            map[fd.cFileName]= filedata;
    
    
    		std::wcout << fd.cFileName << std::endl;
    		//Check if the date is older than 1 day
    		if(i < 86400)
    		{
    			std::wcout << i << " is less than 86400" << std::endl;
    		}
    		else if(i > 86400)
    		{
    			std::wcout << i << " is greater than 86400" << std::endl;
    			sprintf(szBuffer, "del %s%s /F /Q", path, &map);
    			system(szBuffer);
    		}
    
    
            if(FindNextFile(h, &fd) == FALSE) 
                break; 
        } 
        return map.size(); 
    } 
     
    int main() 
    { 
        std::map<std::wstring, file_data> map; 
    
    
    	GetFileList(L"C:\\temp\\*.*", map);
    
    
        for(std::map<std::wstring, file_data>::const_iterator it = map.begin(); it != map.end(); ++it) 
        { 
    		//std::wcout << it->first.c_str() << std::endl;
        }
    
    
    	Sleep(10000);
    	return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •