Run Shell Commandline Silent

doctorcpu

New Member
Joined
Aug 27, 2016
Messages
12
I found this code bit searching google and it does exactly what I need after a few changes. I would like to change it to run silent (No command window) but can't figure out how.


down vote

Based on Andrew Lessard's answer, here's a function to run a command and return the output as a string -
<code style='margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; background-color: rgb(239, 240, 241); white-space: inherit;'>Public Function ShellRun(sCmd As String) As String

'Run a shell command, returning the output as a string

Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")

'run command
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
Set oOutput = oExec.StdOut

'handle the results as they are written to and read from the StdOut object
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine
= oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend

ShellRun
= s

End Function</code>


Any help would be appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If I understand the purpose of your code correctly, you want to read in the non-blank lines in a text file. If that is correct, here is an alternate function that you can consider, simply pass the full path for the text file (drive, directories, filename and extension) into the function...
Code:
[table="width: 500"]
[tr]
	[td]Function TextFile(PathFilename As String) As String
  Dim FileNum As Long, TotalFile As String, vNum As Variant
  FileNum = FreeFile
  Open PathFilename For Binary As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] 
    TotalFile = Space(LOF(FileNum))
    Get [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] , , TotalFile
  Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] 
  For Each vNum In Array(121, 13, 5, 3, 3, 2)
    TotalFile = Replace(Replace(TotalFile, vbCrLf, vbLf), String(vNum, vbLf), vbLf)
  Next
  If Right(TotalFile, 1) = vbLf Then TotalFile = Left(TotalFile, Len(TotalFile) - 1)
  If Left(TotalFile, 1) = vbLf Then TotalFile = Mid(TotalFile, 2)
  TextFile = TotalFile
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
If I understand the purpose of your code correctly, you want to read in the non-blank lines in a text file. If that is correct, here is an alternate function that you can consider, simply pass the full path for the text file (drive, directories, filename and extension) into the function...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function TextFile(PathFilename As String) As String
  Dim FileNum As Long, TotalFile As String, vNum As Variant
  FileNum = FreeFile
  Open PathFilename For Binary As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL] 
    TotalFile = Space(LOF(FileNum))
    Get [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL] , , TotalFile
  Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL] 
  For Each vNum In Array(121, 13, 5, 3, 3, 2)
    TotalFile = Replace(Replace(TotalFile, vbCrLf, vbLf), String(vNum, vbLf), vbLf)
  Next
  If Right(TotalFile, 1) = vbLf Then TotalFile = Left(TotalFile, Len(TotalFile) - 1)
  If Left(TotalFile, 1) = vbLf Then TotalFile = Mid(TotalFile, 2)
  TextFile = TotalFile
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Thanks for the reply, I'm actually trying to run this without the command window showing.
 
Upvote 0
Thanks for the reply, I'm actually trying to run this without the command window showing.
If I guessed correctly what your code is trying to do, then try the code I posted as it does not shell out so there will be no command window to hide.
 
Upvote 0
@doctorcpu
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Please supply links to all other sites where you have asked this question
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top