필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Daniela Correa
Daniela Correa 2018년 4월 21일
편집: Jan 2018년 4월 21일
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
Rik
Rik 2018년 4월 21일
You can't have two files with the same name in the same folder.
Stephen23
Stephen23 2018년 4월 21일
편집: Stephen23 2018년 4월 21일

"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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 4월 21일
편집: Jan 2018년 4월 21일

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.

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by