How to make a new folder within another folder?

45 views (last 30 days)
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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';
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({}, {'.', '..'})); %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({}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newdir, subfolderi);
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);
newfilename = fullfile(newsubfolder, filenamei);
end %files within subfolder
end %subfolders within folder

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (0)


Find more on Large Files and Big Data 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!