VB to run Command for java.exe and output text to txt file - vba

Private Sub Command1_Click()
Dim sCommand as String
Dim oWrite as System.IO.StreamWriter
sCommand = "cmd C:\Windows\System32\java.exe -version2> C:\Users\Desktop\version.txt"
oWrite = IO.File.CreateText(C:\Users\Desktop\version.txt
End Sub
When i run the command in the command prompt it does what it is suppose to do (write to txt file. But when implemented into VB it will run and flash the command prompt but it will not write to a file.
Am I missing a line of something?

I was clearly making things way to complicated...
This ended up working perfectly Fine.
Sub Main()
Dim sdkCommand As String
sdkCommand = "C:\Windows\System32\Java.exe -version 2> C:\Users\Desktop\JavaSDKInfo.txt && C:\Windows\System32\tasklist.exe /FI ""IMAGENAME eq java.exe"" >> C:\Users\Desktop\JavaSDKInfo.txt"
Shell("cmd.exe /c" & sdkCommand)
End Sub

Related

Command Line: '■c' is not recognized as an internal or external command

I am creating a simple batch file using VBA (Fileout) within an AccessDB. The file is created and contains the desired commands, but when I attempt to run the Batch file from within code or at the Command Line, I get the following error:
C:\Test>■c
'■c' is not recognized as an internal or external command,
operable program or batch file.
Here is the basic idea of the code I am using to create the batch file. << commands >> are replaced for purposes of this example:
Dim strCmd As String
Dim strBatchFile As String
Dim fso As Object
Dim Fileout As Object
Set fso = CreateObject("Scripting.FileSystemObject")
strBatchFile = "C:\Test\FTP_Test.bat"
Fileout = fso.CreateTextFile(strBatchFile, True, True)
strCmd = <<command 1>> & vbCrLf
strCmd = strCmd & <<command 2>>
Fileout.Write strCmd
Fileout.Close
Shell strBatchFile
End Sub
From VBA can I specify some other filetype?

vba shell command executing but no output

I am having some problems running a shell command and checking the output of the data. I wish to check using vba if the current remote user of the DB is Active. In
command prompt =
for /f "tokens=1-8" %a in ('quser') do #if "%d"== "Active" echo %COMPUTERNAME% %a %d
returns the users logged on and their state I wish to check that none of them are disconnected ("Disc"). I used this function to check the shell and return the pipe value as a string in a message box
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
Debug.Print sCmd
'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
'example MsgBox ShellRun("cmd.exe /c" & "dir c:\")
End Function
Call Command used on click event
Dim CMDLineCommand As String
CMDLineCommand = "for /f ""tokens=1-8"" %a in ('quser') do #if ""%d""== ""Active"" echo %COMPUTERNAME% %a %d"
'(CMDLineCommand = "dir c:\")<------ THIS WORKS FINE
MsgBox ShellRun("cmd.exe /c " & CMDLineCommand)
This works fine for loads of command line commands I have tested it with but not query and therefore query user. The query user command works fine from command line but does not return anything when issued through a VBA Shell commands.
Any thoughts would be appreciated.
because shell does not know the path of the query.exe(quesr) it does not continue where as command prompt can use system variables to find exe's. solution find the query.exe and copy it to a working directory then run the shell command. mine was located in a hashed folder within C:\Windows\WinSxS be careful as here are 64bit versions and 32 bit.

ProcessStartInfo giving Error: The system cannot find the file specified

I want to run a command line which provision a computer but keep getting error The system cannot find the file specified. But when I run the code through cmd it works.
Below is the code
Public Sub DjoinExe()
Dim myProcessProperties As New ProcessStartInfo
Dim strArguments As New StringBuilder
myProcessProperties.FileName = "C:\Windows\System32\djoin.exe"
If strArguments.Length <> 0 Then
myProcessProperties.Arguments = " /provision /domain blablabla.com /machine machineName /reuse /savefile C:\Apps\offlinedomainjoin.txt "
End If
Dim myProcess As Process = Process.Start(myProcessProperties)
End Sub
Thanks in advance

Place Line from Text File in a Shell CMD. VB.net

I am assuming there is a way to have this sub in? I am assuming Do Until Loop is the way to go, I just do not know how to contruct one.
Sub Main()
'Declare Command
Dim sCommand As String
'Declare File where Users are Located
Dim strFile As String = "C:\TestDUsers.txt"
'Running from Admin Comptuer so permissions are fine
'Want to replace the ******** section with each username from text file
sCommand = "pushd \\*********\C$ && whoami.exe >> C:\File.txt"
'Load the File and perform the loop????
Using sr As New StreamReader(File.Open(strFile, FileMode.Open))
End Using
Console.WriteLine()
Console.Clear()
End Sub
Something like this if I understand your question correct:
Sub Main()
HandleJavaInfo()
End Sub
Sub HandleJavaInfo()
Dim strFile As String = "C:\Users\pseal2\Desktop\TestDUsers.txt"
Dim strCommand = "pushd \\*********\C$ && whoami.exe >> C:\Users\pseal2\Desktop\Javainfo."
Dim strLines() As String = IO.File.ReadAllLines(strFile)
For Each strUserName As String In strLines
'execute the command as shell or process
'Example using Shell (run the command whitout showing the window for the user)
Dim ThisCommand As String = strCommand.Replace("*********", strUserName)
Shell(ThisCommand, AppWinStyle.Hide)
'Example using process
Process.Start("pushd", "\\" & strUserName & "\C$ && whoami.exe >> C:\Users\pseal2\Desktop\Javainfo.")
Next
End Sub
Now, you have only told us what you want to DO, not what you want to achieve. If you tell us exactly what the goal is, then it may be some other .Net ways to do what you want, instead of shelling out to a DOS-command, and you can get more accurate answers.

Multiple Commands to same Text File from VB

I have this which is exporting the SDK info to a txt file. I would also like to export the processes that are running to the same text file.
Dim sdkCommand As String
sdkCommand = "C:\Windows\System32\Java.exe -version 2> C:\Users\JavaSDKInfo.txt"
Shell("cmd.exe /c" & sdkCommand)
End Sub
If I try adding this to it, I am only able to still see the output from sdkCommand, but nothing about the tasks that are running. I am assuming I need to combine the shell statements?
Sub Main()
Dim sdkCommand As String
Dim proCommand As String
sdkCommand = "C:\Windows\System32\Java.exe -version 2> C:\Users\Desktop\JavaSDKInfo.txt"
proCommand = "C:\Windows\System32\tasklist.exe > C:\Users\Desktop\JavaSDKInfo.txt"
Shell("cmd.exe /c" & sdkCommand)
Shell("cmd.exe /c" & proCommand)
End Sub
Combining all the commands into one long one using && actually made this work and made it easier. This allowed me to add a ton more command prompt commands.
Sub Main()
Dim sdkcommand as String
'COmbine using && in command prompt
sdkCommand = "java.exe >>C:\text.txt && tasklist.exe >>C:\text.txt"
Shell("cmd.exe /c" and sdkCommand)
End Sub