2013-06-12 67 views
0

這是我正在嘗試做的事情。編寫文件的VBS問題

該腳本一次讀取一個計算機名稱的文本文件。

檢查是否存在program.lnk。 如果鏈接存在,它將複製到該計算機/用戶配置文件的新鏈接。 然後在新的文本文件中記錄計算機名稱和程序存在。 然後刪除原始的program.lnk文件。

如果program.lnk不存在,它會將相同的文本文件記錄到計算機名稱中,並且該程序不存在。

scipt然後移動到列表中的下一個計算機名稱。

我在創建ComputerListModified.txt文件時遇到問題,如果該文件不存在但附加該文件,如果它存在。

這裏是我的代碼....

InputFile = "C:\scripts\computers.txt" 
Const DeleteReadOnly = True 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile(InputFile) 
Do While Not (objFile.AtEndOfStream) 
strComputer = objFile.ReadLine 
For Each objsubfolder In objFSO.GetFolder("\\" & strComputer & "\c$\Documents and Settings\").subfolders 

If objFSO.FileExists(objsubfolder.Path & "\desktop\Wellcore Operations.LNK") Then 
objFSO.CopyFile "\\pc01130\c$\scripts\Wellcore OperationsReadOnly.lnk", (objsubfolder.Path & "\desktop\") 
outFile="c:\scripts\ComputerListModified.txt" 
Set objFile = objFSO.CreateTextFile(outFile,True) 
objFile.Write strComputer: objFile.Write " This Computer had Wellcore Operations" & vbCrLf 
objFile.Close 

Else 

outFile="c:\scripts\ComputerListModified.txt" 
Set objFile = objFSO.CreateTextFile(outFile,True) 
objFile.Write strComputer: objFile.Write " This Computer did not have Wellcore Operations" & vbCrLf 
objFile.Close 

End If 

If objFSO.FileExists(objsubfolder.Path & "\desktop\Wellcore Operations.LNK") Then 
objFSO.DeleteFile (objsubfolder.Path & "\desktop\Wellcore Operations.LNK") 

End If 
Next 
Loop 

MsgBox "Done" 

回答

1

如果你想在一個(深度嵌套)重複的動作來登錄相關的事實,你必須寫在文件中您最內層的循環。但準備(和關閉)文件是一項頂級任務,不應在循環中完成。

蓋茨先生沒有正確設計.OpenTextFile方法。它可以在沒有任何.FileExists檢查的情況下使用。

演示代碼:

Option Explicit 

Const ForAppending = 8 

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject") 
Dim gsLog : gsLog  = ".\logdemo.log" 
WScript.Echo gsLog, "exists:", CStr(goFS.FileExists(gsLog)) 
'       .OpenTextFile(filename[, iomode[, create[, format]]]) 
Dim goLog : Set goLog = goFS.OpenTextFile(gsLog, ForAppending, True) 
goLog.WriteLine Now & " start" 

Dim sComputer 
For Each sComputer In Split("alpha beta gamma") 
    Dim sFolder 
    For Each sFolder in Split("A B C") 
     goLog.WriteLine Join(Array(Now, sComputer, sFolder)) 
    Next 
Next 

goLog.WriteLine Now & " end" 
goLog.WriteLine "-------------------------" 
goLog.Close 

輸出:

cscript logdemo.vbs 
.\logdemo.log exists: False 

type logdemo.log 
6/12/2013 9:25:26 PM start 
6/12/2013 9:25:26 PM alpha A 
6/12/2013 9:25:26 PM alpha B 
6/12/2013 9:25:26 PM alpha C 
6/12/2013 9:25:26 PM beta A 
6/12/2013 9:25:26 PM beta B 
6/12/2013 9:25:26 PM beta C 
6/12/2013 9:25:26 PM gamma A 
6/12/2013 9:25:26 PM gamma B 
6/12/2013 9:25:26 PM gamma C 
6/12/2013 9:25:26 PM end 
------------------------- 

cscript logdemo.vbs 
.\logdemo.log exists: True 

type logdemo.log 
6/12/2013 9:25:26 PM start 
6/12/2013 9:25:26 PM alpha A 
6/12/2013 9:25:26 PM alpha B 
6/12/2013 9:25:26 PM alpha C 
6/12/2013 9:25:26 PM beta A 
6/12/2013 9:25:26 PM beta B 
6/12/2013 9:25:26 PM beta C 
6/12/2013 9:25:26 PM gamma A 
6/12/2013 9:25:26 PM gamma B 
6/12/2013 9:25:26 PM gamma C 
6/12/2013 9:25:26 PM end 
------------------------- 
6/12/2013 9:26:00 PM start 
6/12/2013 9:26:00 PM alpha A 
6/12/2013 9:26:00 PM alpha B 
6/12/2013 9:26:00 PM alpha C 
6/12/2013 9:26:00 PM beta A 
6/12/2013 9:26:00 PM beta B 
6/12/2013 9:26:00 PM beta C 
6/12/2013 9:26:00 PM gamma A 
6/12/2013 9:26:00 PM gamma B 
6/12/2013 9:26:00 PM gamma C 
6/12/2013 9:26:00 PM end 
------------------------- 
+0

謝謝你幫助我。我能夠看到你在這裏做什麼,並能夠使它適合我的使用。 –