?
Solved

capture output of du.exe in vbscript

Posted on 2011-09-28
7
Medium Priority
?
911 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 56

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 56

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

743 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