Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have code that deletes a file based on time in seconds. The problem is that I have to adjust that time depending on file size. I found the code to get a files size, however I am having problems about incorporating it into my current code. I want to do something like: If filename <= 1 KB then delete file that is <=10 seconds. If filename >1KB && <=3KB then delete file that is <=20 seconds.....

C++
// Norway.cpp : Defines the entry point for the console application.
//
#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::string 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); 
    } 
    FindClose(h);
	return numFoundFiles; 
} 

ULONGLONG GetFileSizeEx( CString strPath )
{  
   WIN32_FIND_DATA FindData = { 0 };
   //gets a file search handle
   HANDLE hFirstFile = FindFirstFile( strPath, &FindData );  
  
   //if the handle is valid
   if( hFirstFile != INVALID_HANDLE_VALUE )
   {
      //closes the file search handle
      FindClose( hFirstFile );
     
      ULONGLONG FileSize = FindData.nFileSizeHigh;
      FileSize <<= sizeof( FindData.nFileSizeHigh ) * 8; // Push by count of bits
      FileSize |= FindData.nFileSizeLow;
     
      return FileSize;
 }
  
   return 0; // File not found
}



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();>
    { 
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		if (age <= (_int64)100000000UL)
		{
			rename(string("c:\\Mapper\\").append(string(iter->cFileName)).c_str(), "c:\\FAIL\\myfile.txt");
			
			wcout << " Delete: '" <<endl;
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			remove ("c:\\FAIL\\myfile.txt");
			//remove(string("c:\\mapper\\").append(string(iter->cFileName)).c_str());
			MessageBox(HWND_DESKTOP,"Text here","No Text here",MB_OK);

		}
		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; 
}</win32_find_data></windows.h></vector></string></stdio.h></iostream>
Posted

1 solution

Assuming you meant file size instead of "filename", your question already contains the answer: use a chain of if-else.
 
Share this answer
 
Comments
Member 7766180 12-Oct-11 14:35pm    
Well I need both, file name and file size. Also CString is giving me a problem.

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