Search code examples
c++windowswinapifilepath

Can GetFileAttributesW handle url-encoded spaces (%20) in file paths?


Function call:

OS_WRAPI::GetFileAttributesW(file_name_str); // file_name_str value is L"C:\\Test%20Tool\\test.exe"

returns INVALID_FILE_ATTRIBUTES.

Function call:

OS_WRAPI::GetFileAttributesW(file_name_str); // file_name_str value is L"C:\\TestTool\\test.exe"

returns valid attributes.

Both paths exist.

How can I get the file attributes in the case of url-encoded spaces (%20) in the file path?


Solution

  • Can GetFileAttributesW handle spaces in filepaths?

    Yes.

    Note that L"C:\\Test%20Tool\\test.exe" does not contain a space. Probably you should be passing L"C:\\Test Tool\\test.exe".

    The documentation says:

    If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError.

    So you should, in case INVALID_FILE_ATTRIBUTES is returned, call GetLastError. I'd expect that to return ERROR_PATH_NOT_FOUND.

    If the question is actually

    Can GetFileAttributesW handle L"%20" in filepaths?

    The answer is still yes. If that path really exists, and GetFileAttributesW is returning INVALID_FILE_ATTRIBUTES, then there must be some other problem, but the presence of L"%20" in a file name presents no problems for the Windows API. Again start by calling GetLastError.

    Perhaps what's really at issue here, is that you think that Windows uses L"%20" to encode a string in the file system. It does not. On the file system, L"%20" and L" " are two distinct names.