4

I want to check if a string represents a full path of a file, like this:

p = 'C:\my\custom\path.txt'

The file does not exist, so commands like isdir and exist return false to me, but still the format of the string represents a valid path for my operating system, while the following one does not because it has an invalid character for the file name:

p = 'C:\my\custom\:path.txt'

So I'd like to know how to check if a string represents a valid file path without needing that the file actually exists.

Johannes Riecken
  • 2,301
  • 16
  • 17
Jepessen
  • 11,744
  • 14
  • 82
  • 149

3 Answers3

5

You might want to use the regexp function with a regular expression to match Windows paths.

if isempty(regexp(p, '^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$', 'once'))
   // This is not a path
end
Horia Coman
  • 8,681
  • 2
  • 23
  • 25
  • Ok thanks. It's not cross platform but it should be easy to adapt it to other systems if needed. – Jepessen Aug 01 '17 at 09:36
  • Yup, I saw that the paths you provided were Windows ones so I assumed you'd only be interested in a Windows solution. The same link has an answer for UNIX/Linux paths, which are much more lax. Anything that's not / or NUL can appear as a filename/dirname more-or-less, though the filesystem also has some say in what it accepts as paths. – Horia Coman Aug 01 '17 at 09:40
  • Yes, I had to write a path for a specific operating system just to show something, but in my post I talked about operating system and not windows. Anyway is a great solution and it will solve my problems. – Jepessen Aug 01 '17 at 09:45
  • 1
    @Jepessen You can also use [`filesep`](https://www.mathworks.com/help/matlab/ref/filesep.html?s_tid=doc_ta) to make it more cross-platform – EBH Aug 01 '17 at 10:11
2

You can also let Matlab try for you:

if ~isdir(p)
    [status, msg] = mkdir(p);
    if status
        isdir(p)
        rmdir(p)
    else
        error(msg)
    end
end

First, you check if the folder exists, if not you try to create it. If you succeed then you delete it, and if not you throw an error.

This is not recommended for checking many strings but has the advantage of being cross-platform.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • 2
    I've two issues with this approach. The first is that my path contains also a file name, and `mkdir` wants a folder. The second thing is that the path can be related to non existent positions, like G hard disk that I don't have in PC, of in positions where I don't have write permissions. – Jepessen Aug 01 '17 at 09:43
  • I think making a directory just to test if a path is correct is a wrong approach. There is no need of fiddling with making new folders and such. – Ander Biguri Aug 01 '17 at 09:46
  • 1
    @AnderBiguri I agree that is not a clean approach, and the comment by the OP is right, but this has the advantage of being cross-platform (without even knowing what the platform is). – EBH Aug 01 '17 at 09:52
1
function bool = isLegalPath(str)
    bool = true;
    try
        java.io.File(str).toPath;
    catch
        bool = false;
    end
end
Daniele
  • 53
  • 7