MakeSureDirectoryPathExists检查目录是否存在

本文介绍了一个实用函数MakeSureDirectoryPathExists,用于一次性建立多级目录。即使中间层级缺失也能成功创建。文章提供了函数的详细说明、参数解释及返回值含义,并附带了完整的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


函数原型

编辑
BOOL  MakeSureDirectoryPathExists(
PCSTR  Dirpath
);

函数作用

该函数的作用是检查指定目录是否存在,如果不存在则创建整个Dirpath所表示的整个目录。

参数

Dirpath:要检查的目录名。如果是路径不是文件名,需以 '\' 结尾。

返回值

如果目录存在,返回TRUE;如果不存在但全部路径创建成功,返回TRUE;
如果不存在且创建失败,返回FALSE。

函数使用

编辑
一次性建立多级目录(用 CreateDirectory只能一级一级的建立)。
如:
MakeSureDirectoryPathExists( "c:\\a\\b\\ ");
如果a文件夹不存在也可以创建成功。
这个函数并不存在于 Kernel32.dll 中,需要包含头文件imagehlp.h,并链接imagehlp.lib。

        #include <ImageHlp.h>
        #pragma comment(lib,"imagehlp.lib")  
该函数的实现如下,以供使用和学习。
LPTSTR _tCharAlloc(UINT uSize)
{
	return (LPTSTR)malloc(sizeof(TCHAR) * uSize);
}
VOID _tCharFree(LPVOID p)
{
	free(p);
}
#define IMAGEAPI WINAPI
BOOL IMAGEAPI MakeSureDirectoryPathExists(LPCTSTR pszDirPath)
{
	LPTSTR p, pszDirCopy;
	DWORD dwAttributes;
	// Make a copy of the string for editing.
	__try
	{
		pszDirCopy = (LPTSTR)_tCharAlloc(lstrlen(pszDirPath) + 1);
		if(pszDirCopy == NULL)
			return FALSE;
		lstrcpy(pszDirCopy, pszDirPath);
		p = pszDirCopy;
		// If the second character in the path is "\", then this is a UNC
		// path, and we should skip forward until we reach the 2nd \ in the path.
		if((*p == TEXT('\\')) && (*(p+1) == TEXT('\\')))
		{
			p++; // Skip over the first \ in the name.
			p++; // Skip over the second \ in the name.
			// Skip until we hit the first "\" (\\Server\).
			while(*p && *p != TEXT('\\'))
			{
				p = CharNext(p);
			}
			// Advance over it.
			if(*p)
			{
				p++;
			}
			// Skip until we hit the second "\" (\\Server\Share\).
			while(*p && *p != TEXT('\\'))
			{
				p = CharNext(p);
			}
			// Advance over it also.
			if(*p)
			{
				p++;
			}
		}
		else if(*(p+1) == TEXT(':')) // Not a UNC. See if it's <drive>:
		{
			p++;
			p++;
			// If it exists, skip over the root specifier
			if(*p && (*p == TEXT('\\')))
			{
				p++;
			}
		}
		while(*p)
		{
			if(*p == TEXT('\\'))
			{
				*p = TEXT('\0');
				dwAttributes = GetFileAttributes(pszDirCopy);
				// Nothing exists with this name. Try to make the directory name and error if unable to.
				if(dwAttributes == 0xffffffff)
				{
					if(!CreateDirectory(pszDirCopy, NULL))
					{
						if(GetLastError() != ERROR_ALREADY_EXISTS)
						{
							_tCharFree(pszDirCopy);
							return FALSE;
						}
					}
				}
				else
				{
					if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
					{
						// Something exists with this name, but it's not a directory... Error
						_tCharFree(pszDirCopy);
						return FALSE;
					}
				}
				*p = TEXT('\\');
			}
			p = CharNext(p);
		}
	}
	__except(EXCEPTION_EXECUTE_HANDLER)
	{
		// SetLastError(GetExceptionCode());
		_tCharFree(pszDirCopy);
		return FALSE;
	}
	_tCharFree(pszDirCopy);
	return TRUE;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值