Effacer les filtres
Effacer les filtres

Save text file from Subfolders in loop

3 vues (au cours des 30 derniers jours)
Daniela Correa
Daniela Correa le 16 Avr 2018
Commenté : Geoff Hayes le 17 Avr 2018
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 commentaire
Geoff Hayes
Geoff Hayes le 17 Avr 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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur MATLAB Report Generator dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by