Create and name a directory

Hi
I want to create a subfolder in some folder and name that subfoder as data-"name of the folder". So if folder name is INFO, I want to create a sufolder in the INFO folder named DATA-INFO.
I have:
mkdir('DATA-' what to put here? )
What is the syntax of adding the main folder name to the mkdir subfolder name?
Thanks

답변 (3개)

Image Analyst
Image Analyst 2011년 11월 9일

5 개 추천

I don't recommend ever relying on "pwd" because that could change without you being aware of it, for example by some other badly written function you call. Don't even ask what pwd is if you're compiling your app - it gets complicated. I recommend keeping track of all your folders in variables that you create and control so that you know for certain what they are at all times. So my answer would be something like this that does not depend on pwd at all:
% First, get the name of the folder you're using.
% For example if your folder is 'D:\photos\Info', parentFolder would = 'D:\photos, and deepestFolder would = 'Info'.
[parentFolder deepestFolder] = fileparts(yourFolder);
% Next, create a name for a subfolder within that.
% For example 'D:\photos\Info\DATA-Info'
newSubFolder = sprintf('%s/DATA-%s', yourFolder, deepestFolder);
% Finally, create the folder if it doesn't exist already.
if ~exist(newSubFolder, 'dir')
mkdir(newSubFolder);
end
Note: forward slashes work in both Unix and Windows.

댓글 수: 15

osama
osama 2016년 12월 14일
nice .. and please tell me How to give an automatically number for folder as name where I want to creat new folders with numbering automatically ?
Image Analyst
Image Analyst 2016년 12월 14일
You can use sprintf() to create any folder name string from any variables you want. You know how to use sprintf() don't you?
osama
osama 2016년 12월 15일
Hello my friend I know but i have GUI and want to add condition to give number automatically sequentially 1,2,3 ... n and it shouldn't be created before 1 .2 .3 as name for new folders created by mkdir() i hope that u can help with question ?
osama
osama 2016년 12월 15일
@Stephen Cobeldick thank , i saw it already but it's not working with my situation i need to give number as name by mkdir() that havn't been before in folder or sequential number 1,2 ...n
Stephen23
Stephen23 2016년 12월 15일
@osama: please ask a new question giving a clear explanation of what you have and what you want.
osama, don't you know how to use sprintf() to create a string using a number? For example to make "folder4" with k = 4, do this:
folder = sprintf('folder%d', k);
Is that what you don't know how to do?
osama
osama 2016년 12월 15일
@Image Analyst i know but i want to give numbersequentially an automatically, for example there r folder inside curr-direct (folder 1) then when i going to creat new folder it will check and create new folder with numbering 2 !! i have GUI and it's impossible to write a cycle for this task, cause every load cycle will start with 1:n example but i need to give new number for name of new folder through creating thanks my friends for all support )
Image Analyst
Image Analyst 2016년 12월 15일
편집: Image Analyst 2016년 12월 15일
First you need to call dir() and get all the subfolders in a certain folder. Then examine their names with sscanf() to extract the number, or else you can strip off with indexing. For example if your existing sub folders are "folder 1", "folder 2" and so on, then you can get the subfolders like
numValidFolders = 1;
folders = dir('folder*')
for k = 1 : length(folders)
thisFolder = folders(k).name;
if folders(k).isdir && length(thisFolder) >= 3
thisFolderNumber(numValidFolders) = str2dobule(thisFolder(8:end));
numValidFolders = numValidFolders + 1;
end
end
lastNumber = max(thisFolderNumber);
nextFolder = sprintf('folder %d', lastNumber+1);
That's just off the top of my head, untested.
osama
osama 2016년 12월 15일
@Image Analyst thanks alot it's good idea ) i will try to do and then tell what get.
osama
osama 2016년 12월 16일
@Image Analyst please, can you explain me thisFolderNumber(numValidFolders) = str2dobule(thisFolder(8:end)); ? what mean ?
Image Analyst
Image Analyst 2016년 12월 16일
thisFolder(8:end) extracts everything from "folder " onwards, in other words, just the numerical part. But it's still a string at that point so str2double() converts it to a double type. Then numValidFolders is what one we're on, the first folder, the second one or whatever. It's not the same as k because we'll skip some filenames, like non-folders, and "." and ".." (if they're included). thisFolderNumber is the numerical array that has all the numbers at the ends of all the folder names.
osama
osama 2016년 12월 16일
@ Image Analyst ok , i undertand but also i get an error: Undefined function or variable 'str2dobule'.
Error in GUI>pushbutton1_Callback (line 102) thisFolderNumber(numValidFolders) = str2dobule(thisFolder(8:end));
Rohan Maharjajn
Rohan Maharjajn 2017년 8월 28일
@osama Did yo get the answer?? I am also facing the same problem!!
Image Analyst
Image Analyst 2017년 8월 28일
It should be str2double, not str2dobule.

댓글을 달려면 로그인하십시오.

Nirmal Gunaseelan
Nirmal Gunaseelan 2011년 11월 8일

1 개 추천

You should be creating customized strings using concatenation and passing those strings to mkdir. Current folder name could be obtained by manipulating output of PWD. Eg.
% This puts the entire path of current folder, but you can change as per what you need as the suffix
currFolderName = pwd;
% Create customized string name sub folder
mkdir(['DATA-', currFolderName]);
Fangjun Jiang
Fangjun Jiang 2011년 11월 8일

0 개 추천

Folder=pwd
[PathStr,FolderName]=fileparts(Folder)
DataFolder=['DATA-',FolderName]
mkdir(DataFolder)

댓글 수: 1

Stephen23
Stephen23 2016년 12월 15일
Don't use pwd in code like this: it is unreliable. See Image Analyst's answer below.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

질문:

2011년 11월 8일

댓글:

2017년 8월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by