Create and name a directory
Afficher commentaires plus anciens
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
Réponses (3)
Image Analyst
le 9 Nov 2011
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 commentaires
osama
le 14 Déc 2016
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
le 14 Déc 2016
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
le 15 Déc 2016
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
le 15 Déc 2016
@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
le 15 Déc 2016
@osama: please ask a new question giving a clear explanation of what you have and what you want.
Image Analyst
le 15 Déc 2016
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
le 15 Déc 2016
@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
le 15 Déc 2016
Modifié(e) : Image Analyst
le 15 Déc 2016
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
le 15 Déc 2016
@Image Analyst thanks alot it's good idea ) i will try to do and then tell what get.
osama
le 16 Déc 2016
@Image Analyst please, can you explain me thisFolderNumber(numValidFolders) = str2dobule(thisFolder(8:end)); ? what mean ?
Image Analyst
le 16 Déc 2016
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
le 16 Déc 2016
@ 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
le 28 Août 2017
@osama Did yo get the answer?? I am also facing the same problem!!
Image Analyst
le 28 Août 2017
It should be str2double, not str2dobule.
Nirmal Gunaseelan
le 8 Nov 2011
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
le 8 Nov 2011
Folder=pwd
[PathStr,FolderName]=fileparts(Folder)
DataFolder=['DATA-',FolderName]
mkdir(DataFolder)
1 commentaire
Stephen23
le 15 Déc 2016
Don't use pwd in code like this: it is unreliable. See Image Analyst's answer below.
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!