WriteDirectory() sporadically failed. My computer is running windows 7/32. I could fix the problem by checking the result of FindFirstFile(). This does not make sense, because FindClose() should also work with invalid handles, however this fix works: fFile = FindFirstFile(cs_directory_to_create,&fileinfo;); if (INVALID_HANDLE_VALUE != fFile) { ...
ReplyYou should change the line if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) to if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) The problem is that if the directory has more than just the directory attribute set the check will fail. I found this out by turning off the "Allow Indexing Service to index this disk for fast file searching" under properties for the drive in Windows XP. This added the attribute FILE_ATTRIBUTE_NOT_CONTENT_INDEXED thus causing the checks for the directory to fail. John.Reply
Originally posted by: san dinfer
can any one help me how to create a folder
Originally posted by: afsheen nasir
I want to create folder with vc++ code anybody can help me???
#include#include #include int main() { char pStrName[64]; char pStrDirectory[100]; cout<<"Enter The Name Of Folder To Be Create on C Drive:"< Reply
Originally posted by: Josh Hogle
If your code is for Windows 2000 or later, check out the SHCreateDirectory API call.
ReplyOriginally posted by: BOBA KOPOBbEB
SECURITY_ATTRIBUTES sa;
// security inits
// alloc & init SD
if ( ! InitializeSecurityDescriptor ( pSD,
// now set up the security attributes
2000 ONLY.
Looks fine at first glance, but acctually failed somewhere on the road. Take a look at Ownership/Auditing
PSECURITY_DESCRIPTOR pSD = NULL;
//SECURITY_DESCRIPTOR_CONTROL sdCtrl;
memset ( ( VOID *) &sa, 0, sizeof ( SECURITY_ATTRIBUTES) );
if ( ! ( pSD = ( PSECURITY_DESCRIPTOR)
( malloc ( SECURITY_DESCRIPTOR_MIN_LENGTH)) ) )
return;
SECURITY_DESCRIPTOR_REVISION) )
return;
// specify inheritanse
UINT res = SetSecurityDescriptorControl(pSD, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
printf("- ret = %d\n", res);
// set NULL DACL on the SD
if ( ! SetSecurityDescriptorDacl ( pSD, TRUE, ( PACL) NULL, FALSE) )
return;
sa.nLength = sizeof ( SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = pSD;
BOOL ret = CreateDirectory((LPCTSTR)path, &sa);
Originally posted by: Eugen
BOOL CreateDirectoryTree(LPCTSTR pszDirectory)
{
char szDir[MAX_PATH];
char *p, *pNext;
strcpy (szDir, pszDirectory);
pNext = strchr(szDir, '\\');
if (pNext)
{
pNext++;
while ((p=strchr(pNext, '\\')) != 0)
{
*p = 0;
if (GetFileAttributes (szDir) == -1)
{
if (CreateDirectory(szDir, 0) == FALSE)
{
return FALSE;
}
}
*p = '\\';
pNext = p+1;
}
}
if (GetFileAttributes (szDir) == -1)
{
if (CreateDirectory(szDir, 0) == FALSE)
{
return FALSE;
}
}
return TRUE;
}
strcpy (szDir, pszDirectory); is bad use: char szDir[MAX_PATH+1]; strncpy (szDir, pszDirectory,MAX_PATH); szDir[MAX_PATH] = 0; www.antitux.de!Reply
Originally posted by: Patrick Walsh
You will get a kick out of this one.
It will even prompt the user to format the drive if not formated, mount the network drive etc...
Reply
Originally posted by: Ashes
Although recursive logic might be simpler to code, recursive functions use up a lot more stack space. So, the non-recursive original code might be more useful, especially if I am creating very deeply nested paths (4K or greater).
However, I agree using the ImageHlp API is probably the best.
ReplyOriginally posted by: Vikas
See 'MakeSureDirectoryPathExists()'
This's really cool !!
Excellent method to create multiple level of SubDirectories !!