0

I have this script and I need it to finish (auto-end or autokill itself and also close wscript.exe) after launching the .bat

On Error Resume next
If WScript.Arguments.Named.Exists("elevated") = False Then
      'Launch the script again as administrator
       CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
       WScript.Quit
Else
      'Change the working directory from the system32 folder back to the script's folder.
      Set oShell = CreateObject("WScript.Shell")
      oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

End If

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )

SCRIPT = homedrive & "\test\bar.bat"
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")

objshell.Run (script),0,True

Failed attempts:

  • I tried with this code and it did not work
  • WScript.Quit (at the end of the script) and it did not work
  • Change "true" to "False" and it did not work

Any ideas?

Update: The main problem is that wscript.exe remains running after launching the .vbs (it can be seen in the task manager) and i can not use cmd commands inside the .vbs or inside .bat (taskkill /f /im "wscript.exe" /t) because the .bat and the .vbs are closed

If my question has no answer: I would appreciate someone telling me how to launch a .bat with a method that Windows recognizes natively (without installing dependencies). Please exclude the following methods:

  1. launch .bat directly
  2. Launch .bat with a shortcut
  3. Convert .bat to .exe
  4. Launch .bat with .vbs
7
  • How are you starting the script? Commented Feb 28, 2019 at 14:17
  • @Appleoddity ?. double click on the .vbs
    – acgbox
    Commented Feb 28, 2019 at 14:24
  • I don’t understand the purpose of your script. The way it is written it appears like it won’t function as you intend. A large portion of the script actually does absolutely nothing. This is what I’m talking about with Sanity checks. Get rid of all the code except the 4 lines responsible for running the batch file. I guarantee you wscript will end. It is obvious your code is opening another instance of wscript or dieing for some reason. But it is NOT because of the batch file. This is part of debugging your code. You’ve been given the answer specific to your question. Commented Feb 28, 2019 at 16:15
  • What the .vbs does is: 1. make sure that it runs with privileges 2. it calls the .bat in its "path of homedrive". 3. (without waiting) it must close the .vbs and its interpreter wscript.exe (this is the part doesn't work)
    – acgbox
    Commented Feb 28, 2019 at 16:20
  • I understand the script. I’m saying it doesn’t function like you expect and a large portion of the code does actually nothing. The code is buggy and that is your problem. It is NOT a problem of wscript not finishing after running the .bat. I told you how to solve that issue. Commented Feb 28, 2019 at 16:21

2 Answers 2

1

Try this:

On Error Resume next
Set fso = CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )

SCRIPT = homedrive & "\test\bar.bat"

If WScript.Arguments.Named.Exists("elevated") = False Then
    'Launch the script again as administrator
    CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
    WScript.Quit
Else
    'Change the working directory from the system32 folder back to the script's folder.
    objShell.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
End If

objshell.Run (script),0,False
0
1

Change this line:

objshell.Run (script),1,True

To:

objshell.Run (script),1,False

This tells VB to not wait for the command to return.

https://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp

4
  • sorry. It does not work. It does not close the vbs or wscript.exe.
    – acgbox
    Commented Feb 28, 2019 at 13:11
  • are you sure? can you update your question with an annotated screenshot or recording that demonstrates the problem?
    – Attie
    Commented Feb 28, 2019 at 13:16
  • That’s is what it does. I’ve written oodles of vb scripts. Setting ‘wait’ to false will prevent vb script from blocking while the command runs. You’re missing something or we don’t have all the info. Have you confirmed the batch file is actually running? Have you out any sanity checks in? For instance wscript.echo “I’m here.” right after or before the objshell.run so you know if the script actually moved beyond that. Commented Feb 28, 2019 at 13:18
  • I have added an update, where the problem is described more exactly.
    – acgbox
    Commented Feb 28, 2019 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .