0

I have this script (courtesy of @WesternSage) that renames foo.txt to foo.bat, launches foo.bat, and when foo.bat ends, renames it back to foo.txt.

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"

SCRIPT = "foo.bat"
Set objShell = CreateObject("WScript.Shell")
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 "%COMSPEC% /c " & NewPath, 1, True

' Changes start here
'===================================================================

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

' Hold execution until cmd.exe process is done
Do 
    ' Get cmd.exe processes
    Set colProcessList = objWMIService.ExecQuery _
        ("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
    WScript.Sleep 250
Loop While colProcessList.Count > 0

Fso.MoveFile "foo.bat", "foo.txt"

The problem is:

foo.txt (foo.bat) is in a path, which can change depending on the Windows version. For this I need to use environment variables to set the foo.txt path (example: %homedrive%), but this change doesn't work.

SCRIPT = "%homedrive%\test\foo.bat"

I need to call a second batch (bar.bat), when the first one ends (foo.bat). But this change does not work at the end of .vbs.

Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "%homedrive%\test\bar.txt", "%homedrive%\test\bar.bat"

SCRIPT = "%homedrive%\test\bar.bat"
Set objShell = CreateObject("WScript.Shell")
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 "%COMSPEC% /c " & NewPath, 1, True

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

' Hold execution until cmd.exe process is done
Do 
    ' Get cmd.exe processes
    Set colProcessList = objWMIService.ExecQuery _
        ("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
    WScript.Sleep 250
Loop While colProcessList.Count > 0

Fso.MoveFile "%homedrive%\test\bar.bat", "%homedrive%\test\bar.txt"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
acgbox
  • 312
  • 2
  • 13
  • You need to [return an environmental variable](https://ss64.com/vb/env.html). – Squashman Feb 25 '19 at 20:58
  • Or [expand an environmental variable](https://ss64.com/vb/envexpand.html). You can search SO and find many examples of doing both. – Squashman Feb 25 '19 at 21:06
  • @Squashman I am not an expert on vbs. If I knew how to do it I would not have asked the question. I have already read about it (https://www.robvanderwoude.com/vbstech_data_environment.php), but I do not know how to apply it. Could you be more specific and set an example in my case? Thank you – acgbox Feb 25 '19 at 21:39
  • The `Run` method recognizes environment variables, but FileSystemObject methods do not, therefore you must expand the environment variable before using the variable with the script path. – Ansgar Wiechers Feb 25 '19 at 22:08
  • If you read that page from Rob, then I would think you would have at least attempted, `homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )`. You have so much more complicated Vbscript written in your code, the most simple thing to do in any language is to assign something to a variable. – Squashman Feb 25 '19 at 22:12
  • @AnsgarWiechers The question has two components. You have marked the first component as a duplicate. And the second? (will you also mark it as a duplicate?) – acgbox Feb 25 '19 at 22:45
  • Why? You're not supposed to cram two questions into one in the first place. And it's not even clear to me what that second problem is. Please post a new question with a *lot* more information about what "but this change does not work at the end of .vbs" actually means. – Ansgar Wiechers Feb 25 '19 at 23:38
  • @AnsgarWiechers The question is about applying environment variable to a path ... and also about launching 2 batch in sequence with a vbs script. The question is clear. But if you still want to follow marking as a duplicate, I guess you're the one in charge and nothing I can do about it. – acgbox Feb 26 '19 at 00:02

1 Answers1

1

This should works:

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

homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )
Fso.MoveFile homedrive & "\test\bar.txt", homedrive & "\test\bar.bat"

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),1,True

Fso.MoveFile homedrive & "\test\bar.bat", homedrive & "\test\bar.txt"
Danfossi
  • 171
  • 1
  • 2
  • 13
  • Please review. The environment variable works. Maybe there is an error in its implementation (Not call .bat: SCRIPT = homedrive & "\test\foo.bat") – acgbox Feb 25 '19 at 22:22
  • Now should works – Danfossi Feb 26 '19 at 11:23
  • A problem: I need .vbs to end (and close wscript.exe) when it launch the .bat. How do you do that? – acgbox Feb 28 '19 at 00:12
  • The True value at the end of the line `objshell.Run (script),1,True` is what indicates that Run() should wait for the process to finish so don't need the WMI-based wait() function. The wscript process terminates when the batch file is finished, otherwise it could not rename the .bat to .txt – Danfossi Feb 28 '19 at 09:57
  • i got it. thanks a lot. pd: By the way. I have another similar question. I appreciate your time if you can read it. https://superuser.com/questions/1410033/autoend-vbs-when-it-runs-bat – acgbox Feb 28 '19 at 16:17