Next Available Filename

Version 4.0.2 (10,5 ko) par Stephen23
Return the next unused filename, incrementing an integer as required.
477 téléchargements
Mise à jour 31 oct. 2024

Afficher la licence

Note de l’éditeur : 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 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 (automatically zero padded if required).
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)'

Citation pour cette source

Stephen23 (2024). Next Available Filename (https://www.mathworks.com/matlabcentral/fileexchange/64108-next-available-filename), MATLAB Central File Exchange. Extrait(e) le .

Compatibilité avec les versions de MATLAB
Créé avec R2010b
Compatible avec les versions R2009b et ultérieures
Plateformes compatibles
Windows macOS Linux
Catégories
En savoir plus sur File Operations dans Help Center et MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Publié le Notes de version
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.