Create Text file containing list of file names
Show older comments
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 Comments
C G
on 14 Apr 2018
per isakson
on 14 Apr 2018
"an error with one of my lines of code" Full error message, please!
Walter Roberson
on 14 Apr 2018
If the directory has folders (subdirectories) do you want the names of them to be excluded from the list? Do you want the list of all of the files (but not directories) to any level of depth? Or just non-directories that are directly in the current directory?
Walter Roberson
on 14 Apr 2018
files([files.isdir]) = []; %removes ., .. and all folders
per isakson
on 14 Apr 2018
"for 100 different directories" How to find the names of those directories?
C G
on 16 Apr 2018
C G
on 16 Apr 2018
C G
on 30 Apr 2018
Accepted Answer
More Answers (1)
Ahmet Cecen
on 14 Apr 2018
Edited: Ahmet Cecen
on 14 Apr 2018
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 Comments
Walter Roberson
on 14 Apr 2018
You should never rely on . and .. being the first two entries in the directory. It is not true on the NTFS file system or FAT file system, or on any of the Apple file systems, or on the more common Linux file systems that I have encountered. Basically it hasn't been true since the days of SVS2 (UNIX System 5 Version 2)
Ahmet Cecen
on 14 Apr 2018
Edited: Ahmet Cecen
on 14 Apr 2018
Ah, good to now this. I have never seen them not be the first two, but my breadth in operating systems (or file systems) is lacking.
Ahmet Cecen
on 14 Apr 2018
Ah there it is:
>> dir('*')
!test.txt . .. A1.tiff A2.tiff
Welp... gotta go do some changes in old codes now...
Walter Roberson
on 14 Apr 2018
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.
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!