【问题标题】:Appending to a file instead of overwritting the file everytime in VBScript在 VBScript 中每次都附加到文件而不是覆盖文件
【发布时间】:2013-11-10 17:32:06
【问题描述】:

我只是想在这里创建一个日志文件。我尝试用OpenTextFile 而不是CreateTextFile 做一些事情,但它什么也没写,我真的不知道为什么而且我找不到我需要的信息。

'Nick Repella 10/29/13

'Needed in case object does not exist (outdated list)
On Error Resume Next

Function IsCompDisabled(strLine)
    Dim objComputer
    objComputer = "LDAP://cn="
    objComputer = objComputer & strLine
    objComputer = objComputer & ",ou=HIDDENOU,dc=HIDDENDC,dc=HIDDENDC,dc=HIDDENDC"
    IsCompDisabled = GetObject(objComputer).AccountDisabled
End Function 

'Set the file to read computer names from (Change C:\scripts\text.txt to the 
'target file)
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\scripts\text.txt", 1)

Dim strLine

Do While Not objFileToRead.AtEndOfStream
    strLine = objFileToRead.ReadLine()
    If (IsCompDisabled(strLine) = True) Then
        outFile="c:\scripts\compDisableCheck.log"
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.CreateTextFile(outFile, True)
        objFile.Write strLine & "has been deleted"
        objFile.Close        
    Else
        WScript.Echo strLine & " computer is enabled no action taken"
    End If
Loop

MsgBox "Done"

【问题讨论】:

    标签: file vbscript append overwrite


    【解决方案1】:

    仔细研究docs。定义ForAppending, .OpenTextfile(sFSpec, ForAppending, True) 之前的输出文件,循环结束后关闭。

    (未测试)代码:

    Option Explicit
    
    Const ForAppending = 8
    
    'No global OERN
    
    'Function to tell if the computer is disabled
    Function IsCompDisabled( strLine )
        ' type prefix fraud!
        Dim objComputer
        objComputer = "LDAP://cn="
        objComputer = objComputer & strLine
        objComputer = objComputer & ",ou=HIDDENOU,dc=HIDDENDC,dc=HIDDENDC,dc=HIDDENDC"
      ' Needed  H E R E  in case object does not exist (outdated list)
      On Error Resume Next
        IsCompDisabled = GetObject(objComputer).AccountDisabled
        ' should be logged; pass otp file as parameter
      On Error GoTo 0
    End Function
    
    'Set the file to read computer names from (Change C:\scripts\text.txt to the target file)
    Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
    Dim objFileToRead : Set objFileToRead = goFS.OpenTextFile("C:\scripts\text.txt") ' Using defaults, no magiv number
    ' delete otp file here, if you want logs per session
    Dim objFile : Set objFile = goFS.OpenTextFile("c:\scripts\compDisableCheck.log", ForAppending, True)
    'objFile[ToAppend] ?
    
    Dim strLine
    
    'Read from file until end of file
    'If computer disabled say so / If computer enabled say so
    Do Until objFileToRead.AtEndOfStream ' not while not
        strLine = objFileToRead.ReadLine()
        If IsCompDisabled(strLine) Then ' no camparison against boolean literals
           ' timestamp?
           objFile.WriteLine strLine & " has been deleted"
        Else
           ' ? objFile.WriteLine ...
           WScript.Echo strLine & " computer is enabled, no action taken"
        End If
    Loop
    objFile.Close
    objFileToRead.Close
    
    MsgBox "Done"
    

    【讨论】:

    • 您在声明的常量 ForAppendig 上缺少“n”... :)
    • @BHart - 谢谢。固定。
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2016-04-04
    • 2016-06-04
    • 2011-11-08
    • 2018-09-15
    相关资源
    最近更新 更多