Thiago
Thiago

Reputation: 335

How to create and execute a batch file in vbscript?

I tried to do it this way:

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

that was to work but of an error saying "The file is already being used by another process"

Upvotes: 0

Views: 1864

Answers (2)

Serenity
Serenity

Reputation: 184

Terminates paint directly in 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

Upvotes: 0

JosefZ
JosefZ

Reputation: 30103

The file is already being used by your own cscript or wscript process. You should use outFile.Close (and maybe moreover Set outFile = Nothing) before run.

Upvotes: 1

Related Questions