Post from February, 2006

How To Set The Working Directory For WScript.Shell’s Run Method?

Tuesday, 14. February 2006 16:48

The Run method uses always the current working directory. Therefore, you have to change the directory if the command that will be executed with the Run method requires to be run from a specific directory.

theDir = "C:\WINDOWS"
theCmd = "%ComSpec% /K Echo Type EXIT to close this window."
Set objSh = WScript.CreateObject("WScript.Shell")
objSh.CurrentDirectory = theDir
objSh.Run theCmd

The Sub below, RunHere, executes a command in the specified working directory and then changes back to the original directory.

' -----------------------------------------------------
' ---- Sub RunHere(theCommandLine, theWorkingDirectory)
' -----------------------------------------------------

Sub RunHere(theCommandLine, theWorkingDirectory)

	Const WAIT = True

	Dim objSh, strPopd

	On Error Resume Next
	Set objSh = WScript.CreateObject("WScript.Shell")
	strPopd = objSh.CurrentDirectory
	objSh.CurrentDirectory = theWorkingDirectory
	objSh.Run theCommandLine, , WAIT
	objSh.CurrentDirectory = strPopd
	Set objSh = Nothing
	On Error GoTo 0

End Sub

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter