Matlab: check if string has correct file path syntax

I want to check if a string represents the full path to a file, for example:

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

File does not exist, so commands such as isdirand exist, I return false, but still a format string is a valid path for my operating system and the next - not because it is an invalid character for the file name:

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

So, I would like to know how to check if a string matches a valid file path without requiring the file to actually exist.

+4
source share
2 answers

, regexp Windows .

if isempty(regexp(p, '^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$', 'once'))
   // This is not a path
end
+3

Matlab :

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

, , . , , .

, -.

+1

Source: https://habr.com/ru/post/1682730/


All Articles