vbs 之操作文件函数集锦

上一篇 / 下一篇  2016-07-08 16:24:38 / 个人分类:QTP自动化测试

vbs 之操作文件函数集锦:
Rem ===============================================================
Rem 功能:创建文件夹
Rem  
Rem 参数说明:创建文件夹的路径
Rem 
===============================================================
Function CreateFolderDemo(FolderPath)

Rem 声明变量
Dim Fso

Rem 创建对象
Set Fso = CreateObject("Scripting.FileSystemObject")

Rem 如果指定文件夹不存在,则创建
If Fso.FolderExists(FolderPath) = False Then

    Fso.CreateFolder(FolderPath)
    
End If
wait 1

Rem 释放对象
Set Fso = Nothing

End Function

Rem ===============================================================
Rem 功能:操作文件或文件夹
Rem  
Rem 参数说明:1、文件路径,2、操作类型:0、判断指定的文件是否存在,1、删除指定的文件,2、判断指定的文件夹是否存在 ,3、创建指定的文件夹,4、删除指定文件夹 ,5移动指定的文件到指定的文件夹路径
Rem 
===============================================================

Function FileOperater(FileOrFolderPath,OpType)

   Rem 声明变量
   Dim Fso
   Dim PathArray
   Dim SubIndex
   Dim Iterator
   Dim TempPath
   Dim FolderObj
   Dim FolerCount

   Set Fso = CreateObject("Scripting.FileSystemObject")

   Rem 根据操作类型,进行相应操作
   Select Case OpType
 
    Rem 验证指定的文件是否存在,并返回给函数
     Case 0
        FileOperater = Fso.FileExists(FileOrFolderPath)

    Rem 删除指定的文件
    Case 1
        Fso.DeleteFile(FileOrFolderPath)

    Rem 判断指定的文件夹是否存在,并返回判断结果
    Case 2
        FileOperater = Fso.FolderExists(FileOrFolderPath)

    Rem 创建指定的文件夹
    Case 3

        Rem 将路径分组
        PathArray = Split(FileOrFolderPath,"\",-1,1)

        Rem 获取数组最大下标
        SubIndex = Ubound(PathArray)

        Rem 暂存路径
        TempPath =  PathArray(0)

        Rem 创建路径
        For Iterator = 1 To SubIndex

            If Fso.FolderExists(TempPath & "\" &  PathArray(Iterator)) = False Then

                Fso.CreateFolder TempPath & "\" & PathArray(Iterator)

                TempPath = TempPath & "\" & PathArray(Iterator)

            Else

                TempPath = TempPath & "\" & PathArray(Iterator)

            End If

        Next

    Rem 删除指定文件夹
    Case 4
        Fso.DeleteFolder FileOrFolderPath

    Rem 移动指定的文件到指定的文件夹路径
    Case 5

        Rem 复制备份文件日志
        Fso.CopyFile FileOrFolderPath,"D:\log\" & Date
        Wscript.Sleep 5000

    Rem 移动指定的文件夹到指定的文件夹路径
    Case 6

        Rem 复制备份文件夹日志
        Fso.CopyFolder FileOrFolderPath,"D:\log\"  & Date
        Wscript.Sleep 5000

    Rem 返回指定路径下的文件数
    Case 7

        Set FolderObj = Fso.GetFolder(FileOrFolderPath)
        FileCount = FolderObj.Files.Count
        FileOperater = FileCount

    Rem 返回指定路径下的文件夹数
    Case 8

        Set FolderObj = Fso.GetFolder(FileOrFolderPath)
        FolderCount = FolderObj.SubFolders.Count
        FileOperater = FolderCount

   End Select


   Rem 释放创建的对象
   Set Fso = Nothing

End Function

TAG:

 

评分:0

我来说两句

Open Toolbar