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

조회 수: 3 (최근 30일)
Sim
Sim 2024년 2월 1일
댓글: Sim 2024년 2월 1일
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
Stephen23
Stephen23 2024년 2월 1일
편집: Stephen23 2024년 2월 1일
"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 2024년 2월 1일
A very clear explanation!! Many many thanks @Stephen23!!! :-)

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2024년 2월 1일
S = dir('C:\Users\XXX\Desktop\...\*.csv');
S(startsWith({S.name},'.')) = [];

추가 답변 (1개)

Sim
Sim 2024년 2월 1일
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};

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by