Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Most recent file in folder... 1

Status
Not open for further replies.

LiquidBinary

Programmer
Jul 27, 2001
148
US
Can someone paste some code that will allow me to find the most recent file in a directory? i.e. The one file with the most recent creation time. I would like to do this with the raw API and not MFC. I know there are some API calls that can accomplish this, but you all know how big the win32 API is. mlg400@blazemail.com
 
look at thread205-117916
In file attributes find the date and sort by it. Ion Filipski
1c.bmp


filipski@excite.com
 
#include <windows.h>

BOOL FindLatestFile(TCHAR* pPath,TCHAR* pLatestFile);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
TCHAR cFile[_MAX_PATH];

FindLatestFile(&quot;c:\\winnt&quot;,cFile);
return 0;
}

//Call with pLatestFile pointing to a buffer
//of at least _MAX_PATH TCHARS
//name of the latest file is in pLatestFile
//if there is one

BOOL FindLatestFile(TCHAR* pPath,TCHAR* pLatestFile)
{
BOOL bReturn = FALSE;
HANDLE hFind;
WIN32_FIND_DATA ffd;
TCHAR* pFile;
int iLen;
TCHAR cWorkingPath[_MAX_PATH];
FILETIME ftLatest = {0,0};

if(pPath && pLatestFile)
{
*pLatestFile = '\0';
lstrcpy(cWorkingPath,pPath);
iLen = lstrlen(cWorkingPath);

if(iLen > 0)
{
switch(cWorkingPath[iLen - 1])
{
case '\\':
case '/':
break;
default:
cWorkingPath[iLen] = '\\';
iLen++;
cWorkingPath[iLen] = '\0';

}
}

pFile = cWorkingPath + iLen;
lstrcat(cWorkingPath,&quot;*.*&quot;);

hFind = FindFirstFile(cWorkingPath,&ffd);

if(hFind != INVALID_HANDLE_VALUE)
{
bReturn = TRUE;

do
{
if((ffd.dwFileAttributes &
FILE_ATTRIBUTE_DIRECTORY) == 0)
{
if(CompareFileTime(&ffd.ftLastWriteTime,
&ftLatest) > 0)
{
ftLatest = ffd.ftLastWriteTime;
lstrcpy(pLatestFile,ffd.cFileName);
}
}
}while(FindNextFile(hFind,&ffd));

FindClose(hFind);
}
}

return bReturn;
}
 
Thanks MemoryLeak, you posted exactly what I needed. Keep up the good work!!! :) mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top