VB icon

My Cleaner

Email
Submitted on: 10/27/2019 8:49:25 AM
By: Philippe RIO  
Level: Intermediate
User Rating: Unrated
Compatibility: C
Views: 170
author picture
 
     It is a test program to clean your pc running under Windows.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: My Cleaner
// Description:It is a test program to clean your pc running under Windows.
// By: Philippe RIO
//**************************************

#define WINVER 0x0600
#define WIN32_LEAN_AND_MEAN
#define	INITGUID
#include <windows.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <Stringapiset.h>
#include <initguid.h>
#include <KnownFolders.h>
#include <shellapi.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 PhRCleaner(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)
			{
				PhRCleaner(_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) ;
}
LPSTR GetSpecialFolder(REFKNOWNFOLDERID rfid,LPSTR __lpszResult)
{
	LPWSTR	_lpwszPath = NULL ;
	HRESULT	_hr = SHGetKnownFolderPath(rfid,KF_FLAG_CREATE,NULL,&_lpwszPath) ;
	if(SUCCEEDED(_hr))
	{
		WideCharToMultiByte(CP_UTF8,0,_lpwszPath,lstrlenW(_lpwszPath),__lpszResult,MAX_PATH,NULL,NULL) ;
		return (__lpszResult) ;
	}
	return (NULL) ;
}
void MainDelete(LPSTR __lpszDirectory)
{
	HANDLE			_hThread ;
	DWORD			_dwThreadId ;
	_hThread = CreateThread(NULL,1024 * 1024 * 4,(LPTHREAD_START_ROUTINE) PhRCleaner,__lpszDirectory,CREATE_SUSPENDED,&_dwThreadId) ;
	SetThreadPriority(_hThread,THREAD_MODE_BACKGROUND_BEGIN) ;
	ResumeThread(_hThread) ;
	WaitForSingleObject(_hThread,INFINITE) ;
	SetThreadPriority(_hThread,THREAD_MODE_BACKGROUND_END) ;
	CloseHandle(_hThread) ;
	return ;
}
int GetWindowsDrive(void)
{
	char	_szTmp[1024], _c ;
	LPSTR	_lpszTmp = _szTmp ;
	GetWindowsDirectory(_lpszTmp,MAX_PATH) ;
	_c = *_lpszTmp ;
	return ((int) _c) ;
}
int PASCAL WinMain(HINSTANCE __hInstance, HINSTANCE __hPrevInstance, LPSTR __lpszCmdLine, int __nCmdShow)
{
	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 ;
	iNbDirs = 0 ;
	GetSpecialFolder(&FOLDERID_History,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_InternetCache,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_CDBurning,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_Cookies,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_PrintHood,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_Recent,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_AppUpdates,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_Downloads,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_Screenshots,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_SavedSearches,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\Downloaded Program Files") ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\Downloaded Program Files") ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\Prefetch") ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\SoftwareDistribution") ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\Logs") ;
	MainDelete(_lpszDirectory) ;
	GetWindowsDirectory(_lpszDirectory,MAX_PATH) ;
	lstrcat(_lpszDirectory,"\\Temp") ;
	MainDelete(_lpszDirectory) ;
	GetSpecialFolder(&FOLDERID_SearchHistory,_lpszDirectory) ;
	MainDelete(_lpszDirectory) ;
//FOLDERID_AppDataProgramData	GUID	{559D40A3-A036-40FA-AF61-84CB430A4D34}
//FOLDERID_LocalAppData			GUID	{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}
	SHEmptyRecycleBin(NULL,NULL,SHERB_NOCONFIRMATION) ;
	wsprintf(_lpszDirectory,"%c:\\Windows.old",GetWindowsDrive()) ;
	MainDelete(_lpszDirectory) ;
	WinExec("Dism.exe /online /Cleanup-Image /StartComponentCleanup",SW_SHOW) ;
	wsprintf(_szTmp,"Number of files deleted : %lu\nNumber of directories deleted : %lu",iNbFiles,iNbDirs) ;
	MessageBox(NULL,_szTmp,"PhRCleaner",MB_OK|MB_ICONEXCLAMATION) ;
return (0) ;
}


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.