Create Text file containing list of file names

조회 수: 42 (최근 30일)
C G
C G 2018년 4월 14일
댓글: C G 2018년 4월 30일
I am sure this has been answered before somewhere, but I am getting really frustrated trying to find the correct code.
I have over 400 files in a directory. I would like to create a text file that lists all of their file names. Then place this text file into the directory with the original files, not the matlab working directory. I have to do this well over 100 times for 100 different directories.
The only code that has worked partially is this:
dirName = 'D:\ABN tdump files\ERA -I 10m\April 10m'; %# folder path
files = dir( fullfile(dirName,'*.') ); %# list all *.xyz files
files = {files.name}'; %'# file names
I have created a cell with all of my file names, but now I can't create the text file. I get the very ambiguous error message that just says there is an error with one of my lines of code. Not helpful. So. Question. How does one create a text file with a cell array?
  댓글 수: 8
C G
C G 2018년 4월 16일
Thank you Walter Roberson. Works Great.
files([files.isdir]) = []; %removes ., .. and all folders
This also worked.
files = dir(fullfile(dirName,'name*') );
C G
C G 2018년 4월 30일
This is for all that need to know how to create a lot of files and then have them move to a certain location. This code works a charm. I created an excel file with all of the directory paths, "FileDirectory". I then imported the list as a string.
for q = 1:144 %This number is linked to the number of cells that have filepaths. Pathnames in FileDirectory.xls.
filedirectory = FileDirectory{q,1};
dirName = filedirectory; %'D:\ABN_tdump_files\10_day_Traj\ERA-I_2500m\April2500m\'; %# folder path
files = dir(fullfile(dirName,'fdump*') ); %# list all *.xyz files
files = {files.name}; %'# file names
[fid,msg] = fopen(sprintf('INFILE'),'wt');
assert(fid>=0,msg)
fprintf(fid,'%s\n',files{:});
fclose(fid);
movefile ('C:\Users\cglor\Documents\HYSPLIT\INFILE', dirName)
end

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

채택된 답변

Stephen23
Stephen23 2018년 4월 14일
편집: Stephen23 2018년 4월 14일
Simpler:
D = 'D:\ABN tdump files\ERA -I 10m\April 10m';
S = dir(fullfile(D,'*.xyz')); % specify the file extension to exclude directories
[fid,msg] = fopen(fullfile(D,'text.txt'));
assert(fid>=3,msg)
fprintf(fid,'%s\n',S.name)
fclose(fid);
Repeat for each directory D.

추가 답변 (1개)

Ahmet Cecen
Ahmet Cecen 2018년 4월 14일
편집: Ahmet Cecen 2018년 4월 14일
Almost there. Add these lines to the bottom and it should work:
files(1:2) = []; % get rid of . and ..
fileID = fopen(fullfile(dirName,'list.txt'),'w'); % create and open file
cellfun(@(x) fprintf(fileID,'%s \n',x), files) % write file
  댓글 수: 4
Ahmet Cecen
Ahmet Cecen 2018년 4월 14일
Ah there it is:
>> dir('*')
!test.txt . .. A1.tiff A2.tiff
Welp... gotta go do some changes in old codes now...
Walter Roberson
Walter Roberson 2018년 4월 14일
I have been unable to find any documentation promising any particular directory order for NTFS. In practice the order has always been sorted by Unicode when I have tested. (Though I have not checked if it is utf8 or utf16-le or something else).
It is not uncommon these days for filesystems to sort the first block of a directory whenever a change is made to the block, but filesystems differ in whether they keep all of the blocks of one directory sorted, since doing so can result in unnecessary disk i/o. Filesystems have also differed about whether to sort alphabetically (allowing binary search on names) or sorting to have the most commonly used entries first.

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

카테고리

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