20090622学习心得:创建多级目录

20090622学习心得:创建多级目录

今天试着写了一个创建多级目录的函数,用C语言实现。
涉及的库函数如下:
strcpy
strchr
_access
_mkdir
这些函数的具体使用方法参考msdn :),这里不再一一列出。

今天(20090623)有做了点更改,支持网络路径的创建。当然要先保证能对网络目录具有读写权限才可以:)

//  头文件
#include  < io.h >
#include 
< direct.h >

// ==================================================================
// 函数名: CreateMultiLevelDirectory
// 作者:  xinxin
// 日期:  2009-06-21
// 功能:  创建多级目录
// 输入参数:保存多级目录的字符串
// 返回值: TRUE:创建成功; FALSE:失败.
// ==================================================================
BOOL CreateMultiLevelDirectory( const   char   * strFilePathName)
{
    
char  strFilePath[ 260 ];
    
char  strTemp[ 260 ];
    
char   * s,  * p;

    strcpy(strFilePath, strFilePathName);
    
    s 
=  strFilePath;
   
    // if strFilePathName is network path, skip the ip address/host name
    // Modified on 20090623

     if(0 == strncmp(s, "\\\\", 2))
    {
        s += 2;
        s = strchr(s, '\\');
         if(!s)
        {
            return (FALSE);
        }
        else
        {
            s += 1;
        }
    }

    
do
    {
        p 
=  strchr(s,  ' \\ ' );
        
if (p)
        {
            
* =   ' \0 ' ;
        }
        s 
=  strFilePath;

        
//  directory doesn't exist
         if ( - 1   ==  _access(s,  0 ))
        {
            
//  failed to create directory.
             if ( - 1   ==  _mkdir(s))
            {
                
return  (FALSE);
            }
        }

        
if (p)
        {
            
* =   ' \\ ' ;
            s 
=  p  +   1 ;
        }
    } 
while (p);

    
return  (TRUE);
}

你可能感兴趣的:(20090622学习心得:创建多级目录)