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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create directory

Status
Not open for further replies.

Maurader

Technical User
May 8, 2002
59
CA
how do i create directories on the harddrive from my program?
 
you can use:

Code:
#include<windows.h>
....
...
BOOL CreateDirectory(
  LPCTSTR lpPathName,                         // directory name
  LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
);

example:

CreateDirectory(&quot;c:\\my directory&quot;, NULL);

or you might also use:

#include<direct.h>
....
...

int _mkdir( const char *dirname );

example:

_mkdir(&quot;c:\\my directory&quot;)
 
is there a way to check if the directory already exists? or is it necessary?
 
Just call the mkdir function or any other function and check for its error code. If the creation was a succes, then you can delete the directory you just created, if you just wanted a check. Else, an error is returned.

I don't know any other means to check for something like that (at least, no ANSI ways). [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
#include <windows.h>

DWORD dwAttrs = GetFileAttributes ( &quot;C:\\Is\\This\\A\\Dir&quot; );
if ( dwAttrs == 0xffffffff )
{ // failure of GetFileAttrs
} else
{ if ( dwAttrs & FILE_ATTRIBUTE_DIRECTORY )
{ // It is a directory
} else
{ // It exists, but is not a directory
} }
Marcel
 
Duh...
Yep Marcel... That brings me to another idea... why not use stat function? I completely forgot about it, so I'd redflag my previous post :-( [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
if &quot;CreateDirectory&quot; returns 0 than it means that this function has fail,there could be many reasons.
One of these reasons could be because the directory already exist.

And for &quot;_mkdir&quot; if it return -1 it means that the function has fail.It could be for the same reasons than before.

&quot;_mkdir&quot; will return 0 if it succeeds and &quot;CreateDirectory&quot; will return 1.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top