首页 > 开发 > 其他 > 正文

VBA脚本语言测试Excel宏定义处理文件拷贝

2019-10-14 22:10:27
字体:
来源:转载
供稿:网友
VBA脚本语言测试Excel宏定义处理文件拷贝



  1. '指定文件目录复制工具(可用户基于svn版本管理的项目做增量发布用途使用)   
  2. '作者:许果   
  3. '日期:2010-11-26   
  4. Sub copyfiles()   
  5. On Error GoTo errorflag   
  6. '指定目标文件的行号   
  7. Dim i As Integer   
  8. '指定目标文件的目录的长度变量   
  9. Dim j As Integer   
  10. '目录名称数组,生成目标文件的过程目录名字列表   
  11. Dim directoryNameArray() As String   
  12. '指定的目标文件部分文件路径名称   
  13. Dim relativeFilePath As String   
  14. '临时目录名称   
  15. Dim tempDirectoryName As String   
  16. For i = 0 To Cells(3, 2) - 1   
  17.     tempDirectoryName = ""   
  18.     '获得指定的目标文件的路径名称   
  19.     relativeFilePath = Cells(4 + i, 2)   
  20.     If relativeFilePath = "" Then   
  21.         Exit For   
  22.     End If   
  23.     relativeFilePath = Replace(relativeFilePath, "/""/")   
  24.     directoryNameArray = Split(relativeFilePath, "/")   
  25.     '如果拷贝的目标文件件路径的目录不存在,就依次建立相应的目录文件   
  26.     For j = 0 To UBound(directoryNameArray) - 1   
  27.         tempDirectoryName = tempDirectoryName & "/" & directoryNameArray(j)   
  28.         If Dir(Cells(2, 2) & tempDirectoryName, vbDirectory) = "" Then   
  29.            MkDir Cells(2, 2) & tempDirectoryName   
  30.         End If   
  31.     Next j   
  32.        
  33.     '设置需要复制的源文件路径和目标路径   
  34.     Dim destinationFileFullPath As String   
  35.     Dim sourceFileFullPath As String   
  36.     sourceFileFullPath = Cells(1, 2) & tempDirectoryName & "/" & directoryNameArray(j)   
  37.     destinationFileFullPath = Cells(2, 2) & tempDirectoryName & "/" & directoryNameArray(j)   
  38.    
  39.     '复制文件   
  40.     FileCopy sourceFileFullPath, destinationFileFullPath   
  41. Next i   
  42. MsgBox "指定的文件目录已复制完毕"   
  43. GoTo finished   
  44. Exit Sub   
  45. errorflag:   
  46. MsgBox sourceFileFullPath & (i + 4)   
  47. finished:   
  48. End Sub   

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表