Solved

capture output of du.exe in vbscript

Posted on 2011-09-28
7
898 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

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 53

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

825 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