code_junkie

vbscript optimization : how to get faster file writing

Nov 13th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. Function OutputToLog (strToAdd)
  2. Dim strDirectory,strFile,strText, objFile,objFolder,objTextFile,objFSO
  3. strDirectory = "c:log"
  4. strFile = "log-"& StampNow & ".bat"
  5. 'strText = "test"
  6. strText = strToAdd
  7.  
  8. ' Create the File System Object
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")
  10.  
  11. ' Check that the strDirectory folder exists
  12. If objFSO.FolderExists(strDirectory) Then
  13. Set objFolder = objFSO.GetFolder(strDirectory)
  14. Else
  15. Set objFolder = objFSO.CreateFolder(strDirectory)
  16. 'WScript.Echo "Just created " & strDirectory
  17. End If
  18.  
  19. If objFSO.FileExists(strDirectory & strFile) Then
  20. Set objFolder = objFSO.GetFolder(strDirectory)
  21. Else
  22. Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
  23. 'Wscript.Echo "Just created " & strDirectory & strFile
  24. End If
  25.  
  26. set objFile = nothing
  27. set objFolder = nothing
  28. ' OpenTextFile Method needs a Const value
  29. ' ForAppending = 8 ForReading = 1, ForWriting = 2
  30. Const ForAppending = 8
  31.  
  32. Set objTextFile = objFSO.OpenTextFile _
  33. (strDirectory & strFile, ForAppending, True)
  34.  
  35. ' Writes strText every time you run this VBScript
  36. objTextFile.WriteLine(strText)
  37. objTextFile.Close
  38. End Function
  39.  
  40. Dim loggerFSO
  41.  
  42. Function PrepLog
  43. dim objFolder
  44. ' Create the File System Object
  45. if loggerFSO is nothing then Set loggerFSO = CreateObject("Scripting.FileSystemObject")
  46.  
  47. ' Check that the strDirectory folder exists
  48. If loggerFSO.FolderExists(strDirectory) Then
  49. Set objFolder = loggerFSO.GetFolder(strDirectory)
  50. Else
  51. Set objFolder = loggerFSO.CreateFolder(strDirectory)
  52. 'WScript.Echo "Just created " & strDirectory
  53. End If
  54.  
  55. If loggerFSO.FileExists(strDirectory & strFile) Then
  56. ' do nothing
  57. Else
  58. loggerFSO.CreateTextFile(strDirectory & strFile)
  59. 'Wscript.Echo "Just created " & strDirectory & strFile
  60. End If
  61. End function
  62.  
  63. Function OutputToLog (strToAdd)
  64. Dim strDirectory,strFile,strText, objTextFile
  65. strDirectory = "c:log"
  66. strFile = "log-"& StampNow & ".bat"
  67. 'strText = "test"
  68. strText = strToAdd
  69.  
  70. ' Create the File System Object
  71. if loggerFSO is nothing then Set loggerFSO = CreateObject("Scripting.FileSystemObject")
  72.  
  73. ' OpenTextFile Method needs a Const value
  74. ' ForAppending = 8 ForReading = 1, ForWriting = 2
  75. Const ForAppending = 8
  76.  
  77. Set objTextFile = loggerFSO.OpenTextFile _
  78. (strDirectory & strFile, ForAppending, True)
  79.  
  80. ' Writes strText every time you run this VBScript
  81. objTextFile.WriteLine(strText)
  82. objTextFile.Close
  83. End Function
  84.  
  85. Dim OutputToLogFileObject
  86. Function OutputToLog (strToAdd)
  87. Dim strDirectory,strFile,strText, objFile,objFolder,objTextFile,objFSO
  88. If IsEmpty(OutputToLogFileObject) Then
  89. strDirectory = "c:log"
  90. strFile = "log-"& StampNow & ".bat"
  91. 'strText = "test"
  92. strText = strToAdd
  93.  
  94. ' Create the File System Object
  95. Set objFSO = CreateObject("Scripting.FileSystemObject")
  96.  
  97. ' Check that the strDirectory folder exists
  98. If objFSO.FolderExists(strDirectory) Then
  99. Set objFolder = objFSO.GetFolder(strDirectory)
  100. Else
  101. Set objFolder = objFSO.CreateFolder(strDirectory)
  102. 'WScript.Echo "Just created " & strDirectory
  103. End If
  104.  
  105. If objFSO.FileExists(strDirectory & strFile) Then
  106. Set objFolder = objFSO.GetFolder(strDirectory)
  107. Else
  108. Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
  109. 'Wscript.Echo "Just created " & strDirectory & strFile
  110. End If
  111.  
  112. set objFile = nothing
  113. set objFolder = nothing
  114. ' OpenTextFile Method needs a Const value
  115. ' ForAppending = 8 ForReading = 1, ForWriting = 2
  116. Const ForAppending = 8
  117.  
  118. Set OutputToLogFileObject = objFSO.OpenTextFile _
  119. (strDirectory & strFile, ForAppending, True)
  120. End If
  121. OutputToLogFileObject.WriteLine strText
  122. End Function
Add Comment
Please, Sign In to add comment