Files read with "dir" have additional (nondesirable) characters

3 vues (au cours des 30 derniers jours)
Sim
Sim le 1 Fév 2024
Commenté : Sim le 1 Fév 2024
Some csv files read by dir as
'._20190217.csv'
instead of
'20190217.csv'
?
How to remove dot and underscore in the names of the files?
My code is very simple:
files = dir('C:\Users\XXX\Desktop\...\');
filenames = {files.name};
where:
>> files
files =
90×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
>> filenames'
ans =
90×1 cell array
{'.' }
{'..' }
{'._20190217.csv'}
{'._20190218.csv'}
{'._20190219.csv'}
{'._20190220.csv'}
{'._20190221.csv'}
...
{'20190517.csv' }
{'20190518.csv' }
{'20190519.csv' }
{'20190520.csv' }
{'20190521.csv' }
I used the same code on macos (Matlab R2023a) and everything worked perfectly. Now, on windows ('23.2.0.2485118 (R2023b) Update 6'), I get that strange output for a number of csv files.
  3 commentaires
Stephen23
Stephen23 le 1 Fév 2024
Modifié(e) : Stephen23 le 1 Fév 2024
"Files read with "dir" have additional (nondesirable) characters"
No, DIR does not add characters to those filenames. Those really are the filenames of those files.
"How to remove dot and underscore in the names of the files?"
I strongly doubt that you want to do that: why do you want to remove some characters that are the only way to identify temporary hidden files created by MacOS? And doing so would require attempting to create duplicate filenames (see your screenshot)... you really would have problems attempting that.
Apparently you copied data from a folder on MacOS (e.g. by saving on a drive) which includes many hidden files that MacOS creates. MacOS does not show them (that is why they are called "hidden" files) and who knows what magic its filesystem does when you call DIR on MacOS. The internet has sufficient explanations of MacOS temporary files, there is no point in copying it all here.
In any case, once you have them on a Windows machine then they have no special (i.e. hidden) status any more, they are simply some random files. So of course your DIR call returns them.
Filter for names that start with a dot and remove them from your cell array. So far I do not see what the problem is.
"If it can help, I have noticed that "dir" returns the same files names with a dot and underscore at the beginning."
This is an unfortunate property of the filesystem. They are not files, they are directories. The dot directory names refer to the current directory and its parent directory. They are definitely not guaranteed to be the first two names returned: their location depends entirely on (alphabetic) sorting of the names. Code that assumes that they are the first two names is buggy and wrong.
The simplest way to avoid them is to specfy the DIR search name with suitable name pattern (with wildcards as required).
Sim
Sim le 1 Fév 2024
A very clear explanation!! Many many thanks @Stephen23!!! :-)

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 1 Fév 2024
S = dir('C:\Users\XXX\Desktop\...\*.csv');
S(startsWith({S.name},'.')) = [];

Plus de réponses (1)

Sim
Sim le 1 Fév 2024
After the extremely useful comment of @Stephen23, I found a way to read my files:
mypath = 'C:\Users\XXX\Desktop\...\';
files = dir(fullfile(mypath,'2019*.csv'));
filenames = {files.name};

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by