-6

我想检查文件的大小

如果我使用它是否正确

aux = dir(diary_file);
sizeOfFile = aux.bytes;

bytes: 362

现在我想检查文件的大小是否 >1 ,我该怎么做

4

2 回答 2

3

除非我在这里遗漏了一些东西,否则它非常简单:

if sizeOfFile > 1
   disp('Size of file is greater than 1'); % or do whatever else you want in that case
else
   disp('Size of file is less or equal to 1'); % or do whatever else you want in that case
end
于 2013-11-12T14:26:25.223 回答
3

只是为了找点乐子,这里有一个更防白痴的版本:

try 
    aux = dir(diary_file);
catch ME
    ME2 = MException('insert:id', 'Could not get directory listing for file/dir:');
    throw(addCause(ME2, ME));
end

if ~isempty(aux) 

    if numel(aux) == 1
        sz = aux.bytes;
    elseif aux.isdir
        error('insert:id', 'Expected single file; got directory listing.');
    else
        error('insert:id', 'Inconsistent directory listing.');
    end

    if ispc
        [~,~,ext] = fileparts(aux.name);
        if strcmpi(ext, '.lnk')
            warning('insert:id', ...
               'File seems to be a link; size may be misrepresented.'); 
        end
    end

    if sz > 1
        % CHECK PASSED
    else
        % CHECK NOT PASSED
    end

else
    error('insert:id', 'File does not exist.');
end
于 2013-11-12T14:30:05.040 回答