Getting inexistent files using dir function

13 ビュー (過去 30 日間)
Luis Eduardo Cofré Lizama
Luis Eduardo Cofré Lizama 2023 年 8 月 2 日
コメント済み: DGM 2023 年 8 月 3 日
Hi All, need some help withh this. Im currently reading all files names froom my folder containing data using the script below. Althoughj my folder contains only 48 files, the finfo structure produced shows 52, including files with similar names to the ones that are real (e.g., V1_001) and 2 "ghosts" files. The prob is that I'm creating a loop with the length of the strcuture and reading the names nuumbers, which is affected bny this 4 odd/inexistent names/files. Any idea what is wrong here?
finfo = dir (strcat(pwd,'/',Schools{v}));
Many thansk
Eduardo
finfo = dir (strcat(pwd,'/',Schools{v}));

採用された回答

DGM
DGM 2023 年 8 月 2 日
編集済み: DGM 2023 年 8 月 2 日
Those are hidden files. It's unknown why they're hidden. Perhaps they are temporary or autosave files that exist because the file by the same name (without the ._) is open in some other application. Perhaps they are not currently open in another application, but were open days ago when the power went out or the application crashed or the directory contents were copied. It's up to you whether you want to investigate their reason for existing and determine whether they need to be cleaned up.
If you want to exclude files that start with '.', then exclude them based on that criteria.
dinfo = dir('mydirectory/*.csv');
dinfo(strncmp({dinfo.name}, '.', 1)) = []; % skip files and dir starting with '.'
You might also simply be able to avoid them with a more restrictive expression.
dinfo = dir('mydirectory/V1_*.csv'); % something like?
  3 件のコメント
Image Analyst
Image Analyst 2023 年 8 月 2 日
Yes, the second case would definitely work and is preferred over the first case because it's simpler. That's what I did in my answer below. I tested it and it worked great.
DGM
DGM 2023 年 8 月 3 日
Learning to tailor specificity to the task is probably lesson #1, but FS cleanliness is worth a mention.
I'm lazy, but I also don't like dead files accumulating. Encountering temp files sets off a "does this need to exist" compulsion in my brain. Nevermind the gigabytes of chaff I hoard; I must destroy 20-year old invisible 1kB junk files. I'm dumb like that.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 8 月 2 日
Try this:
folder = 'Volumes\EDUARDO-A\DET Data\HR_V1';
filePattern = fullfile(folder, 'V*.*'); % Only files that start with V
fileList = dir(filePattern);
for k = 1 : numel(fileList)
thisFile = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Processing %s\n', thisFile);
% Now do something with the file.
end

カテゴリ

Help Center および File ExchangeCommunications Toolbox についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by