2013-10-28 86 views
0

嘿,我這樣做直到將文本文件製作成字符串(「strnotapprovedData」)的循環,然後調用此函數來運行命令以刪除共享。我不斷收到所需的錯誤對象:「xxxx」。我該如何解決這是循環語句或字符串函數的問題。錯誤:需要的對象:'example'

功能:

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 
+0

我已經有一個[工作液]只要你(http://stackoverflow.com/a/19489784/1630171)。你爲什麼回到你的舊(非工作)代碼?如果你真正的問題是「如何斷開網絡驅動器?」:只需使用['RemoveNetworkDrive'](http://msdn.microsoft.com/en-us/library/d16d7wbf%28v=vs.84%29 .aspx)方法而不是脫殼。 –

+0

我在腳本的另一部分中使用了您的代碼,這是爲了獲取不在任一列表中的共享並運行刪除共享的命令行。 「網絡共享」共享名稱「/ DELETE」爲準確 –

回答

1

strnotapprovedData被設定爲字符串作爲文件系統對象的ReadAll方法的結果。由於它是一個字符串而不是流,因此它不具有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 
+0

回覆原始問題中的腳本。 –

+0

@ user2890980 - 我用一些代碼更新了我的答案以嘗試。 –

+0

謝謝你的循環工作很好,但我的功能的邏輯是真正的問題是。我用你的循環,但命令「網絡共享」行「/ DELETE」仍然不運行。你能幫我那 –

相關問題