getUniqueFileName(f​ile, varargin)

버전 1.1.1.0 (4.22 KB) 작성자: Zhengyi
GETUNIQUEFILENAME returns a unique file name if the supplied file name has been taken.
다운로드 수: 12
업데이트 날짜: 2018/5/14

라이선스 보기

In scenarios where you want to save a file and the file name is generated programmatically, you may want to check whether the file name has been taken by another file in the same path. In this case, GETUNIQUEFILENAME generates a new file name by adding a suffix to the original file name with a specified pattern.
OutFile = getUniqueFileName(InFile, Argments);
InFile: file name including file path and extension. File path can be skipped if it is the current Matlab path.
OutFile: if no conflict, the original file name if conflicting, a file name suffixed with a sequential integer enclosed in parenthesis. Sequential integer starts from 1.
Arguments: one or both of the following:
- an integer: custom start number for the sequential integer.
- formatting spec:
string formatting spec: must include one and only one operator '%d', additional operators may cause error or unexpected file name.

datetime format spec: use formatted datetime string as suffix, now is used to get the serial date number.

'DefaultDateSpec': uses '_ddmmyy-HHMMSSFFF' as the datetime format spec

Note: If a datetime suffixed filename still conflicts with one of the existing file's name, ' (%d)' will be suffixed.

Example:

For a file structure:
X:/
|- file.txt
|- file (1).txt
|- file (2).txt

Commands:
fname = 'X:/file.txt';
NEWFILE_1 = getUniqueFileName(fname);
NEWFILE_2 = getUniqueFileName(fname, 5);
NEWFILE_3 = getUniqueFileName(fname, '_%d');
NEWFILE_4 = getUniqueFileName(fname, 0, '_v%d');
NEWFILE_5 = getUniqueFileName(fname, '_dd-mm-yy_HH-MM-SS-FFF');
NEWFILE_6 = getUniqueFileName(fname, 'DefaultDate');

Results:
NEWFILE_1 = X:/file (3).txt
NEWFILE_2 = X:/file (5).txt
NEWFILE_3 = X:/file_1.txt
NEWFILE_4 = X:/file_v0.txt
NEWFILE_5 = X:/file_01-01-1900_00-00-00-000.txt
NEWFILE_6 = X:/file_0101190000-0000000.txt

인용 양식

Zhengyi (2024). getUniqueFileName(file, varargin) (https://www.mathworks.com/matlabcentral/fileexchange/67274-getuniquefilename-file-varargin), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R10
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.1.1.0

Fixed a problem where supplying a file name with path included results in an infinite loop.

1.1.0.0

Now works for files with no extension.
Fixed a mistake in description.

1.0.0.0