Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
Daniela Correa
Daniela Correa am 21 Apr. 2018
Bearbeitet: Jan am 21 Apr. 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 Kommentare
Rik
Rik am 21 Apr. 2018
You can't have two files with the same name in the same folder.
Stephen23
Stephen23 am 21 Apr. 2018
Bearbeitet: Stephen23 am 21 Apr. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 21 Apr. 2018
Bearbeitet: Jan am 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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by