Next Available Filename

버전 4.0.3 (10.7 KB) 작성자: Stephen23
Return the next unused filename, incrementing an integer as required.
다운로드 수: 487
업데이트 날짜: 2024/11/23

라이선스 보기

편집자 메모: This file was selected as MATLAB Central Pick of the Week

NEXTNAME returns a filename, incrementing a numbered suffix such that the returned name is not currently used by any file or folder.
On occasion it may be required to save files without knowing or requiring a particular number sequence, for example when saving interim results or backups during large calculations. Using an internal counter is one option, but this does not work when there are already existing files with the same names, or when the code is stopped-and-started unpredictably (e.g throws errors while calculating and is restarted). This function offers one simple solution: NEXTNAME will search for existing files/folders that match the provided inputs, increment an integer until the value is unused by any current filename, and then return the next unused filename.
Note that unlike some other submissions on FEX, this function compares the number values, not the literal filenames! This means you will not get "x001" if e.g. "x1" or "x01" or "x00000001" already exist in the specified location.
Syntax 1
One text input is required: the folder/filename (including file extension, if required). The folder/filename must include one integer within angle brackets (aka less/greater than characters), e.g.: 'test<01>.txt'. The integer will be incremented as required to define an unused filename. The angle brackets are not returned in the output.
Syntax 2
Three text inputs are required:
  1. The basic folder/filename, without any file extension.
  2. The suffix, which must contain one integer. Some examples of suffixes are: '0', '_1', '(5)', '-0001-backup', '_temp1', etc. This suffix will be appended to the folder/filename, before the file extension, after incrementing the integer as required to define an unused filename.
  3. The file extension, if required. For folders (or files without extensions) use '' or "".
Note the path, name, and extension inputs can generally be obtained from a filename using FILEPARTS.
Integer
The integer specifies:
* the value to start incrementing from (this can be zero or any positive integer, i.e. 0, 1, 2, 3, etc.),
* the minimum width of the output number by using leading zeros as required. For example, "001" specifies the integer in the output has (minimum) width of three characters. The output integer will be automatically zero padded (if required) to this length.
Examples
% Current directory contains files 'A1.txt', 'A2.txt', and 'A4.txt':
>> nextname("A<1>.txt") % syntax 1
ans = "A3.txt"
>> nextname("A","1",".txt") % syntax 2
ans = "A3.txt"
>> nextname("A<001>.txt") % syntax 1
ans = "A003.txt"
>> nextname("A","001",".txt") % sytnax 2
ans = "A003.txt"
% Directory 'HTML' contains subdirectories 'B(1)', 'B(2)', and 'B(4)':
>> nextname('HTML/B(<1>)') % syntax 1
ans = 'B(3)'
>> nextname('HTML/B','(1)','') % syntax 2
ans = 'B(3)'
>> nextname('HTML','B(<1>)') % syntax 1
ans = 'B(3)'
>> nextname('HTML','B','(1)','') % syntax 2
ans = 'B(3)'
>> nextname('HTML','B(<1>)',true) % syntax 1
ans = 'HTML/B(3)'
>> nextname('HTML','B','(1)','',true) % syntax 2
ans = 'HTML/B(3)'

인용 양식

Stephen23 (2024). Next Available Filename (https://www.mathworks.com/matlabcentral/fileexchange/64108-next-available-filename), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2010b
R2009b 이상 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

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

* Input checking and error message improvements.
* Documentation improvements.

4.0.2

* Documentation improvements.

4.0.1

* Documentation improvements.

4.0.0

* Add syntax 1: filename with integer in angle brackets.
* Documentation improvements.

3.1.0

* Return name with same class as first input.

3.0.2

* 4th input accepts numeric or logical.
* Improve documentation.

3.0.1

Simplify string handling.

3.0.0

* Accepts string scalar or char vector inputs.
* Add error IDs.

2.0.0

* Separate file extension input (allows for no-ext names and folders with periods).

1.3.1

* Simplify documentation examples.

1.3.0

* Optional third input selects to return name only or same path as input name.
* Use UINT64 for simpler integer handling.

1.2.1

* Improve efficiency.

1.2.0

* Handle single matched subfolder.

1.1.0.0

* Add check for no matching files.

1.0.0.0

* Update HTML docs.