如何在VBScript中写入文本文件而不覆盖?

时间:2012-06-20 16:48:08

标签: vbscript

我目前正在编写一个脚本,它从计算机解析ServiceTage,Computername和Username并将其输出到共享上的文本文件,但它似乎总是覆盖那里的数据而不是添加到它。

代码如下:

'Get Dell Service Tag Info
set ProSet = GetObject("winmgmts:").InstancesOf("Win32_BIOS")
Set ProSet1 = GetObject("winmgmts:").InstancesOf("Win32_SystemEnclosure")
For each Pro in ProSet
  For each Pro1 in ProSet1
    ServiceTag=Pro.SerialNumber
    exit for
  Next
  exit for
Next

'get username and computername, could also be asked in a batch
Set oShell     = WScript.CreateObject("WScript.Shell")
Set oShellEnv  = oShell.Environment("Process")
sComputerName  = oShellEnv("ComputerName")
sUsername      = oShellEnv("username")

dim filesys, filetxt, getname, path
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile("\\xx.xx.xx.xx\Support\DataLog.txt", 8, True)
path = filesys.GetAbsolutePathName("\\xx.xx.xx.xx\Support\DataLog.txt")
getname = filesys.GetFileName(path)
filetxt.WriteLine sUsername & ", " & sComputerName & ", " & ServiceTag
filetxt.Close

谢谢!

1 个答案:

答案 0 :(得分:1)

直接来自VBScript文档:

object.CreateTextFile(filename[, overwrite[, unicode]])

object.OpenTextFile(filename[, iomode[, create[, format]]])

从你的代码:

Set filetxt = filesys.CreateTextFile(filename, 8, True)

诊断:

使用.OpenTextFile()

参数调用.CreateTextFile()

处理:

使用.OpenTextFile():

Set filetxt = filesys.OpenTextFile(filename, 8, True)