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

조회 수: 4 (최근 30일)
karan goyal
karan goyal 2019년 2월 5일
댓글: Image Analyst 2019년 2월 6일
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.

채택된 답변

StefBu
StefBu 2019년 2월 5일
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
  댓글 수: 2
karan goyal
karan goyal 2019년 2월 6일
it worked man.thank you so much .
the only thing which i would like to further take is that i want to see the folders name too in the text file and all the files in it and then loop out of that folder and then again write the next folder's name and the files in it.
what changes can i do in the code?
Image Analyst
Image Analyst 2019년 2월 6일
Not sure what you want. I'm making a couple of guesses.
To see anything IN the text files, you'll have to call fopen(), then fgetl(), and then fclose().
To split apart a full filename into folder, base filename with no extension, and extention, use fileparts():
[folder, baseFileNameNoExt, ext] = fileparts(fullFileName);
Not sure why you need to "loop out of that folder" (or even what that means exactly) and have another loop after it. Why can't you do everything about listing/printing filenames and folder names inside that main loop?

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 2월 5일
See attached demo.

카테고리

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