File Exchange

image thumbnail

Next Available Filename

version 2.0.0 (8 KB) by Stephen Cobeldick
Returns the next unused file or folder name. The name is created by incrementing a numbered suffix.

8 Downloads

Updated 01 Jan 2020

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Returns the next available filename or foldername, creating the name by incrementing a numbered suffix.

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. This function offers one simple solution: call the function with the required file (or folder) name, the required suffix (with starting integer) and the file extension (if any), and it will return the next unused name.

%% Inputs %%

Three inputs are required:

1. The basic file or folder name string (1xN char), 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 string (1xN char), which must contain the starting number. Some examples of suffixes are: '0', '_1', '(5)', '.backup.0001', '_temp_050', 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 controls:

* the starting value (can be zero or any positive integer, i.e. 0, 1, 2, etc.).
* the minimum length of the output number (use leading zeros as required).
* any literals and other required characters (note: this function does not check if the literals are permitted by the filesystem. User beware!)

3. The file extension, if required. For folders and files without extensions use ''.

%% Examples %%

%%% Current directory contains files 'A1.m', 'A2.m', and 'A4.m':

>> nextname('A','1','.m')
ans = 'A3.m'

>> nextname('A','001','.m')
ans = 'A003.m'

%%% 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) % prepend same path as the input name.
ans = 'HTML\B(3)'

Cite As

Stephen Cobeldick (2020). Next Available Filename (https://www.mathworks.com/matlabcentral/fileexchange/64108-next-available-filename), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

Shinuo Weng

Exactly what I need.

Updates

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.

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux