How do I create a new folder each time I run a code?

16 views (last 30 days)
I want to make a new folder each time I run my code.
If there is a folder named 'newFolder', then generate newFolder1 after running once.
If there is a folder named 'newFolder7', then generate newFolder8 after running once.
In general, if there is a folder named 'newFolderx', then generate newFolderx+1 after running once.
I am using this code below: (important part is the nextname fumction usage)
mkdir("C:/Users/vaidy/Desktop/JAY/Labwork/Models for devices/FeFETIMT model/MATLAB code/Voltage source/25x25/" + nextname('newFolder','1')).
mkdir("C:/Users/vaidy/Desktop/JAY/Labwork/Models for devices/FeFETIMT model/MATLAB code/Voltage source/25x25/" + nextname('newFolder','1')).
But this generates a new folder only for the first time, and later it says that the folder already exists.
Jay Vaidya
Jay Vaidya on 16 Dec 2019
Ok Sorry for that. I will keep that in mind.

Sign in to comment.

Accepted Answer

Stephen23 on 16 Dec 2019
Edited: Stephen23 on 1 Jan 2020
There are two problems with your code:
  1. You are not passing nextname the path of that location, so instead of looking in the location that you want, it will only check the current directory. How do you expect nextname to know where to look for folders if you do not tell it where to look?
  2. The current version of nextname can optionally return exactly the same absolute/relative path of the input file/folder name (plus the incremented suffix) in its output, so you do not need to concatenate the path onto its output.
You need to use nextname something like this:
D = 'C:/Users/vaidy/Desktop/JAY/Labwork/Models for devices/FeFETIMT model/MATLAB code/Voltage source/25x25';
This worked when I tried it just now:
>> D = 'C:\Users\stephen.cobeldick\Documents'; % not the current folder!
>> dir(fullfile(D,'newF*'))
'C:\Users\stephen.cobeldick\Documents\newF*' not found.
>> mkdir(nextname(fullfile(D,'newFolder'),'1','',true))
>> dir(fullfile(D,'newF*'))
>> mkdir(nextname(fullfile(D,'newFolder'),'1','',true))
>> dir(fullfile(D,'newF*'))
newFolder1 newFolder2
>> mkdir(nextname(fullfile(D,'newFolder'),'1','',true))
>> dir(fullfile(D,'newF*'))
newFolder1 newFolder2 newFolder3

More Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 16 Dec 2019
This is easily solved:
dirname = fullfile(path_2_root_dir,sprintf('NewFolder-%s',datestr(now,'yyyymmdd-HHMMSS')));
Gives you an easy enough time to separate directories, with the added bonus that you explicitly have the create-time in the name, and this will give you unique numbers that sort nicely.
Walter Roberson
Walter Roberson on 19 Dec 2019
?? There are no ID-numbers mentioned in the original question. Also, the original author specifically indicated that the second folder was to be newFolder1 -- not, for example, newFolder0001 so padding with zeros is not desired by the original poster (and not necessary for Stephen's FEX contribution.)

Sign in to comment.

Image Analyst
Image Analyst on 19 Dec 2019
Edited: Image Analyst on 19 Dec 2019
Here's another way:
parentFolder = pwd; % Wherever you want
prefix = 'newFolder'; % Whatever you want.
baseFolderName = sprintf('%s1', prefix);
newFolder = fullfile(parentFolder, baseFolderName);
maxIterations = 5000; % Whatever. However many times you want to try before giving up.
counter = 1;
while isfolder(newFolder) && counter <= maxIterations
baseFolderName = sprintf('%s%d', prefix, counter); % Can use %3.3d if you want leading zeros.
newFolder = fullfile(parentFolder, baseFolderName);
counter = counter + 1;
if counter >= maxIterations
% Could not find a suitable folder name. Alert the user.
message = sprintf('Could not find a folder name after trying %d times', maxIterations);
% Found a good name so make the folder.

Community Treasure Hunt

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

Start Hunting!