PDA

View Full Version : Solved: Output of DOS command to excel using VBA



sagotianitin
04-09-2013, 01:33 AM
Hi Guys,

I want to know if it is possible to capture the output of a DOS command to
excel using VBA code (Without using a text file to store the output) ?

Exactly what I am trying to do is:
I am running "net users /domain sagotianitin" in CMD. And output of command is:

The request will be processed at a domain control
User name sagotianitin
Full Name NITIN SAGOTIA
comment
Country code 000 (System Default)
Account active Yes
Account expires Never

So, here I want that excel captures the User Name value i.e. "sagotainitin" and paste it on to excel sheet.

Help on this would be appreaciated! :beerchug:

Thank You,
Nitin

Teeroy
04-09-2013, 05:38 AM
Hi sagotianitin,

I've never actually tried but I can't think of a way to pass the information from a CMD window without a text file. If you only want the username couldn't you use
Environ("USERNAME")
As a mental exercise though you could use VBA to initiate the shell command, import the (temporary) text file produced, extract the information, and automatically delete the text file within the code.

Kenneth Hobs
04-09-2013, 05:39 AM
Try WScript.
e.g.

Sub DirStdOut()
Dim s As String, a() As String
' /b = bare file listing, /s = search subfolders, /c = open command shell, run command and then close shell.
' /a:-d means list files only and not subfolders
s = CreateObject("Wscript.Shell").Exec("cmd /c dir x:\test\*.* /a:d /b").StdOut.ReadAll
a() = Split(s, vbCrLf)
With Range("A1")
.EntireColumn.Clear
.Resize(UBound(a)).Value = WorksheetFunction.Transpose(a)
End With
End Sub

snb
04-09-2013, 06:16 AM
sub M_snb()
msgbox replace(filter(split(createobject("wscript.shell").exec("cmd /c net user /domain sagotianitin").stdout.readall,vbcrlf),"user name")(0),"user name ","")
end sub


but probably one of these suffice:

msgbox CreateObject("Wscript.Network").COMPUTERNAME
msgbox CreateObject("Wscript.Network").USERDOMAIN
msgbox CreateObject("Wscript.Network").UserName

sagotianitin
05-24-2013, 01:20 AM
Thanks every one! This query has been resolved. :)