Thread: Optimise FindFiles.

  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002

    Optimise FindFiles.

    Following is a recursive find files implementation. Does anyone have any ideas on how to further optimize it?

    EDIT: No worries, I think this version is about as optimised as it is going to get.

    static HRESULT FindFiles(LPTSTR szPath, BOOL bDeep, LPTSTR pAsterisk)
    	/* FindFiles expects a modifiable string of MAX_PATH length
    	 * with trailing backslash and asterisk. eg. "C:\\my documents\\*" */
    	HRESULT hr    = NOERROR;
    	HANDLE hFind;
    	WIN32_FIND_DATA fdata;
    	hFind = FindFirstFile(szPath, &fdata);
    	*pAsterisk = TEXT('\0');    /* Remove trailing asterisk */
    	if (INVALID_HANDLE_VALUE != hFind) {
    		do {
    			if (pbj->bCancel) { hr = E_ABORT; break; }
    			if (!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
    				ProcessFile(szPath, &fdata);
    			else if (TEXT('.') != fdata.cFileName[0] && bDeep)
    				HRESULT hrSub;
    				TCHAR szChild[MAX_PATH];
    				LPTSTR szEnd;
    				SIZE_T cchRemaining;
    				if (SUCCEEDED(StringCchCopyEx(szChild, ARRAYSIZE(szChild), szPath,  &szEnd, &cchRemaining, 0)) &&
    				    SUCCEEDED(StringCchCopyEx(szEnd, cchRemaining, fdata.cFileName, &szEnd, &cchRemaining, 0)) &&  
    				    SUCCEEDED(StringCchCopyEx(szEnd, cchRemaining, TEXT("\\*"),     &szEnd, NULL, 0)))
    					hrSub = FindFiles(szChild, TRUE, szEnd - 1);
    				if (FAILED(hrSub) && E_ABORT != hrSub)
    					ReportError(MSG_ERR_FIND_SUB_DIR, hrSub, ET_WARNING2, __FILE__, __LINE__, szPath, fdata.cFileName);
    		} while (FindNextFile(hFind, &fdata));
    	else hr = HRESULT_FROM_WIN32(GetLastError());
    	return hr;
    Last edited by anonytmouse; 01-14-2004 at 10:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is there a better way to optimise this?
    By qubit67 in forum C Programming
    Replies: 20
    Last Post: 09-07-2007, 05:09 AM
  2. Optimise?
    By rtan3005 in forum C Programming
    Replies: 17
    Last Post: 11-14-2006, 02:10 PM
  3. Will 'static' optimise my code?
    By samGwilliam in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2005, 10:28 AM
  4. optimise algorithm
    By cyberCLoWn in forum C++ Programming
    Replies: 10
    Last Post: 01-18-2005, 01:27 PM
  5. How to optimise this c program?
    By megablue in forum C Programming
    Replies: 5
    Last Post: 06-28-2003, 04:20 AM
Website Security Test