autorename if saved file already exist

40 次查看(过去 30 天)
is there an option in save (or any other function) which support automatic renaming if the filename already exists or do I need to check by myself? (like saving as file2.mat if file.mat already exist)

采纳的回答

Jan
Jan 2012-1-14
You have to check it by your own if a file is existing already.
[EDITED: Consider {File.txt, File3.txt}]
function SaveWithNumber(FileName, Data)
[fPath, fName, fExt] = fileparts(FileName);
if isempty(fExt) % No '.mat' in FileName
fExt = '.mat';
FileName = fullfile(fPath, [fName, fExt]);
end
if exist(FileName, 'file')
% Get number of files:
fDir = dir(fullfile(fPath, [fName, '*', fExt]);
fStr = lower(sprintf('%s*', fDir.name);
fNum = sscanf(fStr, [fName, '%d', fExt, '*']);
newNum = max(fNum) + 1;
FileName = fullfile(fPath, [fName, sprintf('%d', newNum), fExt]);
end
save(FileName, 'Data');
Limitations: "FileNameInf.txt" and "FileNameNaN.txt" will cause troubles...
Consider, that:
exist(FileName, 'file')
replies TRUE for directories also.
  2 个评论
Image Analyst
Image Analyst 2012-1-14
Other options could be alerting the user that the file exists and to either
(1) Cancel (Skip it),
(2) Overwrite with the same name, or
(3) Use the new name determined by Jan's nice function, or
(4) call uiputfile to let the user supply a name.
You could use menu() to present the user with those options.
Jan
Jan 2012-1-14
My "nice" function is too lazy: Imagine these files are existing already: File.txt, File3.txt. Now the user wants to save File.txt - now the function tries to create File3.txt and fails recursively.
I'm posting a new version, which is hopefully smarter.

请先登录,再进行评论。

更多回答(1 个)

Diaa
Diaa 2020-10-1
编辑:Diaa 2020-10-1
Revisitng this old question, I followed the idea of Jan to come up with this solution of appending the existent file name with an incremental number enclosed in parentheses.
if exist(filename,'file')
[fPath, fName, fExt] = fileparts(filename);
fDir = dir(fullfile(fPath, [fName,' (*)', fExt]));
if isempty(fDir)
filename = fullfile(fPath, [fName,' (1)', fExt]);
else
pattern = "(" + digitsPattern + ")" + fExt;
hasThePattern = endsWith(extractfield(fDir,'name'),pattern);
Extracted = extract(extractfield(fDir(hasThePattern),'name'),pattern);
num = max(cell2mat(cellfun(@(C) textscan(C,'(%d)') , Extracted,'UniformOutput',true)));
num = num+1;
filename = fullfile(fPath, [fName,' (',num2str(num),')', fExt]);
end
end
  4 个评论
Mohammed Attrash
Mohammed Attrash 2023-7-9
You can add to your file name the following
datestr(now,'dd.mm.yyyy_HH.MM')
This will create a unique name
Walter Roberson
Walter Roberson 2023-7-9
It can sometimes be important to preserve the file extension, so make sure that is added before the extension.
These days it is better to use
string(datetime('now', 'format', 'uuuu.MM.dd_HH.mm'))
ans = "2023.07.09_08.27"

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by