5

I'm trying to write a VBScript (.vbs) script that uses the WScript.Shell Run() method, but it seems as though Run() can't find the file I'm passing in.

I've boiled my script down to the following code that will reproduce the results. This can be copied to a text editor, saved as test.vbs and ran. The type command simply outputs the text inside the file passed in.

Dim WShell
Set WShell = WScript.CreateObject("WScript.Shell")

WShell.Run("type C:\inetpub\wwwroot\iisstart.htm")

Set WShell = Nothing

If you were to run the code in Run() directly from the CMD prompt, it works fine. But when it's run from inside a .vbs script and using Run(), it gives me the following error:

Test.vbs(4, 1) (null): The system cannot find the file specified.

I can run other commands using Run() just fine, but when I try to pass in a path it fails. Exec() fails with the same error by the way. Any ideas?

1 Answer 1

10

Try this

Set oShell = CreateObject("WScript.Shell")

strCmd = "cmd /K type C:\inetpub\wwwroot\iisstart.htm"

oShell.Run(strCmd)

Set oShell = Nothing
Sign up to request clarification or add additional context in comments.

1 Comment

Use of cmd.exe .k is necessary because TYPE isn't an actual executable program but rather an internal command within the CMD Command Processor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.