Save text file from Subfolders in loop
3 views (last 30 days)
Show older comments
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 Comment
Geoff Hayes
on 17 Apr 2018
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.
Answers (0)
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!