在 VBScript 中每次都附加到文件而不是覆盖文件 [英] Appending to a file instead of overwritting the file everytime in VBScript

查看:18
本文介绍了在 VBScript 中每次都附加到文件而不是覆盖文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在这里制作一个日志文件.我试过用 OpenTextFile 而不是 CreateTextFile 做一些事情,但后来它什么也没写,我真的不知道为什么,我找不到我需要的信息.

I am just trying to make a log file here. I have tried doing something with OpenTextFile instead of CreateTextFile, but then it just writes nothing and I'm really not sure why and I'm having trouble finding the info I need.

'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"

推荐答案

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

Study the docs carefully. Define ForAppending, .OpenTextfile(sFSpec, ForAppending, True) the output file before, and close it after the loop.

(未测试)代码:

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"

这篇关于在 VBScript 中每次都附加到文件而不是覆盖文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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