Save text file from Subfolders in loop
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I'm using this rotine to identify my text files, and it's processing all my text files from my hundreds subfolders. But I don't know how I can save my text files in another place. Because I want to separate and save my text files from my subfolders in loop.
*
% Define a starting folder wherever you want
start_path = fullfile(matlabroot, 'C:\Users\Dani\Documents\PORTUGAL\Error_Sims');
% Ask user to confirm or change
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
listOfFolderNames(:,1)=[];
numberOfFolders = length(listOfFolderNames);
% Process all text files in those folders.
for k = 1 : numberOfFolders
thisFolder = listOfFolderNames{k};
% fprintf('Processing folder %s\n', thisFolder);
% Get filenames of all TXT files.
filePattern = sprintf('%s/*error.txt*', thisFolder);
baseFileNames = dir(filePattern);
numberOfFiles = length(baseFileNames);
fullFileName=cell(numberOfFolders,1);
if numberOfFiles >= 1
for f = 1 : numberOfFiles
fullFileName{k,1} = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing text file %s\n', fullFileName{k,1});
end
% else
% fprintf('Folder %s has no text files in it.\n', thisFolder);
end
end*
Thank you!
1 件のコメント
Geoff Hayes
2018 年 4 月 17 日
Daniela - do you just want to copy or move the files from one location to another? Perhaps take a look at copyfile to see if that will do what you want.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!