[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

capture output of du.exe in vbscript

Posted on 2011-09-28
7
Medium Priority
?
919 Views
Last Modified: 2012-05-12
I need to invoke a sysinternals utility(disk usage) from one of my scripts, but I can't get it to return anything

Dim strCommand, objShell, objExec 
strCommand = "%comspec% /k C:\Windows\System32\du.exe"
Set objShell = CreateObject("WScript.Shell") 
Set objExec = objShell.Exec(strCommand) 
WScript.Echo objExec.StdOut.ReadAll 
WScript.Echo objExec.Status

Open in new window


if I trace the execution step by step I can see it opens an empty command window, but nothing from the output is displayed.

how can I capture the output of du?

 windows during execution
0
Comment
Question by:ee-gd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 36719121
try this:
strCommand = "%comspec% /k C:\Windows\System32\du.exe > C:\myoutput.txt"
Set objShell = CreateObject("WScript.Shell") 
objShell.Run strCommand 

Open "C:\myoutput.txt" For Binary Access Read As #fn
buf = Space$(LOF(fn))
Get #fn, , buf
Close #fn

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 36719128
You have to run it once manually from Windows to accept the license agreement.  Then you can run it from the command line with a valid directory name as a parameter.  Something like this:

strCommand = "%comspec% /k C:\Windows\System32\du.exe c:\temp"
0
 
LVL 4

Expert Comment

by:GeoffHarper
ID: 36719257
Sorry about the VB6 code; to display the contents of the file:
strCommand = "%comspec% /k C:\Windows\System32\du.exe > C:\myoutput.txt"
Set objShell = CreateObject("WScript.Shell") 
objShell.Run strCommand 

Set Objtxtfile = Server.CreateObject("Scripting.FileSystemObject")
Set Readtxtfile= Objtxtfile.OpenTextFile ("C:\myoutput.txt", 1, True)
If Not Readtxtfile.AtEndOfStream Then
    buf = Trim(Readtxtfile.ReadLine)
End If
Set Readtxtfile = Nothing
MsgBox buf

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Assisted Solution

by:Bill Prew
Bill Prew earned 800 total points
ID: 36719526
Change this line:

strCommand = "%comspec% /k C:\Windows\System32\du.exe"

to

strCommand = "C:\Windows\System32\du.exe c:\temp"

No need to invoke CMD.EXE (%comspec%) just to run an EXE, VBS can do that.

And without a directory name DU won't display anything to stdout, only to stderr.

~bp


0
 
LVL 65

Accepted Solution

by:
RobSampson earned 1200 total points
ID: 36745989
You should also use cmd /c because the prompt will stay open otherwise.

Regards,

Rob.
Dim strCommand, objShell, objExec 
strFolder = "C:\Temp"
strCommand = "%comspec% /c C:\Windows\System32\du.exe -accepteula """ & strFolder & """"
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strCommand)
While objExec.Status = 0
	WScript.Sleep 100
Wend
WScript.Echo objExec.StdOut.ReadAll & VbCrLf & objExec.StdErr.ReadAll

Open in new window

0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 36749652
@Rob,

Do you disagree that spawning CMD is not even needed in this case, since we just want to run a single EXE?

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36751092
No, provided that du doesn't require the command prompt to be run, it should work without the %comspec% /c entirely.

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question