2015-04-06 108 views
0

我試圖做這樣說:如何在vbscript中創建和執行批處理文件?

Dim objFSO, outFile, wshShell 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set outFile = objFSO.CreateTextFile("paint.bat", True) 
outFile.WriteLine "taskkill /f /im mspaint.exe" 
Set wshShell = CreateObject("WScript.Shell") 
wshShell.Run "paint.bat", 0, false 

,這是工作,但一個錯誤,說「文件已被另一個進程使用」

回答

0

該文件已在使用自己的cscriptwscript過程。您應該在run之前使用outFile.Close(也可能使用Set outFile = Nothing)。

0

直接以vbscript結束塗料。

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process") 
For Each objItem in colItems 
    If objitem.Name = "mspaint.exe" Then 
     msgbox objitem.name & " PID=" & objItem.ProcessID & " SessionID=" & objitem.sessionid 
     objitem.terminate 
    End If 
Next