How to save text files with the exactly same name, in the same folder?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I have this rotine, and all my textfile are 'erros.txt' from differents subfolders, and I want to save all in the same folder.
mainFolder = uigetdir('C:\Users\Dani\Documents\PORTUGAL\Error_Sims'); % Select your Main folder
[~,message,~] = fileattrib([mainFolder,'\*']);
fprintf('\n There are %i total files & folders.\n',numel(message));
allExts = cellfun(@(s) s(end-2:end),{message.Name},'uni',0);% Get file ext
TXTidx = ismember(allExts,'txt');% Search extensions for "CSV" at the end
TXT_filefolders = {message(TXTidx).Name}; % Use idx of TXTs to list paths.
fprintf('There are %i files with *error.txt* file ext.\n',numel(TXT_filefolders));
for ii = 1:numel(TXT_filefolders)
% open TXT_filefolders
copyfile(TXT_filefolders{ii}, 'C:\Users\Dani\Documents\PORTUGAL\Pasta_Erros','f')
end
2 commentaires
Réponses (1)
Jan
le 21 Avr 2018
Modifié(e) : Jan
le 21 Avr 2018
The code is strange. Using the message replies by fileattrib is a slow and indirect way to count the number of files in a folder. Comparing the last 3 character of the file names is not save, when a file is called e.g. "file.mtxt". Faster and easier:
FileList = dir(fullfile(mainFolder, '*.txt')); FileList([FileList.isdir]) = []; % A folder can have a file extension also for ii = 1:numel(FileList) ...
For the actual question: This cannot work, as mentioned in the comments already, because the file names must be different.
0 commentaires
Voir également
Catégories
En savoir plus sur Text Files 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!