6

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.

  • Using a regular expression to check the contents of filename.
  • Creating a file name, possibly in %TEMP% path of the system. If creation fails, the filename is (possibly) invalid. Otherwise, it is valid (and therefore, delete the file).
  • Write up a function, that checks if invalid characters are present in the filename (e.g. ?:*>)

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.

Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 5
    There's a nice little function which will remove illegal characters for you: http://msdn.microsoft.com/en-us/library/bb776472%28v=VS.85%29.aspx – Connor McGuinness Nov 14 '14 at 11:12
  • 2
    You cannot technically figure out if a filename is not valid that is not a pathname, since the validity of a filename depends on the underlying file system as well. – danielschemmel Nov 14 '14 at 11:19
  • http://msdn.microsoft.com/en-us/library/windows/desktop/bb773608%28v=vs.85%29.aspx – BLUEPIXY Nov 14 '14 at 11:21
  • 1
    @gha.st, that's true, but the OS would know it. And, I am asking OS supplied function only. – Ajay Nov 14 '14 at 12:48
  • Thanks @BLUEPIXY, but `PathGetCharType` doesn't solve the problem. It works for path, but not (only) for filename. – Ajay Nov 14 '14 at 12:49
  • @Ajay You can have (many) different file systems on one OS. In fact, you may even end up with file systems that are not shipped with your OS. – danielschemmel Nov 17 '14 at 15:05
  • @gha.st I was only concerned about NTFS on Windows. I will reconsider WhenEver sun rises from west. – Ajay Nov 17 '14 at 15:08
  • @Ajay No need to be snarky. Adding the NTFS restriction makes the problem significantly easier. As to the sun rising from the west: Consider that USB sticks are not usually preformatted with NTFS and that many a NAS solution uses a linux core with an ext filesystem internally. – danielschemmel Nov 17 '14 at 16:00
  • Possible dublicate: http://stackoverflow.com/questions/62771/how-do-i-check-if-a-given-string-is-a-legal-valid-file-name-under-windows/62888 – koppor Jul 16 '15 at 21:13

1 Answers1

4

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;
}
Zombo
  • 1
  • 62
  • 391
  • 407
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 1
    You can accept your own answer if this solved the problem for you. – Jonathan Mee Nov 14 '14 at 12:17
  • 1
    `PathCleanupSpec` strips out colons that indicate the NTFS data stream to use such as `"file.ext:ads"` or `"file.ext:ads:$DATA"` for a record named "ads". The default for `"file.ext"` is the anonymous stream `"file.ext::$DATA"`. – Eryk Sun Nov 14 '14 at 15:27
  • 4
    Note that PathCleanupSpec is listed as deprecated. It might not exist in future versions of Windows. – Harry Johnston Nov 15 '14 at 03:05