FileNameDepth
FileNameDepth["name"] gives the number of path elements in the file name.
Examples
Count path elements:
FileNameDepth["/path/to/file.txt"]
(* 4 *)Relative path:
FileNameDepth["folder/subfolder/data.csv"]
(* 3 *)Please visit the official Wolfram Language Reference for more details.