Check directory is full or not?

24 ビュー (過去 30 日間)
Aniya
Aniya 2016 年 10 月 21 日
回答済み: rooz 2020 年 2 月 5 日
How we can check the directory is full or not before write data to the file in that directory?
  2 件のコメント
KSSV
KSSV 2016 年 10 月 21 日
s = dir(path) ;
Gives you the contents of the directory. What do you mean by full?
Aniya
Aniya 2016 年 10 月 21 日
編集済み: Aniya 2016 年 10 月 21 日
directory size exceeded or not.Is it possible?

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2016 年 10 月 21 日
First, note that there isn't really a concept of a full directory. Directories themselves don't have a space limit (unless it's a network share that has some quota, but that's a completely different thing). It's the logical/physical disk where the directory resides that has a space limit.
So really, if you want to know if you have enough space to store something, you need the check the disk, not the directory. There is no tool for that in matlab. You could query the size of all the directories as per Siva's answer, or go through Java, .Net (if on windows) or system commands but it is the wrong approach anyway.
Because you have no control over what happens the file system, it may well have become full between the time you asked if there was space and the time you attempt to write to it (however short that interval is). Asking beforehand is pointless. The correct approach when dealing with file system operations is simply to try them and check that they succeed:
[fid, errmsg] = fopen('somefile', 'w');
if fid == -1
error('failed to create file because: %s', errmsg);
end
try
fwrite(fid, somedata);
fclose(fid);
catch ex
fclose(fid);
error('failed to write file because: %s', ex.msgtext);
end
Note that this is true of any file operation. There is no point checking that a file exists before using it as some other program may have deleted it in between the check and the moment you use it, there is no point checking that you have permission to write somewhere since some other process may have changed the permission in between. Just try the operations and handle the failure appropriately.
  1 件のコメント
Aniya
Aniya 2016 年 10 月 21 日
Thank you Guillaume

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2016 年 10 月 21 日
編集済み: KSSV 2016 年 10 月 21 日
function [x] = dirsize(path)
s = dir(path);
name = {s.name};
isdir = [s.isdir] & ~strcmp(name,'.') & ~strcmp(name,'..');
subfolder = fullfile(path, name(isdir));
x = sum([s(~isdir).bytes cellfun(@dirsize, subfolder)]);
end
Gives you the size of input directory in bytes....you can convert bytes to MB by dividing with 10^6.
Modified as suggested by: Guillaume
  2 件のコメント
Guillaume
Guillaume 2016 年 10 月 21 日
Rather than strcat(x, filesep(), y, ...) use fullfile which does the same in less code:
fullfile(path, name(isdir));
Aniya
Aniya 2016 年 10 月 21 日
Thank you siva...

サインインしてコメントする。


rooz
rooz 2020 年 2 月 5 日
Hi,
Just a question, wouldn't the following work as well:
if numel(dir(path)) <=2
% folder is empty
else
% folder isn't empty
end

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by