MATLAB: File Names inside a folder(x) and subfolders of(x)

filenamefolderinsidesubfolder

i want to find out all the files having (csv extension) within a folder (x) and the fileshaving (csv extension) within the subfolders of folder(x).
Also i want to save the output in a text file.

Best Answer

Here you go:
Path = 'C:\Users\' % wherever you want to search
searchPath = [Path ,'\**\*.csv']; % Search in folder and subfolders for *.csv
Files = dir(searchPath); % Find all .csv files
% Save to text file
fid = fopen('C:\Users\FoundFiles.txt','wt'); % create file
formatSpec= '%s\n' % new Line after String
for i = 1:size(Files,1) % write each string in for-loop
fprintf(fid,formatSpec, Files(i).name);
end
fclose(fid); % close file again
Greetings
Stefan
Related Question