File Names inside a folder(x) and subfolders of(x)
4 次查看(过去 30 天)
显示 更早的评论
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
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 个评论
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!