Introduction
I’m trying to write a program that will create lots of .7z files. Over 1000. If I create one cmd window for every instance, either it creates 1000 windows at the same time, or one in a row for several hours. Neither situation is good.
I can’t say: hide the window, because I actually need to show the progress of the 7z to the user as some of the files may be huge, and without a window, the process seems to have died, but it just takes a bit, and I don’t know how to use 7z.dll and be able to get the status.
My solution to the problem
If I can spawn a cmd window that the user can interact with (doesn’t have to be), and then send keypresses to that window, everything is consolidated.
My problem
I can’t seem to get it to work. The best thing that I have so far is that it does spawn a window, but every attempt to have something printed on the cmd window, ends up appearing in my debug output window. Because the 7z archiving process has dynamic text update, capturing the output and putting it in a textfield doesn’t work because it doesn’t capture the archiving percentage for the file being created. While it displays some stuff about the archiving process, the essential part is missing.
My code:
1 form: Form1
1 button: bInteractWithCMD
Targetting .net 4.8.1 using Visual Studio 2022 (if the fix is going to be using a different version of .net, let me know which version.)
Public Class Form1
Private Process As New Process
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Process.StartInfo.FileName = "cmd.exe"
Me.Process.StartInfo.Arguments = "/k echo Hello World"
Me.Process.StartInfo.UseShellExecute = False
Me.Process.StartInfo.RedirectStandardOutput = False
Me.Process.StartInfo.RedirectStandardInput = True
Me.Process.Start()
End Sub
Private Sub bInteractWithCMD_Click(sender As Object, e As EventArgs) Handles bInteractWithCMD.Click
Me.Process.StandardInput.WriteLine("echo This is a test.")
End Sub
End Class
Footnote
In this example, I’m doing a simple echo. If I replace that with the 7z.exe code, the behavior is still the same, but this example is far easier to troubleshoot.
If you have any other way of getting the 7z.exe progress without displaying it in the cmd window, I’ll happily accept that answer. Also, I can’t let my program create a .cmd script and execute that. While it would theoretically work, my program will do steps in between processing each file including informing the user, and I don’t want that part to happen in the cmd window. The user should be able to minimize the cmd window, and go back to it to peek for the progress if necessary. The cmd window is more a way to keep track that the process hasn’t stalled. My program will display the status in a more convenient way.
2
Answers
Answering myself
I've decided to also write an answer to show what I ended up doing. I'm sure some people out there want this too, and my approach is a bit more simplified. I will of course keep the answer that allowed me to learn from my mistake to be the accepted answer. This one is just bonus. :)
Run the 7zip process and capture its output to a textbox (for debug)
For the following code, Create a form with a command button and a textbox.
Run the 7zip process and capture its output to a progressbar (end result)
For the following code, you need a command button and a progressbar. I kept the textbox reference in, you can uncomment it to see its result (for debug).
The following shows how one can use System.Diagnostics.Process with 7-Zip to compress files in a directory. If one specifies
-bsp1
in the command-line arguments, the progress will be displayed.Create a class (name: HelperProcess.vb)
Usage:
Resources: