Solved

capture output of du.exe in vbscript

Posted on 2011-09-28
7
903 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 83

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 200 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 300 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 54

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

Technology Partners: 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!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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