Click here to Skip to main content
15,904,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know your probably as sick of this code as I am, but I was told by Albert Holguin that this can be done. And he's one of the brightest guys on here. I've gotten this far. I just need to finish it. I'm trying to delete any file that is less than a minute old. I think I have the first part about the systemtofile, now from what I read I need to ComparefileTime. My first problem is that I am not sure where to place it, any help is appreciated. Thank you.

C++
struct file_data 
{ 
    std::wstring sLastAccessTime; 
    __int64 nFileSize      ; 
};

int GetFileList(const wchar_t *searchkey, std::map &map) 
{ 
    WIN32_FIND_DATA fd; 
    HANDLE h = FindFirstFile(searchkey,&fd); 
    if(h == INVALID_HANDLE_VALUE) 
    { 
        return 0; // no files found 
    } 
    while(1) 
    { 
       	wchar_t buf[128]; 
        SYSTEMTIME st;
		FILETIME ftNow;
		LARGE_INTEGER li;    
		GetSystemTime(&st);
		SystemTimeToFileTime(&st, &ftNow);
    	li.LowPart = ftNow.dwLowDateTime;
		li.HighPart = ftNow.dwHighDateTime;
		
		GetSystemTimeAsFileTime(&ftNow);
		auto ftAs64 = ftNow.dwLowDateTime + ((unsigned __int64)ftNow.dwHighDateTime << 32) - 6000000000UL;
		FILETIME ftOneMinuteAgo = { (DWORD)ftAs64, (DWORD)(ftAs64 >> 32) };
		FileTimeToSystemTime(&ftNow, &st); 
        wsprintf(buf, L"%d-%02d-%02d",st.wYear, st.wMonth, st.wDay); 
		
        file_data filedata; 
        filedata.sLastAccessTime= buf; 
        filedata.nFileSize      = (((__int64)fd.nFileSizeHigh) << 32) + fd.nFileSizeLow; 
 
        map[fd.cFileName]= filedata; 
		//////////////////////////////////////////////////////
        if (FindNextFile(h, &fd) == FALSE)
			GetFileTime(&ftOneMinuteAgo);
		if (-1 == CompareFileTime (&ftMostRecent, &ft))
		{
		// ... do something ... //
		}
		//////////////////////////////////////////////////////
            break; 
    } 
     FindClose(h);
	return map.size(); 
} 
 
int main() 
{ 
	std::map map; 
    GetFileList(L"C:\\Mapper\\*.txt", map);
	GetFileList(L"C:\\Mapper\\*.pdf", map);
	GetFileList(L"C:\\Mapper\\*.jpg", map);
	{
		if (map.size() > 0) 
		{ 
		printf("\n   Delete: %i   \n", map.size());
		} 
		else 
		{ 
		printf ("%s \n", "Nothing");
		}
	system("pause");
    return 0;
}
}
Posted

Place it where it is needed! You are the one with the requirements so you must decide where you need to make this test. Do you want to check it at the time you find a file, at the time you need to decide whether to delete it or at some other point in your program?

Personally, I would use this test during the part of the code where I was looking for candidates for deletion. If the file matches the search criteria and is less than 1 minute old, add it to a list which can then be processed by the delete function.
 
Share this answer
 
Comments
Member 7766180 1-Oct-11 4:19am    
Richard I would like to add it at the time the file is found then add it to the list. So am I right where it is or do I need to move it, and if I do.Move it to where? Thank you.
Richard MacCutchan 1-Oct-11 4:36am    
More or less, yes, however:
1. Move the code for getting the time outside of your while loop; there is no point in recalculating this every time round the loop.
2. Check the time against the current file at the beginning of the loop, if it matches add it to the list.
3. Call FindNextFile() to get the next candidate, and if the call fails exit the loop.

your code just needs a little tidying up before it's ready. Try and think logically about each step in your routine and why it should be in the particular sequence that it is.
Member 7766180 1-Oct-11 5:05am    
Thank you Richard, I'll tackle it in the morning 5:00 am here. I want to be awake for this, I appreciate your help!
Richard MacCutchan 1-Oct-11 5:14am    
I guess you're on the Eastern seaboard then. It's just after 10 here in the UK, the sky is blue and the sun is shining. Not bad for October 1st.
Further to the discussion above, I can't help but wonder why use a map for this task? Since FindFirstFile and FindNextFile give you all the data you want (and more) in a single chunk, I'd just store that using a vector.

Something to toy with:

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>

using namespace std;

typedef vector<WIN32_FIND_DATA> tFoundFilesVector;

int getFileList(string filespec, tFoundFilesVector &foundFiles)
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    bool validResult=true;
    int numFoundFiles = 0;

    h = FindFirstFile(filespec.c_str(), &findData);

    if (h == INVALID_HANDLE_VALUE)
        return 0;

    while (validResult)
    {
        numFoundFiles++;
        foundFiles.push_back(findData);
        validResult = FindNextFile(h, &findData);
    }
    return numFoundFiles;
}

void showFileAge(tFoundFilesVector &fileList)
{
    INT64 fileTime, curTime, age;
    tFoundFilesVector::iterator iter;

    CoFileTimeNow(&ftNow);
    curTime = ((unsigned INT64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime;

    for (iter=fileList.begin(); iter<fileList.end(); iter++)
    {
        fileTime = ((unsigned INT64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;

        age = curTime - fileTime;

        cout << "FILE: '" << iter->cFileName << "', AGE: " << (INT64)age/10000000UL << " seconds" << endl;
    }
}

int main()
{
    string fileSpec = "*.*";
    tFoundFilesVector foundFiles;
    tFoundFilesVector::iterator iter;

    int foundCount = 0;

    getFileList("*.c??", foundFiles);
    getFileList("*.h", foundFiles);

    foundCount = foundFiles.size();
    if (foundCount)
    {
        cout << "Found "<<foundCount<<" matching files.\n";
        showFileAge(foundFiles);
    }
    return 0;
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Oct-11 8:31am    
Don't complicate things, it's taken us long enough to get him this far!
enhzflep 1-Oct-11 8:34am    
Better?
Richard MacCutchan 1-Oct-11 12:22pm    
Perfect, looks like you've done this before!

BTW you may wish to look at OP's other threads to see what's been going on and how many other CPians have lost patience and walked away.
enhzflep 1-Oct-11 12:35pm    
Maybe :wink:

Yeah, I'd been watching and face-palming at many of the OPs other threads over the past week or so. Seem to remember that even SA or C'OToole washed his hands of him.
My aim was to separate the different steps into something that had a semblance of clear and logical thinking. My aim wasn't to present a finished solution, but rather to put the code into clear blocks of functionality. Meh, you can't lose 'em all...
Richard MacCutchan 1-Oct-11 12:39pm    
My aim was to separate the different steps into something that had a semblance of clear and logical thinking.
Yes, you and the rest of us.

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