timber
timber

Reputation: 21

vbs run command no output

Okay I created a small script to call "net file" and pump the output to a file. There are no spaces in the filenames, and everything seems okay when I run it interactively. So I run the following and I get no results and no errors:

set oWShell = CreateObject("WScript.Shell")
owshell.run "net file > C:\openfiles.txt"
Set owShell = nothing

Now if modify this just slightly to show execute the same command (and keep my command window open) it works just as expected (except it keeps the cmd window open which I can't have)

set oWShell = CreateObject("WScript.Shell")
owshell.run "%comspec% /k net file > C:\openfiles.txt"
Set owShell = nothing

It must be something obvious that I'm just missing. I only touch vbs once in a blue moon so it isn't something that I'm that used to using.

Any help would be appreciated! Thanks in advance!

Upvotes: 1

Views: 3475

Answers (2)

CurtisJC
CurtisJC

Reputation: 690

Have a look at Run. Changing

owshell.run "%comspec% /k net file > C:\openfiles.txt"

to

owshell.run "%comspec% /c net file > C:\openfiles.txt", 0, FALSE

This will hide the cmd and carry on with the rest of the script... If you want to wait for the command to finish, change FALSE to TRUE.

Also keep in mind that some machines are funny about letting you write files directly to C:\. Possibly create a test folder and write to there instead!

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

You need a shell (i.e. %comspec%) to get shell features like redirection; the persisten window is what you asked for: the /k lets the shell stay open (try /c instead) and you should use the second and third parameter of the .Run method to get a hidden window (and possibly wait for the process before you zap the owShell).

Upvotes: 2

Related Questions