Executing 7-zip from VB - executable

Alright, I'm having a little trouble getting 7-zip to execute from VB.
Here's my current code:
ZipFileName = "\\network\path\PDFs\Test.zip "
PathToPDFs = "\\network\path\PDFs\*.pdf"
Arg1 = "a -tzip"
Process.Start("C:\Program Files\7-Zip\7z.exe" + Arg1 + Zipfilename + PathToPDFs)
The error I keep getting is The system cannot find the file specified and Win32Exception was unhandled
I know my path is correct and there are PDFs in that directory.
Any suggestions?

You have to use this
Process.Start(
"C:\Program Files\7-Zip\7z.exe",
Arg1 + Zipfilename + PathToPDFs)
First argument must be ONLY executable, while second one must be ProcessInfo or a string with arguments.
Tkae a look at this Microsoft page.

I did something similar with gzip like this:
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
proc.EnableRaisingEvents = False
proc.StartInfo.FileName = "d:\gnuwin32\bin\gzip"
proc.StartInfo.Arguments = My.Settings.GZIPFlags & " " & strDestDir & strFile
proc.Start()
proc.WaitForExit()

Related

Different results for exit conditions in process call

As a small part of a project, I'm calling ffmpeg to convert a video - I worked from an old example that used some of the output to create a progress bar, which was hanging. Code below:
[initiation code common to both examples]
Dim inputFile, outputFile As String, myProcess As New Process
Dim psiProcInfo As New ProcessStartInfo, ffreader As StreamReader
inputFile = "C:\Users\mklefass\Downloads\Video 2017-08-16 21.01.39.mov"
outputFile = "C:\Users\mklefass\Downloads\tmp2\Output"
psiProcInfo.FileName = Application.StartupPath + "\ffmpeg.exe" 'Location Of FFMPEG.EXE
psiProcInfo.Arguments = " -i " & Chr(34) & inputFile & Chr(34) & " -vf fps=5 " & Chr(34) & outputFile & "%d.jpg" & Chr(34) 'start ffmpeg with command strFFCMD string
psiProcInfo.UseShellExecute = False 'use the shell execute command we always want no
psiProcInfo.WindowStyle = ProcessWindowStyle.Hidden 'hide the ffmpeg process window
psiProcInfo.RedirectStandardError = True 'Redirect the error out so we can read it
psiProcInfo.RedirectStandardOutput = True 'Redirect the standard out so we can read it
psiProcInfo.CreateNoWindow = True
[bit that changes]
myProcess.Start()
ffreader = myProcess.StandardError
Do
Try
If Not myProcess.HasExited Then
'Debug.WriteLine(ffreader.ReadLine)
End If
Catch
If Not myProcess.HasExited Then
MsgBox("Something went wrong")
End If
End Try
Loop Until myProcess.HasExited
MsgBox("done")
I then found another example that just called the executable and then continued when it was done - as below:
[same initialisation]
Debug.WriteLine("Starting...")
myProcess.Start()
strOutput = myProcess.StandardError.ReadToEnd
myProcess.WaitForExit()
myProcess.Close()
Debug.Write(strOutput)
MsgBox("done")
The second approach worked perfectly... What's different about the "exit state" that Process.HasExited and Process.WaitForExit look for?

VB.Net shell output sometimes not returning

for 99% of all my Shell commands this code works fine, but for just one command I don't get the returned text, which is normally visible in the Shell window. Here is the code I use to execute commands and read back the results:
Function RunCommandCom(command As String, arguments As String) As String()
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
Dim output As String
pi.Arguments = " " + "/c" + " " + command + " " + arguments
pi.FileName = "cmd.exe"
pi.RedirectStandardOutput = True 'pi.CreateNoWindow = True
pi.WindowStyle = ProcessWindowStyle.Normal
pi.UseShellExecute = False
p.StartInfo = pi
p.Start()
output = p.StandardOutput.ReadToEnd()
p.WaitForExit()
p.Close()
If output Is ""
Then
Return Nothing
Else
Return output.Replace(Chr(13), "").Split(Chr(10))
End If
End Function
The command making Problems is executed fine. It shall create a entry in a database, which defintly is existing after calling my funciton. Executing the command directly in a Shell generates the same entry and I can see the returned text.
Does anyone has an idea why the streamreader does not read/contain anything for exactly this one special command?
Arg. I found the problem. These two commands send their output to the StandardError stream. I don't know why, as no error occured.

Batch start with parameters Error

I'm trying to make only a small program which includes the two steps decompile and start from a .java file. I can't run the .bat file correctly because DOS doesn't accept the spaces like I want.
Here's my code:
Process.Start("cmd.exe", "/c start "" """ & TextBoxJavacPath.Text & _
"""" & " " & """" & TextBoxFile.Text & """")
That's the string that comes out: (it's right)
/c "C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe"
"C:\Users\Niklas\Desktop\Java\Kap06\src\eingabe\LetsReadLine.java"
If I enter it by typing in console it works, but via vb.net it doesn't work.
The error is the following: The command "C:/Program" is written incorrect or couldn't be found.
Try using this code (I don't know if it'll work) :
Private Sub Start(javacPath As String, file As String)
Using p As New Process With {
.StartInfo = New ProcessStartInfo With {
.WorkingDirectory = Path.GetDirectoryName(javacPath),
.Arguments = "/c """ & Path.GetFileName(javacPath) & """ """ & file,
.FileName = "cmd",
.CreateNoWindow = True}}
p.Start()
End Using
End Sub
And call the method like this :
Start(TextBoxJavacPath.Text, TextBoxFile.Text)
I can't test the code, because I don't have any .java file...

How do i read the output from a batch file in a vb program

thanks in advance i just want to know if theres a way to read the output from a running batch file in a vb.net program. Thanks!
As one commenter above mentions, you can run the batch file in a shell within your VB.NET program and then read the directed output. I have done this exactly in previous project.
Here is a code snippet which shows how you can do it:
Dim outputFile As String = """" & Path.GetTempFileName & """"
Dim batchCommand As String = """C:\Path\To\MyFile.bat"">" & outputFile
Dim cmdProcess As New Process
With cmdProcess
.StartInfo = New ProcessStartInfo("cmd.exe", "/C " & batchCommand)
With .StartInfo
.CreateNoWindow = True
.UseShellExecute = False
End With
.Start()
.WaitForExit()
End With
' This is the output from the batch file.
Dim batchOutput As String = My.Computer.FileSystem.ReadAllText(outputFile)

Displaying the output of Process.Start

I have a Process.Start command that I would like to see the output of, but the new window is opening and closing too quickly for me to see anything. Here is the code I have so far that I'm working with:
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("VS110COMNTOOLS") & "..\Ide\MSTEST.EXE", "/Testsettings: """ & rwSettings & "" & " /Testcontainer: """ & rwContainer & "" & " /Resultsfile: """ & rwResults & "")
Unfortunately as I try to debug this if I allow this to run it flashes up the window but doesn't let me see what the error is, or if it's running successfully at all. I'm using VS2012 so I might just not be looking at the right view when I'm debugging.
Here is some code taen out of the middle of some logic, so it is not standalone. You can use ProcessStartInfo() and Process() to have more control:
Dim start_info As New ProcessStartInfo("sqlcmd", cmd)
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
Dim dt As Date = Now()
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As StreamReader = proc.StandardOutput() ' will not continue until process stops
Dim std_err As StreamReader = proc.StandardError()
' Retrive the results.
Dim sOut As String = std_out.ReadToEnd()
Dim sErr As String = std_err.ReadToEnd()