我用mkdir来建立目录,但现在有个问题就是如果我建立的地方已经有这个目录了,就会出错有没有办法来先判断再建立, 还有个问题就是我如何在一个目录下建立一个二级目录呢?

解决方案 »

  1.   

    就是比方说我想做用dos命令实现就是   md temp 回车   cd temp  回车  md temp1
      

  2.   

    建目录这前先检查有没有这个目录。
    用dir
        If Dir("文件名", vbDirectory) Then
            MsgBox "存在"
        Else
            MsgBox "不存在"
        End If
      

  3.   

    二级目录。
    mkdir 一级目录名\二级目录名
      

  4.   

    mkdir "c:\temp" '以C盘根目录下建temp目录
    mkdir "c:\temp\temp1" '在c:\temp目录下建temp1子目录
    mkdir "c:\temp\temp1\temp2"   '在c:\temp\temp1目录下建temp2子目录
      

  5.   

    mkdir "c:\temp" '以C盘根目录下建temp目录
    mkdir "c:\temp\temp1" '在c:\temp目录下建temp1子目录
    mkdir "c:\temp\temp1\temp2"   '在c:\temp\temp1目录下建temp2子目录
      

  6.   

    快速建立多级目录Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    Private Declare Function CreateDirectory Lib "kernel32" _
    Alias "CreateDirectoryA" (ByVal lpPathName As String, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
    '函数:
    'Call CreateNewDirectory("c:\test\directory\vb\tips\")
    Public Sub CreateNewDirectory(NewDirectory As String)
        Dim sDirTest As String
        Dim SecAttrib As SECURITY_ATTRIBUTES
        Dim bSuccess As Boolean
        Dim sPath As String
        Dim iCounter As Integer
        Dim sTempDir As String
        iFlag = 0
        sPath = NewDirectory
        
        If Right(sPath, Len(sPath)) <> "\" Then
            sPath = sPath & "\"
        End If
        iCounter = 1
        Do Until InStr(iCounter, sPath, "\") = 0
            iCounter = InStr(iCounter, sPath, "\")
            sTempDir = Left(sPath, iCounter)
            sDirTest = Dir(sTempDir)
            iCounter = iCounter + 1
            SecAttrib.lpSecurityDescriptor = &O0
            SecAttrib.bInheritHandle = False
            SecAttrib.nLength = Len(SecAttrib)
            bSuccess = CreateDirectory(sTempDir, SecAttrib)
        Loop
    End Sub
    Private Sub Command1_Click()
        Call CreateNewDirectory("c:\test\directory\vb\tips\")
    End Sub