Next Available Filename

Returns the next unused file or folder name. The name is created by incrementing a numbered suffix.
459 download
Aggiornato 6 gen 2024

Visualizza la licenza

Nota dell'editore: This file was selected as MATLAB Central Pick of the Week

NEXTNAME returns a file or folder name, 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 those names, or when the code is stopped-and-started, or throws errors while calculating. This function offers one simple solution: call the function with the required file (or folder) name, the required suffix (including the starting integer) and the file extension (if any), and it will return the next unused name.
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.
Three text inputs are required (may be string scalars or char vectors):
1. The basic file or folder name, without the file extension (if any). If the location to be checked for existing files is not the current directory then the basic name must use a relative or absolute path to that location.
2. The suffix, which must contain the starting number. Some examples of suffixes are: '0', '_1', '(5)', '-0001-backup', '_temp1', etc.. This suffix will be appended to the file/folder name (before the file extension), and the number will be incremented to identify the first unused name starting from the number provided in the suffix. Thus the suffix defines:
* the starting value (can be zero or any positive integer, i.e. 0, 1, 2, 3, etc.).
* the minimum length of the output number (use leading zeros as required).
* any literal characters.
3. The file extension, if required. For folders and files without extensions use '' or "".
Note the inputs 1 and 3 are easily obtained from a filename using FILEPARTS
%% Current directory contains files 'A1.txt', 'A2.txt', and 'A4.txt':
>> nextname("A","1",".txt")
ans = "A3.txt"
>> nextname("A","001",".txt")
ans = "A003.txt"
%% Subdirectory 'HTML' contains folders 'B(1)', 'B(2)', and 'B(4)':
>> nextname('HTML/B','(1)','')
ans = 'B(3)'
>> nextname('HTML/B','(001)','')
ans = 'B(003)'
>> nextname('HTML/B','(1)','',false) % default = name only.
ans = 'B(3)'
>> nextname('HTML/B','(1)','',true) % with the path from the input name.
ans = 'HTML/B(3)'

Cita come

Stephen23 (2024). Next Available Filename (, MATLAB Central File Exchange. Recuperato .

Compatibilità della release di MATLAB
Creato con R2010b
Compatibile con qualsiasi release
Compatibilità della piattaforma
Windows macOS Linux
Scopri di più su File Operations in Help Center e MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Versione Pubblicato Note della release

* Return name with same class as first input.


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


Simplify string handling.


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


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


* Simplify documentation examples.


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


* Improve efficiency.


* Handle single matched subfolder.

* Add check for no matching files.

* Update HTML docs.