Solved

capture output of du.exe in vbscript

Posted on 2011-09-28
7
885 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 4

Expert Comment

by:GeoffHarper
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
@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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now