Remove invalid characters from a file name in Windows (C++/MFC)



In Windows, a file or folder name cannot contain the following characters \/:*?\”<>| . Sometimes before saving a file, you may want to remove these invalid characters from the file name. Here is the function written in C++/MFC.

 
void RemoveInvalidFileNameCharacters(CString & strFileName)
{
	LPCTSTR pInvalidCharSet = _T("\\/:*?\"<>|");
	int iLen = (int)_tcslen(pInvalidCharSet);
 
	for(int i=0;i<iLen;i++)
	{
		//remove each invalid character
		strFileName.Remove(pInvalidCharSet[i]);
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *