I'm using C++ VS 2010 in Win7. I'm trying to delete a zip. and/or txt. file that is less than a minute old. However; heres the rub. I don't know the name or location of the file. Only that I want to delete it by extension and a timestamp of newer than 1 minute. I've been looking everywhere for a solution. I do have a batch file that does this but I would prefer it in C++ in Windows. Any help is appreciated. Thank You.

You can get a list of all the files/folders in a folder by first calling FindFirstFile() then call FindNextFile() until it returns no more files. Both functions returns a structure that contains the timestamp that your program will need to check. The problem is that your program may not be able to detect files that are newer than 1 minute, but you'll just have to run the program to find out.

I need to search all folders on my hard drive and delete the file by type, that is less than a minute old. My current app doesn't do anything with files yet. That is wht I am trying to write. Remember I don't know where the file is or its name. Just the extension and the time. Thank you.

Have this so far. Now how can I find a file that is less then a minute old? Thank you.

#include <string>
#include <vector>
#include <windows.h>
#include <time.h>
#include <map>
 

int GetFileList(const wchar_t *searchkey, std::map<std::wstring,> &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];
     FILETIME ft = fd.ftLastWriteTime;
     SYSTEMTIME sysTime;
     FileTimeToSystemTime(&ft, &sysTime);
     wsprintf(buf, L"%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay);
     map[fd.cFileName] = buf;
        if(FindNextFile(h, &fd) == FALSE)
            break;
    }
    return map.size();
}
 
void main()
{ 
    std::map<std::wstring,> map;
    int count = GetFileList(L"*.*", map);
    // Using const_iterator
    for(std::map<std::wstring,>::const_iterator it = map.begin(); 
      it != map.end(); ++it)
    {
       MessageBoxW(NULL,it->first.c_str(),L"One",MB_OK);
       MessageBoxW(NULL,it->second.c_str(),L"Two",MB_OK);
    }
}

If you have to search the entire hard drive then the program will miss some of them because it will take a lot more than than 1 minute to do the searches.

Look up (e.g. google) low FindFirstFile() and FindNextFile() work and you will find out how you can use them in your program.

Because the above will be too slow for your purposes another option is to let MS-Windows notify your program when files change within a folder. Read this link to see if it will help you.

So you feel that the (FindFirstChangeNotification) would be the fastest and most reliable? If so then I will head in that direction. Thank you.

This works.

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>
 
using namespace std; 
typedef vector<WIN32_FIND_DATA> 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(); iter<fileList.end(); iter++) 
    { 
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		if (age <= (_int64)200000000UL)
		{
			wcout << " Delete: '" <<endl;
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			remove(string("c:\\mapper\\").append(string(iter->cFileName)).c_str()); 			
		}
		else
		{
			//wcout << " Quit: '" <<endl;
			//wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			//return;
		}
    } 
} 
 
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 "<<foundCount<<" matching files.\n"; 
        showFileAge(foundFiles); 
    } 
    system("pause"); 
    return 0; 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.