I wanna check if my string is valid windows file path. I was searching around and it seems that there is no reliable method to do that. Also I checked boost filesystem library , and no obvious function exist to do this check , maybe something like is_valid_windows_name
-
Do you need to check whether the provided path is just valid, or whether it's valid and whether the directory structure is already set up for the path, or whether there already exists a file there? – Jamie Wong Jun 21 '10 at 14:02
-
Good grief. This is not a duplicate since the other is tagged C# and the accepted answer gives C# APIs, whereas this is tagged C++. – Jim Balter Nov 05 '14 at 04:23
-
How about trying to create a file with that name on a Windows and see if it's a success? (maybe you can change the path a little for trial only.) – cshu Nov 05 '14 at 05:43
4 Answers
Yes, there is a boost function that does what you want. Take a look at boost::filesystem::windows_name(...). You will need to include boost/filesystem/path.hpp as well as link against the correct (version- and architecture-specific) libboost_system and libboost_filesystem libraries since path is not a header-only lib.

- 11
- 2
-
Boost might be a bit much if this were its only use, but Boost provides many next-generation C++ features, making it worthwhile for general C++ development. When you already use Boost, this is a great option. – sage May 16 '17 at 16:58
You could use _splitpath()
function and parse the output (based on it, you could easily say if your path is valid or not).
See MSDN for additional information.
Note that this function is windows-specific.

- 4,945
- 2
- 26
- 27
I do not believe there is a standard c++ api for that.
Note that the Windows API allows more filenames than the Windows Shell (The filenames the user is allowed to use in windws explorer).
You should have a look at the windows shell api.
Another possibility is to use trial and error, this way you are truly independend of the current filesystem.
The easiest way is to disallow
\ / < > | " : ? *
and you should be fine.

- 28,510
- 21
- 92
- 151
It's a pity even the newest C++17 filesystem library doesn't have a function to verify file names.
You can use the Windows-specific Shell Lightweight Utility function PathFileExists
or the Windows API GetFileAttributes
and check the last error code specifically for ERROR_INVALID_NAME
.
I think it's kind of a misuse (because there really should be a dedicated function for it) but serves the purpose.

- 1,237
- 14
- 25
-
1Not so good if the file doesn't exist yet. Also, security might prevent its access until elevated. – CodeLurker Jul 25 '17 at 10:46
-
@CodeLurker Why not exactly? OP just wants to check for a valid windows file name/path – klaus triendl Nov 09 '17 at 13:16
-
OP says nothing about it existing already. If it does not, those will not help. Also, it just might be the case that we want to check, even if we don't (yet) have access to it. Note the solution I finally settled on here: https://stackoverflow.com/questions/3038351/check-whether-a-string-is-a-valid-filename-with-qt/45282192#45282192 – CodeLurker Nov 10 '17 at 14:12