Getting inexistent files using dir function

8 visualizaciones (últimos 30 días)
Luis Eduardo Cofré Lizama
Luis Eduardo Cofré Lizama el 2 de Ag. de 2023
Comentada: DGM el 3 de Ag. de 2023
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}));

Respuesta aceptada

DGM
DGM el 2 de Ag. de 2023
Editada: DGM el 2 de Ag. de 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 comentarios
Image Analyst
Image Analyst el 2 de Ag. de 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 el 3 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 2 de Ag. de 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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by