I'm working on a program for which I need to see if a string could theoretically be a filepath/directory (ether absolute or relative). I've been looking around trying to see if there is an easy way to do this and I'm finding loads of questions which focus on also seeing if the file/folder exists (e.g., this or this)
Is there a way to see if a string is a filepath/directory without it actually having to exists?
EDIT: I was asked in the comments to elaborate on what a valid filepath/directory is:
The way my program handles filepaths is that it usually (depending on exactly what it needs to load) takes a few "parts" of the filepath/directory and uses that to read a file.
Therefore we can use the following rules:
either the string contains a filepath which:
- starts with the name of the root folder (absolute filepath), or starts with a directory name (relative filepath), or starts with a filename.
- has 0 or more
/
characters to indicate subfolders. - ends with an extension (Example: .txt, .vs or .fs)
Directories, however
- starts with the name of the root folder (absolute directory), or starts with a directory name (relative directory)
- has 0 or more
/
characters to indicate subfolders. - ends with a
/
character.
Also, no spaces are allowed in the string.
I'm making the software to run on Windows initially but I might need to port it to Linux (therefore changing the name of the root folder and maybe more).