Borrar filtros
Borrar filtros

How to save text files with the exactly same name, in the same folder?

4 visualizaciones (últimos 30 días)
Daniela Correa
Daniela Correa el 21 de Abr. de 2018
Editada: Jan el 21 de Abr. de 2018
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 comentarios
Rik
Rik el 21 de Abr. de 2018
You can't have two files with the same name in the same folder.
Stephen23
Stephen23 el 21 de Abr. de 2018
Editada: Stephen23 el 21 de Abr. de 2018

"How to save text files with the exactly same name, in the same folder?"

I don't know of any OS that allows that. Is it allowed to number the filenames?

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 21 de Abr. de 2018
Editada: Jan el 21 de Abr. de 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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by