vb.net problem running a command - vb.net

the following runapplication won't work
Private Sub RunApplication(ByVal ProgName As String)
// String of text as a command to execute with the command line interpreter
Dim strApplication As String = "cmd.exe/c"
Dim ProcessID As Integer
strApplication = strApplication & " " & ProgName & " > C:\tool.tmp"
// Execute the command but hide it from the user
Shell(strApplication, AppWinStyle.Hide, True)
// View the Output in notepad.exe
ProcessID = Shell("notepad.exe C:\tool.tmp", AppWinStyle.NormalFocus)
AppActivate(ProcessID)
any suggestions would be gratefully appreciated

Put a space "cmd.exe/c" should be "cmd.exe /c"

Related

Printing to Brother PT-D600 from MS Access Using VBA

I am trying to print a Serial Number to a Brother PT-D600 printer whenever an MS access form is submitted.
This is my current script.
Public Sub PrintLabel_Click(ByVal printStatement As String)
On Error GoTo ErrorHandler
Dim labelmakerPath As String
labelmakerPath = "C:\Program Files (x86)\Brother\Ptedit54\ptedit54.exe"
Dim labelData As String
labelData = printStatement
Dim labelPath As String
labelPath = "C:\Users\Username\Documents\My Labels\AutoLabel.lbx"
Dim labelCommand As String
labelCommand = """" & labelmakerPath & """" & " /X """ & labelData & """ """ & labelPath & """"
Dim success As Integer
success = Shell(labelCommand, vbNormalFocus)
If success = 0 Then
MsgBox "Error: Unable to run command: " & labelCommand, vbExclamation, "Error"
End If
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
End Sub
While trying to debug, I've tried to use the following python script to print directly from the command line
import subprocess
serialNumber = "20230219"
labelText = "1234567"
printerName = "Brother PT-D600"
printCommand = f'ptouch-printer -n "{printerName}" -l "24mm" -t "test label" -i "ABC" -s "{serialNumber}" -b "1" -q "1" "{labelText}"'
# Print the label
try:
subprocess.run(printCommand, shell=True, check=True)
print("Label printed successfully")
except subprocess.CalledProcessError as e:
print(f"Printing label failed: {e}")
However, it doesn't seem like ptouch-printer is available anymore?

WScript.Shell Fails For Unknown Reasons VB6.5 [duplicate]

This question already has answers here:
VBS with Space in File Path
(4 answers)
Closed 4 years ago.
In VBA I am running a piece of external software I wrote. I know the software works well. I can run it from the command line. But when I attempt to execute it using WScript.Shell.Run, it returns a 1 and it never runs. I can't even tell that it executes the software at all.
Here is the executing portion of the VBA class.
Public Function Execute() As Integer
Dim wsh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim exitCode As Integer: exitCode = -1
Set wsh = VBA.CreateObject("WScript.Shell")
'Run program with arguments and wait for the program to finish
If Me.PrinterType = 1 Then exitCode = wsh.Run(Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location & "", windowStyle, waitOnReturn)
If Me.PrinterType = 2 Then exitCode = wsh.Run(Me.Path & " /boxid=" & Me.Location & " /version=" & Me.Version, windowStyle, waitOnReturn)
Execute = exitCode
End Function
An example of the full execute string:
C:.path.to.\PrintLabel.exe /serial=EOSJ61110044 /position=2
Evidence that the script runs without error from the same machine the VBA is executing on:
So I've got no idea what's going on.
I can use the following VBA Shell function successfully:
If Me.PrinterType = 1 Then exitCode = Shell(Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location, vbHide)
But the problem then becomes that the VBA doesn't wait on the software to finish before continuing. So I need to continue to try and make WScript.Shell work. What am I missing? Are there any specific references that need to be enabled that I could be missing?
Update 1:
Tried this to mimic this SO post, but still didn't have success:
If Me.PrinterType = 1 Then fullExecutionString = Me.Path & " /serial=" & Me.SerialNumber & " /position=" & Me.Location
If Me.PrinterType = 2 Then fullExecutionString = Me.Path & " /boxid=" & Me.Location & " /version=" & Me.Version
wsh.Run fullExecutionString, windowStyle, waitOnReturn
Update 2:
Made a small method that repeats the process in windows cmd console. It works (more or less) with the same code. I am guessing it has something to do with the command path string with arguments?
Private Sub Button2_Released()
Dim wsh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim exitCode As Integer: exitCode = 0
Set wsh = VBA.CreateObject("WScript.Shell")
exitCode = wsh.Run("C:\Windows\System32\cmd.exe", windowStyle, waitOnReturn)
End Sub
Answering my own question:
After much debugging I resolved that the path with spaces is causing the issue. If I directly input the following (notice the quote placement), the code successfully runs:
exitCode = wsh.Run("""C:\path.to\PrintLabel.exe"" /serial=EOSJ61110044 /position=2", windowStyle, waitOnReturn)
So in the end I adjusted my Path property:
Public Property Let Path(ByVal NewValue As String)
actPath = Chr(34) & NewValue & Chr(34)
End Property
Public Property Get Path() As String
Path = actPath
End Property

VB.NET not copying a folder to new destination

So I want to copy the folder at the end, But, for some reason, it doesn't copy it. I do not get an error message, so the code doesn't have errors, it's just incorrect.
Dim Log As String = System.IO.Path.Combine(DateTime.Now.ToString("yyyy_MM_dd_HHmmss"))
Process.Start("CMD", "/c robocopy.exe " & Source & " " & Destination & " /log:C:\Backup\log_" & Log & ".txt ")
Dim Copy2 As String = ("Backup_" & DateTime.Now.ToString("yyyy_MM_dd_HHmmss"))
Dim Destination2 As String
Destination2 = Destination
Dim copy4 As String = Destination2.Substring(0, Destination2.LastIndexOf("\"))
Dim Copy3 As String = System.IO.Path.Combine(copy4, Copy2)
FileIO.FileSystem.CreateDirectory(Copy3)
My.Computer.FileSystem.MoveDirectory(Destination, Copy3, True)
MsgBox("Backup ist vollendet!")
did you try to end the Process, befor accessing the just copied data ?
Try :
process.kill

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 to send Multiline sms using gnokii with vb.net

dear all im tried to send multiline sms using gnikii but it fails
Dim xCmd As String
xCmd = "cmd.exe /c echo " & txtBody.Text & " | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt"
Shell(xCmd)
Please help me
You can split your text on vbCrLf and add echo in front so you end up with this cmdline instead
cmd.exe /c lineOne echo line2 | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt
code wise you can do it like this:
Dim sString As String
Dim aLines As String()
Dim xCmd As String
sString = "lineOne" & vbCrLf & "line2"
aLines = sString.Split(vbCrLf)
sString = Join(aLines," echo")
xCmd = "cmd.exe /c " & sString & " | c:\sms\gnokii.exe --sendsms 0771234567 2> test.txt"
Shell(xCmd)