How to make a new folder within another folder?

Hi, I need to make a new sub folder within a main folder. When using mkdir(newfolder, newsubfolder) where newfolder is the parent folder where I want the newsubfolder saved, I get syntax errors every time. The full code is attached below.
> In CSVtoMatLab (line 77) Error using mkdir The filename, directory name, or volume label syntax is incorrect.
Error in CSVtoMatLab (line 85) mkdir(newfolder,newsubfolder);
Thanks for any help.
if true
clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Asks for User Inputs %%%%%%%%%%%%%%%%%%%%%%
prompt={'Enter a value for R:','Enter a value for C:','Enter a value for beta:','Enter a value for Life Goal:','Enter a value for N:','Enter a value for m:'};
title='Resonant Dwell Calculator Inputs';
answer=inputdlg(prompt,title);
a = str2num(answer{1});
b = str2num(answer{2});
c = str2num(answer{3});
d = str2num(answer{4});
e = str2num(answer{5});
f = str2num(answer{6});
%%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
projectdir = 'C:\Users\it58528\Documents\Dig Test'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%Reliability goals %%%%%%%%%%%%%%%%%%%%%%%%%%%%
R = a;
C = b;
beta = c;
LifeGoal = d;
N = e;
m = f;
%%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newdir, subfolderi);
mkdir(newfolder,newsubfolder);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename, ext] = fileparts(thisfile);
data = csvread(thisfile,5,2);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:17);
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newsubfolder = fullfile(newdir, subfolderi);
mkdir(newsubfolder)
newfilename = fullfile(newsubfolder, filenamei);
save(newfilename,'PIN');
end %files within subfolder
end %subfolders within folder

4 Comments

Please tell us the complete error message, which means all of the red text. The error message has useful information that help use to understand what is happening when your code runs.
Sorry, I went ahead and added it.
Have you put breakpoints in?
This kind of thing is usually far quicker to find the problem with by just putting in a breakpoint, looking on the command line at the components you are using and passing to mkdir and seeing the problem than waiting for answers on a forum.
The
mkdir(newfolder, newsubfolder)
syntax works fine in a general case if the arguments you give it are sensible - i.e. an existing parent folder and a valid string for the subfolder.
Obviously you will need to have write access in the folder too though.

Sign in to comment.

 Accepted Answer

Just use one argument:
mkdir(newsubfolder);
And in the for fileidx = 1 : length(fileinfo) loop, get rid of the lines
newsubfolder = fullfile(newdir, subfolderi);
mkdir(newsubfolder)
This has to be done just once for each subfolder, not once for each file in a subfolder.

4 Comments

Your method works, but it only creates one subfolder. The directory that I am reading this data from has 4 folders which have 200 folders each, with random amounts of .csv files. This recreates the 200 folders, but I need it to recreate the 200 folders within their original four folders, which was what I was hoping to do.
For example, to test I am reading csv data from a folder call CSV Data within a folder called Dig Test. What I need the program to do is save an array as a .mat file inside of a folder called CSV Data that is inside of a folder called Dig Test in a different directory, so I can keep the original file structure, with .mat files instead of .csv.
Currently, your method only saves the data within a folder called CSV Data, which I tried to fix using the method above, but that's where I ran into the above issue.
You have the first loop that should create the four folder using
mkdir(newfolder);
and you have the inner loop that creates the 200 subfolders using
mkdir(newsubfolder)
I would add a disp(newfolder) and disp(newsubfolder) in front of every mkdir command to see what is actually created.
Yea, that works a little. It creates the two folders within the specified directory. So within the folder 'Test' I get two files, 'Bla' and 'CSV Data'. And within the CSV Data is the data I am saving. Now what I need to happen is the 'CSV Data' folder to be inside of the 'Bla' folder. But this creates both folders inside of the directory, which is not what I want.
I assume I could somehow edit the second directory to point into the 'Bla' folder, so the 'CSV Data' folder is saved within it? I am not sure how to go about that without making it user friendly in the sense that you don't need to edit the directory within the code.
Thanks for the help.
Must read
newsubfolder = fullfile(newfolder, subfolderi);

Sign in to comment.

More Answers (0)

Categories

Asked:

on 29 Oct 2015

Commented:

Rik
on 26 Dec 2020

Community Treasure Hunt

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

Start Hunting!