How to make a new folder within another folder?

21 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
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
Adam
Adam on 29 Oct 2015
Edited: Adam on 29 Oct 2015
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

Thorsten
Thorsten on 29 Oct 2015
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
Ibro Tutic
Ibro Tutic on 29 Oct 2015
Edited: Ibro Tutic on 29 Oct 2015
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.
Thorsten
Thorsten on 30 Oct 2015
Must read
newsubfolder = fullfile(newfolder, subfolderi);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!