vbscript优化:如何更快地写入文件 [英] vbscript optimization : how to get faster file writing

查看:85
本文介绍了vbscript优化:如何更快地写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在许多vbscript中使用的常用日志功能,并对其进行了相应的修改.我觉得它写得太慢了.我有3个问题:

The following is the usual log function I utilize in alot of my vbscripts which I modify accordingly. I feel it writes too slow. I got 3 questions:

  1. 是否有关于如何优化此方法的想法,以便使其更快地写入?
  2. 首先将所有文本存储在字符串中然后运行OutputToLog函数会更快吗?还是每次我需要在文本文件中插入字符串时执行OutputToLog都会更快?
  3. 如果不是驱动器空间的原因,那么在执行过程中写入文本文件时是否可能会耗尽内存...导致脚本执行的速度越来越慢?

这是我的vbscript函数

Here is my vbscript function

Function OutputToLog (strToAdd)  
    Dim strDirectory,strFile,strText, objFile,objFolder,objTextFile,objFSO
    strDirectory = "c:\log"
    strFile = "\log-"& StampNow &  ".bat"
    'strText = "test"
    strText = strToAdd

    ' Create the File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFolder = objFSO.CreateFolder(strDirectory)
       'WScript.Echo "Just created " & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
       'Wscript.Echo "Just created " & strDirectory & strFile
    End If

    set objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 8

    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)

    ' Writes strText every time you run this VBScript
    objTextFile.WriteLine(strText)
    objTextFile.Close
End Function

预先感谢

推荐答案

我想您既要创建FSO对象,又要在OutputToLog函数外部打开日志文件.它可能不会节省很多时间,但是为什么每次写操作都创建对象,打开和关闭文件?

I think you'd want to both create your FSO objects and open your log file outside the the OutputToLog function. It may not save much time, but why create objects, open and close files with every write?

否则,如果您希望保持功能不变,那么只需执行一次写操作就应该更快.

Otherwise if you want to keep the functions as is, doing just one write should be quicker.

这篇关于vbscript优化:如何更快地写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆