Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Directories

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
I want to create a directory that is several sub directories besed upon user input. I can do it fin if the user specifies say c:\test\ however if they want to create c:\test\test\ and c:\test does not exist CreateDirectory won't work. I could parse the string that is input and create c:\test\ first then create c:\test\test\ however there should be an easier way to do this.. any tips?

 
no. There is no way to create a directory in an inexistent directory. You should create nesting directories one by one in the nested order. John Fill
1c.bmp


ivfmd@mail.md
 
Here is how I did it:

Lets say the user typed in as you said "c:\test\test\"

first I do a chdir("C:\"), chdir returns non-zero if successful. Then I do a chdir to "test" then I do another chdir to test

if at any time chdir fails (except when drive letter fails) I call mkdir

Lets say c:\test did not exist here is what it would look like

// this uses pointer arithmitic, using MFC there are other
// ways

char path[128];
char current[128];
*current = 0;
char* ptr;
cin>>path;
ptr = strstr(path,"\\");

while(ptr)
{

strncpy(current,ptr-path);
ptr++;
if(!chdir(current))
if(strstr(current,":")
break; // invalid drive letter
else
{
mkdir(current);
chdir(current);
}
}
ptr = strstr(path,"\\");
}


I belive that should do it. You may need a modification if my logic is off but that is how I remember doing it.

matt
 
OK I have come up with an implementation of this however when I try to print out the path as it is being built it doesn't seem to work.. any ideas? I think it has something to do with the line 'char *token; //A token ' .. since token is a pointer there seems to be a problem

void CJobfileServer::MY_CreateDirectory(char *Path)
{

CString Output; //A temp output cstring
//Lets check to see how many occirances of "\\" there are in the string
int count=0; //The count of "\\" in the path
for(int i=0;i< strlen(Path); i++)
{
if(Path=='\\')
count++;
}

if(count == 2) //Since the path only has 2 &quot;\\&quot;'s then we can simply create the path streight out.
{
CreateDirectory(Path, NULL); //Creates the path right away
}
else
{
char TokenChar[]=&quot;\\&quot;; //Using a '\' as the delimiter
char *token; //A token
char CreatePath[255]; //The path that will be created.. this path should be filled in as we go.

token=strtok(Path,TokenChar);
while(token!=NULL)
{
strcat(CreatePath, token);
token=strtok(NULL,TokenChar);
Output.Format(&quot;Path is currently %s&quot;,CreatePath); //Should spit out the path as it is being built
AfxMessageBox(Output);
//Create the directory here if it is not already present
}
Output.Format(&quot;Path is currently %s&quot;,CreatePath); //Should spit out the complete path
AfxMessageBox(Output);
}

}
 
I have a much simpler function to create nested directories

basically it works by scanning the path string, and every time it finds a backslash, it creates the path up to that point.

Code:
BOOL CreatePath(LPCSTR lpszTarget)
{
	int  i, len;
	CString stDir;
	CString stPath(lpszTarget);			// A Copy of the Original Complete Path
	BOOL status;

	// Create all required subdirectory levels
	len = stPath.GetLength();
	// i starts at 4 to skip over creating root directory
	for (i = 4; i <= len; i++) {
		if (i == len || stPath[i] == '\\') {
			stDir = (i == len)? stPath : stPath.Left(i);
			status = CreateDirectory(stDir, NULL);
		}
	}

	return status;
}

Hope this helps :)I
 
What does the line:
stDir = (i == len)? stPath : stPath.Left(i);
Do? How does this work?


Cory
 
Cory

The line
stDir = (i == len)? stPath : stPath.Left(i)

simply is a conditional expression that if the condition (i == len) is TRUE then sets stDir = stPath, otherwise sets stDir = stPath.Left(i)

Hope this helps
Alistair :)I
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top