【MATLAB】文件夹或文件名是否设置正确
编写背景
之前编写了一个判断文件夹路径是否合规的函数,现在也要对文件名是否合规进行设置
使用说明
直接调用函数fileJudge,输入要参与判断的路径或者文件名或文件的绝对地址
[ Message ] = fileJudge( 'D:')
[ Message ] = fileJudge( 'ces.doc')
[ Message ] = fileJudge( 'D:\ces.doc')
编写函数
function [ Message ] = fileJudge( Value ) % 判断char格式是否为文件夹格式或文件名是否正确
Message = 1;% Char 的路径格式正确
if isempty( Value );Message = '输入出错';return;end
if iscell(Value);try Value = cell2mat(Value);catch;Message = '输入出错';return;end;end
if ~ischar( Value );Message = '输入出错';return;end
[ Path , FileName , ~ ] = fileparts(Value);
%% 路径
if ~isempty( Path )
if isfolder(Path)==0
TMEPPath = split(Path,':');
% 判断第二字符为:':'
if length( TMEPPath) ~=2
Message = '路径出错';
else
% 判断':'之后是否存在无效字符
if contains(TMEPPath{2},'\\')==1
Message = '路径出错';
else
if contains(TMEPPath{2},{'/',':',':','*','?','<','>','|','"'})==1
Message = '路径出错';
end
end
end
end
end
%% 文件名 % 不包含文件名后缀
if ~isempty( FileName ) % 判断是否存在无效字符
Message = '路径出错';
if contains( FileName , {'\','/',':',':','*','?','<','>','|','"'})==1
Message = '路径出错';
end
end
end