How to write names of many files in single excel file along with its folder name?
조회 수: 4 (최근 30일)
이전 댓글 표시
Dear experiences
i have many folders where every folder contain large number of text files.. i need to write their names along with parent directory name in an excel file.. such as following:
for example if there are master folder include 100 folders { fol1, fol2, fol3..etc} in every sub-folder{fol1,fol2..etc} there are some text files .. i need to write their names in an excel file where first column contain sub-folder name, and second columns contain the names of text files in that sub-folder name, and so on for others .. all result in a single excel file.. such that.
A column B column
fol1 1st text file name in fol1
fol1 2nd text file name in fol1
fol2 1st text file name in fol2
etc..
i have tested the following code but its write all files in first row..
projectdir = 'D:\masterfiles';
xls_filename ='D:\masterfiles\master.xls'
column_range = A;
row_range = 2;
xls_sheet = 'Sheet1';
dinfo = dir(projectdir);
dinfo(~[dinfo.isdir]) = []; %remove non-folders
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and .. directories
num_subdir = length(dinfo);
for S = 1 : num_subdir
this_subdir = dinfo(S).name;
subdinfo = dir( fullfile(projectdir, this_subdir, '*.htm*') );
num_subfile = length(subdinfo);
for F = 1 : num_subfile
this_file = subdinfo(F).name;
xlswrite(xls_filename,{dinfo(S).name}, xls_sheet, 'A');
xlswrite(xls_filename,{this_file}, xls_sheet, xlscol(row_range));
row_range =row_range + 1;
end
end
thanks for any suggestion.
댓글 수: 0
채택된 답변
Guillaume
2017년 2월 2일
Assuming you're using R2016b, where dir supports recursing into folder these 4 lines are all you need:
projectdir = 'D:\masterfiles';
t = struct2table(dir(fullfile(projectdir, '*\*.txt'))); %find text files in all immediate subdirectories of projectdir
t.folder = strrep(t.folder, fullfile(projectdir, '\'), ''); %fullfile(path, '\') canonise the path
writetable(t(:, {'folder', 'name'}), fullfile(projectdir, 'master.xls');
댓글 수: 5
Guillaume
2017년 2월 2일
Try this:
d = dir(fullfile(projectdir, '*'));
files = [];
for subdir = d([d.isdir] & ~ismember({d.name},{'.', '..'}))'
subfiles = dir(fullfile(projectdir, subdir.name, '*.txt'));
[subfiles.folder] = deal(subdir.name);
files = [files; subfiles]; %#OK<AGROW>
end
t = struct2table(files);
writetable(t(:, {'folder', 'name'}), fullfile(projectdir, 'master.xls');
추가 답변 (1개)
Jan
2017년 2월 2일
편집: Jan
2017년 2월 2일
Collect the data at first and write them to the file in one block:
% [EDITED, 02.02.2017 21:19 UTC]
projectdir = 'D:\masterfiles';
xls_filename = 'D:\masterfiles\master.xls';
xls_sheet = 'Sheet1';
dinfo = dir(projectdir);
dinfo(~[dinfo.isdir]) = []; %remove non-folders
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and .. directories
num_subdir = length(dinfo);
output = {};
for S = 1:num_subdir
this_subdir = dinfo(S).name;
subdinfo = dir(fullfile(projectdir, this_subdir, '*.htm*'));
if ~isempty(subdinfo )
subname = {subdinfo.name}.';
subpath = repmat({this_subdir}, length(subname), 1);
output = cat(1, output, cat(2, subpath, subname)); % [BUGFIX]
end
end
Range = sprintf('A%1:B%d', size(output, 1));
xlswrite(xls_filename, output, xls_sheet, Range);
댓글 수: 3
Jan
2017년 2월 2일
The code had another bug, which is fixed now. The range must be specified to avoid filling the complete column.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!