文件拷贝和记录传输值和运行时错误代码

上一篇 / 下一篇  2009-09-03 14:11:12 / 个人分类:VBScript脚本

On Error Resume Next
Const verwriteExisting = True    
Const logpath = "Sambathroughput.txt"    '定义Log文件位置和名字
Const Times = 2    '定义循环次数
Log()
writeLine("Title" & Now)    '在Log中写注释性文字
TextStreamText()
'set ws=wscript.createobject("wscript.shell")
'ws.run ".\call.bat"

'########################################################################
Sub TextStreamText
    Const ForReading =1
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse =0
    Dim fs,f,ts,s,a
    Set fs=CreateObject("Scripting.FileSystemObject")
    Set f=fs.GetFile("copypath.txt")    '定义读取信息的文件位置和名字
    Set ts=f.OpenAsTextStream(ForReading,TristateUseDefault)
    '获取文件中的Copy信息,执行copy任务
    Do While ts.AtEndOfLine <> True        
        s= ts.Readline()
        WriteLine(s)
        a=Split(s)
        copyfile a(0), a(1)
    Loop
    
    ts.close
End Sub

Function copyfile(resource,destination)        '执行文件拷贝函数
    On Error Resume Next
    Dim runstatus,i
    i=0
    Set bjcopy=CreateObject("scripting.FileSystemobject")
    Dim startime,endtime,totaltime,throughput_result,sum,file_size
    sum=0
   
    Do While i<times
        startime=Now
        fscopy=objcopy.CopyFile(resource,destination,OverwriteExisting)
        endtime=Now
        runstatus = Err        '获取VBS运行错误的代码
        If runstatus = 0 Then        '如果运行正常,计算Throughput,反之,把错误代码写入Log文件
            totaltime=DateDiff("S",startime,endtime)
            Set bjsize=objcopy.GetFile(resource)        '获取源文件Size
            file_size=objsize.Size
            throughput_result=file_size*8/1024/1024/totaltime
            writeLine(throughput_result & " Mb/s")
            writeLine("Success")
            sum=sum+throughput_result
            i=i+1
        Else
            writeLine("Fail" & " error code " & runstatus)    '把错误代码写入Log文件
            Exit Do
        End If
    Loop
   
    Dime average
    average=sum/i
    writeLine("average value:" & average)        '计算Throughput平均值
    writeLine("###########################################")
End Function

Sub writeLine(Content)        '执行写Log信息
    Set bjFSO=CreateObject("Scripting.filesystemobject")
    Set fs=objFSO.OpenTextFile(logpath,8,False)
    fs.WriteLine(content)
    fs.Close
    Set fs=Nothing
    Set bjFSO=nothing
End Sub

Sub createplaintxt()        '执行创建Log文件
    Set bjtxt=CreateObject("scripting.Filesystemobject")
    Set createtxt=objtxt.CreateTextFile(logpath,OverwriteExisting)
    createtxt.Close
    Set createtxt=Nothing
    Set bjtxt=Nothing
End Sub

Sub fileExist()            '判断Log文件是否存在
    Set fs=CreateObject("Scripting.FileSystemObject")
    If fs.FileExists(logpath) Then
        Exit Sub
    Else
        createplaintxt()
    End If
    Set fs=Nothing
End Sub

Sub Log()    '准备Log文件
    fileExist()
End Sub


附文本文件:
档名:copypath.txt
内容:
D:\x\x.exe Z:\x\  X文件上传到Z盘的速度
Z:\x\x.exe D:\temp\ X文件下载到本地PC的速度

TAG:

 

评分:0

我来说两句

Open Toolbar