gpt4 book ai didi

file - 在 VBScript 中每次 append 到文件而不是覆盖文件

转载 作者:行者123 更新时间:2023-12-02 01:08:32 25 4
gpt4 key购买 nike

我只是想在这里创建一个日志文件。我曾尝试使用 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"

最佳答案

研究 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 = oFS.OpenTextFile("C:\scripts\text.txt") ' Using defaults, no magiv number
' delete otp file here, if you want logs per session
Dim objFile : Set objFile = oFS.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"

关于file - 在 VBScript 中每次 append 到文件而不是覆盖文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19694180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com