Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to search for a file created somewhere on my hard drive within the last 5 seconds. I have this so far and have been googling, but still at loss. Any help is appreciated. Thnak you.
C++
ifstream ifile(*.*);
if (ifile) {
  	int myfile
	myFile = 1;
else
        myfile = 0;
}


This is in windows. Just got tougher. It seems filetime and systemtime are two different things and systemtime needs to be converted to filetime!
Posted
Updated 24-Sep-11 8:12am
v2
Comments
Philippe Mori 24-Sep-11 17:05pm    
The sample code does not make any sense and won't even compile:
(1) *.* is not quoted.
(2) ifstream does not works on multiple files
(3) Variable myFile is declared locally in the if part and also used in the else part.
(4) There are missing } and { around else keyword.
Member 7766180 24-Sep-11 17:36pm    
Thanks, I guess I was too sloppy! :)

This example code[^] should be a good starting point.
It also shows how to use FILETIME and SYSTEMTIME.
 
Share this answer
 
Comments
Member 7766180 24-Sep-11 15:10pm    
Thank you. I will try it now and get back to you.
Member 7766180 24-Sep-11 15:33pm    
Ok, I'm trying, but I get 4 Errors. Here is the code.
#include <string>
#include <vector>
#include <windows.h>
#include <time.h>
#include <map>

int GetFileList(const char *searchkey, std::map<std::string, std::string=""> &map)
{
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(searchkey,&fd);
if(h == INVALID_HANDLE_VALUE)
{
return 0; // no files found
}
while(1)
{
char buf[128];
FILETIME ft = fd.ftLastWriteTime;
SYSTEMTIME sysTime;
FileTimeToSystemTime(&ft, &sysTime);
sprintf(buf, "%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::string, std::string=""> map;
int count = GetFileList("c:\\kathir\\*.txt", map);
// Using const_iterator
for(std::map<std::string, std::string="">::const_iterator it = map.begin();
it != map.end(); ++it)
{
// MessageBox(it->first.c_str());
// MessageBox(it->second.c_str());
}
}

THE ERRORS
1 error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

2 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'WCHAR [260]'

3 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"

4 IntelliSense: no operator "[]" matches these operands
Philippe Mori 24-Sep-11 17:00pm    
Uses Unicode (wide) string everywhere (wchar_t, std::wstring, wsprintf and litteral string prefixed with L among other).

Or compile with narrow characters (adjust properties settings for your project).
Member 7766180 24-Sep-11 17:38pm    
Thank you. I appreciate your input, but at this period in time I am lost. Could you clarify a bit. Thanks.
Philippe Mori 24-Sep-11 20:05pm    
Most application are compiled for Unicode (16 bit, wide) but the code that was copied from the web is done for Ansi (8 bit, narrow). When compiling a Windows application, default settings are to compile for Unicode.

Most Win32 API functions have a A and a W variant of the function. This is why the compiler display FindFirstFileW.

Generally you want to compile for Unicode as it support much more distinct characters and all recent Windows OS support them.

Thus, you would need to adapt the code a bit with suggestions I gave.

A few examples to make it more clear:
const char *searchkey --> const wchar_t *searchkey
std::map<std::string,std::string> --> std::map<std::wstring,std::wstring>
char buf[128]; --> wchar_t buf[128];
sprintf --> wsprintf
"%d-%02d-%02d" --> L"%d-%02d-%02d"

and so on...

As a side note, there are macros like _TCHAR, _T that can be used if you want your code to compile for both Unicode and Ansi... Windows Me is the last Windows OS to not fully support unicode out-of-the-box).
Came up with this, However some input would be nice! Anything that I have to change or make better please feel free. Thank you.

C++
time_t currentTime;

class="highlight">


       struct tm * ptm= localtime(¤tTime);
             int tm_sec;      // 0 to 59
             int tm_min;      // 0 to 59
             int tm_hour;     // 0 to 23
             int tm_mday;     // 1 to 31
             int tm_mon;      // 0 to 11
             int tm_year;     //
             int tm_wday;     // 0 to 6
             int tm_yday;     // 0 to 365
             int tm_isdst;    // 0 to 1
             char *tm_zone;   // ^__strong class="highlight">time zone
             int tm_gmtoff;

       char tod;

       tod = tm_sec;

       if ( tod <= time_t - 5 tm_sec  )
          remove ( "^__strong class="highlight">*.txt to delete" );
 
Share this answer
 

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