0

嘿,我一直这样做,直到循环将文本文件转换为字符串(“strnotapprovedData”),然后调用此函数来运行命令以删除共享。我不断收到所需的错误对象:“xxxx”。我该如何解决这个问题是循环语句或字符串的函数。

功能:

Function DeleteThisShare(Value)

   DeleteThisShare = "net share " & Share & " \DELETE"
    Set objShell = CreateObject("Wscript.Shell")
    Msgbox AddQuotes(DeleteThisShare)
    objShell.Run DeleteThisShare

End Function

循环语句:

Do Until strnotapprovedData.AtEndOfStream
Dim objShare : objShare = Split(strnotapprovedData,vbCrLf)
    notapprovedShares = objShare
    DeleteThisShare(notapprovedShares)

Loop

细绳:

Dim notapprovedList, notapprovedShares
Set notapprovedList = objFSo.OpenTextFile ("C:\Users\abro\Shares_Not_Approved.txt")
Dim strnotapprovedFile, strnotapprovedData
strnotapprovedFile = ("C:\Users\abro\Shares_Not_Approved.txt")
strnotapprovedData = objFSO.OpenTextFile(strnotapprovedFile,ForReading).ReadAll

回复克里斯尼尔森

那么我添加了这个,同样的问题仍然存在

strnotapprovedData = objFSO.OpenTextFile(strnotapprovedFile,ForReading).ReadAll

Do Until objnotapprovedLines.AtEndOfStream
    objnotapprovedLines = Split(Trim(strnotapprovedData),vbCrLf)
    DeleteThisShare(objnotapprovedLines)

Loop
4

1 回答 1

1

strnotapprovedDataReadAll作为文件系统对象的方法的结果设置为字符串。由于它是字符串而不是流,因此它没有AtEndOfStream属性。相反,您应该在换行符处拆分它并遍历结果数组。


回复编辑:

您的代码没有显示在哪里objnotapprovedLines定义或初始化。根据您的用法,我认为它以流的形式开始生活,但我无法知道这一点。但是,在您之后的第一行DO,您将其覆盖为一个数组。数组没有AtEndOfStream属性,因此即使没有其他属性也肯定会导致错误。

这是一些未经测试的代码尝试:

' Define a new variable called "notApprovedLines"
' VBScript is loosely-typed, so this could be anything at this point.
Dim notApprovedLines

' Read the contents of the file into the "notApprovedLines" variable.
' This assumes that you have a variable named "strnotapprovedFile" that
' contains the path to the file, as your example code indicates.  At the
' end of this, the "notApprovedLines" variable contains the entire contents
' of the file as one giant string.
notApprovedLines = objFSO.OpenTextFile(strnotapprovedFile, ForReading).ReadAll

' Split the contents of the file into an array, so we can deal with one line at
' a time. After this, "notApprovedLines" will be an array of strings, with each
' entry representing one line of the original file.
notApprovedLines = Split(notApprovedLines, vbCrLf)

' Loop over those lines
Dim x, k, line

' This sets the upper boundary of the loop.
k = uBound(notApprovedLines)

' In VBScript, arrays start at zero.  The "x" is our counter variable. Its
' value will be incremented with each iteration of the loop, so we hit the
' entries one at a time.
For x = 0 To k

    ' Trim whitespace away from each line.  After this executes, the "line"
    ' variable will contain a single line from the file, as a string, without
    ' and leading or trailing whitespace.
    line = Trim(notApprovedLines(x))

    ' We don't want to process blank lines, if any exist.  This test lets us
    ' skip those.  Any line that contained only whitespace would also be
    ' skipped here, since it's length would be zero after trimming away the
    ' whitespace.
    If Len(line) > 0 Then

        ' This executes your function. I have not really proofed it very
        ' closely. Let's hope it works! =)
        DeleteThisShare(line)

    End If
Next
于 2013-10-28T21:45:52.717 回答