I wanted a function, possibly amongst Path Functions, that would check if file-name would be valid. By valid, I meant if character present in the string are all valid (having no ?
, >
etc, for example). But sadly, there is no function. Browsing through the net, and SO, I found few techniques, none of them I liked, or found solid.
%TEMP%
path of the system. If creation fails, the filename is (possibly) invalid. Otherwise, it is valid (and therefore, delete the file).?:*>
)An extended form of function would be to check all invalid names (like AUX
, CON
etc), but that's not an issue (at least for now).
Is there any documented/undocumented function, that I might have missed, which would reliably check if filename (not pathname) is valid.
Edit: the PathCleanupSpec
function is now deprecated and no longer supported. Refer to the Requirements
section at the end of the linked page for details.
Thanks Connor, for the function. For other readers, the function name is PathCleanupSpec
. Using which I have implemented following:
bool IsLegalFileName(LPCWSTR filename)
{
WCHAR valid_invalid[MAX_PATH];
wcscpy_s(valid_invalid, filename);
int result = PathCleanupSpec(nullptr, valid_invalid);
// If return value is non-zero, or if 'valid_invalid'
// is modified, file-name is assumed invalid
return result == 0 && wcsicmp(valid_invalid, filename)==0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With