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

I want to make a new folder each time I run my code.
Example:
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.

3 件のコメント

Walter Roberson
Walter Roberson 2019 年 12 月 16 日
What is the code for nextname() ?
Jay Vaidya
Jay Vaidya 2019 年 12 月 16 日
編集済み: Stephen23 2019 年 12 月 16 日
This is borrowed from this extrenal program. The code is attached below as well.
EDIT: Copyright code removed from this comment. Do NOT post COPYRIGHT code on this forum ... unless you are the copyright holder and wish to release your own code under the Creative Commons Attribution Share Alike 3.0 license:
Jay Vaidya
Jay Vaidya 2019 年 12 月 16 日
Ok Sorry for that. I will keep that in mind.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2019 年 12 月 16 日
編集済み: Stephen23 2024 年 11 月 4 日

0 投票

The main problem is that you are not passing nextname the path of that location, so instead of looking in the location that you want nextname will only check the current directory. How do you expect nextname to know where to look for those folders if you do not tell it where to look?
In addition there are some simplifcations/improvements:
  1. nextname can optionally append exactly the same absolute/relative path from the input file/folder name onto its output, so you do not need to separately concatenate the path onto its output.
  2. nextname accepts the path as an optional first argument.
  3. nextname accepts the name with an integer in angle brackets, e.g. "newFolder<1>.txt"
So you should use nextname like this:
D = 'C:/Users/vaidy/Desktop/JAY/Labwork/Models for devices/FeFETIMT model/MATLAB code/Voltage source/25x25';
mkdir(nextname(D,'newFolder<1>',true))
This worked when I tried it just now:
D = '.'; % absolute or relative path
dir(fullfile(D,'newF*'))
No matches for pattern './newF*'.
mkdir(nextname(D,'newFolder<1>',true))
dir(fullfile(D,'newF*'))
newFolder1
mkdir(nextname(D,'newFolder<1>',true))
dir(fullfile(D,'newF*'))
newFolder1 newFolder2
mkdir(nextname(D,'newFolder<1>',true))
dir(fullfile(D,'newF*'))
newFolder1 newFolder2 newFolder3

1 件のコメント

Jay Vaidya
Jay Vaidya 2019 年 12 月 19 日
Thanks for clarifying my doubt.

サインインしてコメントする。

その他の回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2019 年 12 月 16 日

0 投票

This is easily solved:
dirname = fullfile(path_2_root_dir,sprintf('NewFolder-%s',datestr(now,'yyyymmdd-HHMMSS')));
mkdir(dirname)
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.
HTH

5 件のコメント

Jay Vaidya
Jay Vaidya 2019 年 12 月 19 日
Thank you. This is amazing.
Bjorn Gustavsson
Bjorn Gustavsson 2019 年 12 月 19 日
Yes, I know. And you still go for a cludgy solution where you will have to take extra steps to zero-pad the ID-numbers? (Which we all know is at best a temporary solution because we'll run into the case where we will generate just enough names to fill up the available space plus a few extra...)
Stephen23
Stephen23 2019 年 12 月 19 日
編集済み: Stephen23 2019 年 12 月 22 日
"And you still go for a cludgy solution where you will have to take extra steps to zero-pad the ID-numbers?"
Ouch, that hurt... but wait, what is that in the nextname documentation: it states that leading zeros can be specified quite simply in the suffix, for example if you want four digits:
nextname(fullfile(D,'newFolder'),'0001')
% ^^^^ specifies four digits!
The number will be padded with zeros to ensure that it has atleast as many digits as requested.
Unless of course you really consider adding a few leading zeros to the suffix as painfully difficult "extra steps", a bit like reading documentation...
Bjorn Gustavsson
Bjorn Gustavsson 2019 年 12 月 19 日
Yes. Let me ask how many until the problem I pointed out occurs?
Walter Roberson
Walter Roberson 2019 年 12 月 19 日
?? 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.)

サインインしてコメントする。

Image Analyst
Image Analyst 2019 年 12 月 19 日
編集済み: Image Analyst 2019 年 12 月 19 日

0 投票

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;
end
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);
uiwait(errordlg(message));
else
% Found a good name so make the folder.
mkdir(newFolder);
end

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2019 年 12 月 16 日

編集済み:

2024 年 11 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by