Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have an if statement that does whatever if a file is two seconds old or less. This is in filetime.
Updated.

C++
using namespace std;

typedef vector<win32_find_data> tFoundFilesVector; 
std::string LastWriteTime;

int getPassList(const char * filespec, tFoundFilesVector &foundFiles)
{ 
    WIN32_FIND_DATA findData; 
    HANDLE h; 
    int validResult=true; 
 
    int numFoundFiles = 0; 
    h = FindFirstFile((LPCSTR)filespec, &findData); 
    if (h == INVALID_HANDLE_VALUE) 
        return 0; 
 
    while (validResult) 
    { 
        numFoundFiles++; 
        foundFiles.push_back(findData); 
        validResult = FindNextFile(h, &findData); 
    } 
    FindClose(h);
	return numFoundFiles; 
} 


void PASS(tFoundFilesVector &fileList) 
{//Pass Open 
    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();>
		{//For Open 
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		
		if (age <= (_int64)20000000UL)// 2 Seconds
			{//if Age Open
			rename(string("c:\\Users\\DS\\Downloads\\").append(string(iter->cFileName)).c_str(), string("c:\\PASS\\").append(string(iter->cFileName)).c_str());
			}//If Age Close
		}//For Close 
}//Pass Close </win32_find_data>




However; it's not doing whatever if the file is 0 Seconds old. Do I need to change the if statement? Any help is appreciated. Thank you.
Posted
Updated 5-Nov-11 17:43pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Nov-11 20:48pm    
When you ask things like this one, always show all relevant declaration. Where is you declaration of age?
--SA
Chuck O'Toole 5-Nov-11 23:12pm    
Like SA said, we cannot know what's wrong unless you show how you both declared age and how your computed it.

OK, as reposted, I don't see anything obviously wrong. However, if you want an answer to the question from one of your replies:
Quote:
As written does it run the code based on a file that is 2 seconds old or less, including 0 seconds?
then by all means use the debugger to set breakpoints on various statements and find out yourself. Nobody here is going to take all that code and run it on their own systems just to see if it works. The best person to determine if it works is YOU.

PS, mathematically 0 seconds is <= 2 seconds so there's nothing you should change.

(edited)

Oh, one thing, remove the "UL" from this (_int64)20000000UL. (__int64)20000000 is sufficient.

Also, why do you have a single "_" in the int64 declaration / typecast? I thought it was __int64 (two underscores)
 
Share this answer
 
v2
Comments
Member 7766180 6-Nov-11 0:39am    
This is what I wanted to know!

PS, mathematically 0 seconds is <= 2 seconds so there's nothing you should change.

I will check the typecast. Thank you.
The value "20000000UL" cannot be exactly FILETIME which is really a 64-bit value, but expressed as a structure of two 32-bit values. If your statement, you don't need a type case; you need to use you numeric value representing 2 seconds. What you really need to convert is FILETIME to __int64. Try:
C++
__int64 age = (__int64(filetime.dwHighDateTime)<<32) | __int64(filetime.dwLowDateTime);


—SA
 
Share this answer
 
v2
Comments
Member 7766180 5-Nov-11 23:11pm    
This is what I have so far...

void PASS(tFoundFilesVector &fileList)
{//Pass Open
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++)=""
="" {="" for="" open="" filetime="((_int64)iter-">ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;

age = curTime - fileTime;

if (age <= (_int64)20000000UL)// 2 Seconds
Chuck O'Toole 5-Nov-11 23:25pm    
Is that really your code? This sequence makes no sense and I'm hoping it's a paste error rather than your actual code. for (iter=fileList.begin(); iterftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;. Nothing in the code you pasted compues the fileTime variable. The debugger should be able to show you that.
Chuck O'Toole 5-Nov-11 23:27pm    
PS, you should update your question with this kind of information rather than stick it as a reply. It's easier to read that way and future readers will have all the relevant info without having to read all this stuff.
Chuck O'Toole 5-Nov-11 23:18pm    
SA - You really don't need all that shifting around, you can simply typecast a FILETIME to be an __int64, the bits line up properly. I do that all the time.
Member 7766180 5-Nov-11 23:45pm    
Ok, It's all in the question now. As written does it run the code based on a file that is 2 seconds old or less, including 0 seconds?

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