How to make directories within directories within directories

23 views (last 30 days)
Hi,
The first loop 'for i=1..' creates 3 directories, '1,2,3'. The second loop, 'for n=1:..', then creates two directories inside each of them called 'a' and 'b'. The code is successful thus far. The final loop (list3) attempts to create two further directories 'q' and 'r' within 'a' and 'b'. This loop produces another a,b containing q,r, which is fine, but at the same level as 1,2,3 which is not. So I obtain mainfolder\(1&2&3)\(a&b) + mainfolder\(a&b)\(q+r), rather than the required mainfolder\(1&2&3)\(a&b)\(q+r). I was attempting to use mkdir as in the example: mkdir(Parentfolder, foldername) which creates the foldername within Parentfolder. Thanks in advance! Sorry for the long winded explanation and awkward notation
function testforum();
list1=["1","2","3"]
list2=["a","b"]
list3=["q","r"]
for i=1:length(list1)
x=list1(i)
mkdir(sprintf('%s',x))
for n=1:length(list2)
y=list2(n)
mkdir (sprintf('%s',x),sprintf('%s',y))
for p=1:length(list3)
z=list3(p)
mkdir (sprintf('%s',y),sprintf('%s',z))
end
end
end
end
  2 Comments
Jan
Jan on 21 May 2018
Using the string type makes the code more complicated than necessary. The pile of sprintf commands are confusing.
Guillaume
Guillaume on 21 May 2018
string doesn't complicate anything. mkdir, fullfile, etc. are all happy with strings.

Sign in to comment.

Accepted Answer

Jan
Jan on 21 May 2018
Edited: Jan on 21 May 2018
Use absolute paths for the folders. You cannot create a subfolder using the mkdir(parent, sub) notation, if parent is not found. Example:
base = 'D:\Temp'; % Assumed to be existing
cd(base);
mkdir('A'); % C:\Temp\A created
mkdir('A', 'B'); % C:\Temp\A\B created, because 'A' is found in current directory
mkdir('B', 'C'); % This creates C:\Temp\B\C, not C:\Temp\A\B\C
mkdir('A\B', 'C'); % This creates C:\Temp\A\B\C
Much easier:
mkdir(fullfile(base, 'A', 'B', 'C'))
Use full path names to avoid any troubles. The notation mkdir(parent, sub) was needed in Matlab R5.3, but after 20 years I suggest to use the modern power of mkdir to create a complete folder tree at once.
A fixed version of your code:
base = 'D:\Temp'; % Set as needed
list1 = {'1', '2', '3'}
list2 = {'a', 'b'}
list3 = {'q', 'r'}
for i = 1:length(list1)
x = list1{i}
for n = 1:length(list2)
y = list2{n}
for p=1:length(list3)
z = list3{p}
mkdir(fullfile(base, x, y, z));
end
end
end
  2 Comments
Guillaume
Guillaume on 21 May 2018
I'll reiterate what I said in my answer because it's important: Numbered variables are always an indication of a flawed design. Note that x, y, z is the same as numbered variables. Any kind of sequential naming is indication of a flawed design.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 21 May 2018
Numbered variables are always an indication of a flawed design. Your algorithm is not easily extensible to adding more levels to your directory structure since it means adding another numbered variable and another for loop.
It would be much simpler to have a single variable and a recursion or a single loop. But since mkdir will create all the directories necessary, I would just build all the full paths and pass that in a loop:
lists ={["1","2","3"], ["a","b"], ["q","r"]};
%build the cartesian product of elements of lists:
cartprod = cell(size(lists));
[cartprod{:}] = ndgrid(lists{:});
%pass the cartesian product to fullfile to build the paths
allpaths = fullfile(cartprod{:});
%now iterate over all the paths
for pidx = 1:numel(allpaths)
mkdir(allpaths(pidx)); %will create all inexistant directories in the path
end
The above works with any number of levels, just add the extra level(s) to lists, up to the combinatorial limit of ndgrid.

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!