How to save text files with the exactly same name, in the same folder?
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Jan
on 21 Apr 2018
Edited: Jan
on 21 Apr 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 Comments
See Also
Categories
Find more on Text Files 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!