用FileCopy函数复制文件,把文件存在另一个目录,如果该目录不存在,则自动创建目录?怎么实现?谢谢

解决方案 »

  1.   

    可以先判断一下这个目录是否存在,如果不存在就新建一个
    sub foldercheck()
      Set fs = CreateObject("Scripting.FileSystemObject")
      if fs.FolderExists(folderspec) then  'folderspec是你要判断文件是否存在的路径
        exit sub
      else
        fs.CreateFolder(foldername) 'foldername是你要创建的文件夹
      end if
    end sub
      

  2.   

    Function GetDirectory(ByVal PathStr As String) As Boolean
    On Error GoTo ExectureError
        ChDir PathStr
        GetDirectory = True
        Exit Function
    ExectureError:
        GetDirectory = False
    End FunctionIf (Not GetDirectory(Folder)) Then
        MkDir Folder
        FileCopy SourceFilename, DestFileName
    End If
      

  3.   


    '采用FileSystemObject对象建立多级目录
    Public Function CreateFolders(ByVal sFileName As String) As Boolean
        On Error GoTo Err1
        Dim vaPath As Variant
        Dim nEnd As Long
        Dim nLoop As Long
        Dim sPath As String
        Dim nCnt As Long
        Dim FSO As New FileSystemObject
        If Not FSO.FolderExists(FSO.GetParentFolderName(sFileName)) Then
            vaPath = Split(sFileName, "\")
            nEnd = UBound(vaPath) - 1
            sPath = vaPath(0)
            For nLoop = 1 To nEnd
                sPath = sPath & "\" & vaPath(nLoop)
                If Not FSO.FolderExists(sPath) Then
                    FSO.CreateFolder sPath
                End If
            Next
        End If
        CreateFolders = True
    Exit Function
    Err1:
        CreateFolders = False
    End Function示例:Msgbox IIf(CreateFolders("c:\1\2\3\4\5\6\7\8"), "建立目录成功!", "建立目录失败")
      

  4.   

    用mkdir就可以创建文件夹了
    dir():可以判断文件夹是否存在
      

  5.   

    直接用filecopy会自动创建目录,只要在前面加上:
    on error resume next哈哈,你试试。这是个不好的主意。:)
      

  6.   

    TO: acev(睡眠不足--> http://computer98.com/)Private Sub Command1_Click()
        On Error Resume Next
        FileCopy "c:\1.txt", "c:\1\2\3\1.txt"
    End Sub证实你的想法是错误的
      

  7.   

    可以使用SHFILEOPSTRUCT,具体可查MSDN