Clear Filters
Clear Filters

Getting inexistent files using dir function

10 views (last 30 days)
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}));

Accepted Answer

DGM
DGM on 2 Aug 2023
Edited: DGM on 2 Aug 2023
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 Comments
Image Analyst
Image Analyst on 2 Aug 2023
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 on 3 Aug 2023
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 2 Aug 2023
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

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!