0

Hey I have this do until loop that takes a text file made into a string ("strnotapprovedData") and then calls this function that runs a command to delete the share. I keep getting the error object required: "xxxx". How do I fix this is the problem the function the loop statement or the string.

Function:

Function DeleteThisShare(Value)

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

End Function

Loop Statement:

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

Loop

String:

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

Reply to Chris Nielsen

Well I added this and the same problem still occurs

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 に答える