DeleteFile The DeleteFile function deletes an existing file. BOOL DeleteFile( LPCTSTR lpFileName // pointer to name of file to delete );
Parameters lpFileName Pointer to a null-terminated string that specifies the file to be deleted. Return Values If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res If an application attempts to delete a file that does not exist, the DeleteFile function fails. Windows 95: The DeleteFile function deletes a file even if it is open for normal I/O or as a memory-mapped file. To prevent loss of data, close files before attempting to delete them.Windows NT: The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.To close an open file, use the CloseHandle function. Windows CE: If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT.See Also File I/O Overview, File Functions, CloseHandle, CreateFile
The DeleteFile function deletes an existing file. BOOL DeleteFile(
LPCTSTR lpFileName // pointer to name of file to delete
);
Parameters
lpFileName
Pointer to a null-terminated string that specifies the file to be deleted.
Return Values
If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. Res
If an application attempts to delete a file that does not exist, the DeleteFile function fails. Windows 95: The DeleteFile function deletes a file even if it is open for normal I/O or as a memory-mapped file. To prevent loss of data, close files before attempting to delete them.Windows NT: The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.To close an open file, use the CloseHandle function. Windows CE: If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.See Also
File I/O Overview, File Functions, CloseHandle, CreateFile
BOOL DeleteLogFile(CString strPath,CString strFileExt)
{ WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(strPath+strFileExt, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
TRACE ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
} else {
TRACE ("The first file found is %s\n", FindFileData.cFileName);
DeleteFile(strPath + FindFileData.cFileName);
while(FindNextFile(hFind,&FindFileData))
{
TRACE ("The next file found is %s\n", FindFileData.cFileName);
DeleteFile(strPath + FindFileData.cFileName);
}
}
FindClose(hFind);
return TRUE;
}
=============================================================================
FILE MFC :
BOOL DeleteLogFile(CString strPath, CString strFileExt)
{
CFileFind cFind;
BOOL bIsFinded = cFind.FindFile(strPath+strFileExt);
while(bIsFinded)
{
bIsFinded = cFind.FindNextFile();
if( !cFind.IsDots() && !cFind.IsDirectory())
{
DeleteFile(strPath + cFind.GetFileName());
}
}
cFind.Close();
return TRUE;
}
======================================================
调用示例:
DeleteLogFile(_T("C:\\a\\"), _T("*.log"));