//************************************** // Name: Delete File // Description:A simple file to show how to delete any files. I use DeleteFile which erase files without using the trash, use SHFILE_OPERATION for that. You can use NtDeleteFiles to erase any files even locked files. // By: Philippe RIO // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //************************************** File : Delete.c ----------------------------- #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <shlwapi.h> #include <shlobj.h> #include <Stringapiset.h> HINSTANCE hInstance ; char szPgmDirectory[MAX_PATH + 4] ; char szPgmFileName[MAX_PATH + 4] ; char szFinished[] = "Finished" ; int iNbFiles, iNbDirs ; int Error(LPSTR __lpszText) { MessageBox(NULL,__lpszText,"Error",MB_OK|MB_ICONEXCLAMATION) ; return (0) ; } int MainDelete(LPSTR __lpszDirectory) { HANDLE _hFind ; LPSTR _lpszPath ; register LPSTR _lpszFile ; register LPSTR _lpszFoundFile ; char _szFile[MAX_PATH + 4] ; char _szPath[MAX_PATH + 4] ; WIN32_FIND_DATA _Wf, *_lpWf ; /* if(PathIsSystemFolder(__lpszDirectory,FILE_ATTRIBUTE_SYSTEM)) return (Error("I refuse to delete a system Folder!")) ; */ GetWindowsDirectory(_szPath,sizeof(_szPath)) ; if(lstrcmp(_szPath,__lpszDirectory) == 0) return (Error("I refuse to delete the \"Windows\" Folder!")) ; GetSystemDirectory(_szPath,sizeof(_szPath)) ; if(lstrcmp(_szPath,__lpszDirectory) == 0) return (Error("I refuse to delete the \"Windows' System\" Folder!")) ; _lpWf = (LPWIN32_FIND_DATA) memset(&_Wf,0,sizeof(WIN32_FIND_DATA)) ; _lpszFile = (LPSTR) memset(_szFile,0,sizeof(_szFile)) ; _lpszPath = (LPSTR) memset(_szPath,0,sizeof(_szPath)) ; _hFind = NULL ; lstrcat(__lpszDirectory,"\\") ; lstrcat(lstrcpy(_szPath,__lpszDirectory),"*.*") ; _lpszFoundFile = _lpWf->cFileName ; _hFind = FindFirstFile(_lpszPath,&_Wf) ; if(_hFind == INVALID_HANDLE_VALUE) return (0) ; // Nothing to delete do { if(*_lpszFoundFile != '.') { lstrcat(lstrcpy(_lpszFile,__lpszDirectory),_lpszFoundFile) ; if(_lpWf->dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { MainDelete(_lpszFile) ; if(RemoveDirectory(_lpszFile)) iNbDirs++ ; } else { SetFileAttributes(_lpszFile,FILE_ATTRIBUTE_NORMAL) ; if(DeleteFile(_lpszFile)) iNbFiles++ ; } } } while(FindNextFile(_hFind,_lpWf)) ; FindClose(_hFind) ; if(RemoveDirectory(__lpszDirectory)) iNbDirs++ ; return (iNbFiles) ; } int PASCAL WinMain(HINSTANCE __hInstance, HINSTANCE __hPrevInstance, LPSTR __lpszCmdLine, int __nCmdShow) { HANDLE _hThread ; DWORD _dwThreadId ; LPSTR _lpszTmp, _lpszDirectory ; char _szDirectory[MAX_PATH] ; char _szTmp[1024] ; _lpszTmp = _szTmp ; _lpszDirectory = _szDirectory ; hInstance = __hInstance; GetModuleFileName(hInstance,szPgmFileName,MAX_PATH) ; lstrcpy(szPgmDirectory,szPgmFileName) ; *(PathFindFileName(szPgmDirectory)) = '\0' ; PathRemoveBackslash(szPgmDirectory) ; GetModuleFileName(__hInstance,szPgmFileName,MAX_PATH) ; lstrcpy(szPgmDirectory,szPgmFileName) ; *(PathFindFileName(szPgmDirectory)) = '\0' ; PathRemoveBackslash(szPgmDirectory) ; iNbFiles = 0 ; GetCurrentDirectory(MAX_PATH,_lpszDirectory) ; if(PathIsNetworkPath(_lpszDirectory)) return (Error("I don't want to delete anything on a network!")) ; if(PathIsRoot(_lpszDirectory)) return (Error("I refuse to delete anything starting from a ROOT!")) ; if(PathIsDirectory(_lpszDirectory)) { iNbFiles = 0 ; iNbDirs = 0 ; _hThread = CreateThread(NULL,1024 * 1024 * 4,(LPTHREAD_START_ROUTINE) MainDelete,_lpszDirectory,CREATE_SUSPENDED,&_dwThreadId) ; SetThreadPriority(_hThread,THREAD_MODE_BACKGROUND_BEGIN) ; ResumeThread(_hThread) ; WaitForSingleObject(_hThread,INFINITE) ; SetThreadPriority(_hThread,THREAD_MODE_BACKGROUND_END) ; CloseHandle(_hThread) ; } /* FOLDERID_History {D9DC8A3B-B784-432E-A781-5A1130A75963} CSIDL_HISTORY FOLDERID_InternetCache {352481E8-33BE-4251-BA85-6007CAEDCF9D} CSIDL_INTERNET_CACHE FOLDERID_CDBurning {9E52AB10-F80D-49DF-ACB8-4330F5687855} CSIDL_CDBURN_AREA FOLDERID_Cookies {2B0F765D-C0E9-4171-908E-08A611B84FF6} CSIDL_COOKIES FOLDERID_PrintHood {9274BD8D-CFD1-41C3-B35E-B13F55A758F4} CSIDL_PRINTHOOD FOLDERID_Recent {AE50C081-EBD2-438A-8655-8A092E34987A} CSIDL_RECENT SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_HISTORY,TRUE) ; SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_INTERNET_CACHE,TRUE) ; SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_CDBURN_AREA,TRUE) ; SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_COOKIES,TRUE) ; SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_PRINTHOOD,TRUE) ; SHGetSpecialFolderPath(NULL,_lpszDirectory,CSIDL_RECENT,TRUE) ; */ wsprintf(_szTmp,"Number of files deleted : %lu\nNumber of directories deleted : %lu",iNbFiles,iNbDirs) ; MessageBox(NULL,_szTmp,"Delete Files",MB_OK|MB_ICONEXCLAMATION) ; return (0) ; } ---------------------------------- Libraries used : kernel32.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib advapi32.lib delayimp64.lib shlwapi.lib Shell32.lib ---------------------