So I have this in my coding:
vb Code:
file = My.Computer.FileSystem.OpenTextFileWriter("c:\command.bat", False)
file.WriteLine("#echo off")
file.WriteLine("cd " & TextBox2.Text)
file.WriteLine("adb shell dumpsys meminfo " & TextBox1.Text & " > C:\Sample.txt")
file.Close()
Shell("C:\command.bat")
what I want it to do is to run the batch file without it opening if that makes sense. Right now this runs on a loop for 10 minutes and on every 2 second tick it opens and then closes the .bat. Which is really annoying to see a .bat open and close every two seconds. Is there anyway to get this process to run silently in the background so the user doesnt even know that it is running?
Shell("C:\command.bat", AppWinStyle.Hide)
That will run the batch file but the window is hidden.
or use Process.Start as suggested by David. with WindowStyle = ProcessWindowStyle.Hidden
Here is an example on how to use Process.Start with a hidden window
Dim params As String = "C:\command.bat"
Dim myProcess As New ProcessStartInfo
myProcess.FileName = "cmd.exe"
myProcess.Arguments = params
myProcess.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(myProcess)
if you run into the issue of file not found errors with the path you can try to add the following Windows API call and run your file path through this function as well.
'This would be declared at the top of your Form Code/Class Code
Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As StringBuilder, _
ByVal cchBuffer As Integer) As Integer
And here is the function to return back a ShortPath (win98 style path (ie. c:/progra~1/myfolder/myfile.bat)
Public Function GetShortPath(ByVal longPath As String) As String
Dim requiredSize As Integer = GetShortPathName(longPath, Nothing, 0)
Dim buffer As New StringBuilder(requiredSize)
GetShortPathName(longPath, buffer, buffer.Capacity)
Return buffer.ToString()
End Function
then simply call your path like this in your process.start function
Dim params As String = GetShortPathName("C:\command.bat")
Related
I have a OSI PI Processbook file that I am using VBA on to open a new instance of PI Processbook. Basically I have a text element (Text35) on which I have a vba click event:
Private Sub Text35_Click(ByVal lvarX As Long, ByVal lvarY As Long)
Dim filePathAndName As String
Dim exeLocation As String
Dim PID As Variant
On Error GoTo errHandle
filePathAndName = "C:\Users\myuser\Desktop\TEST.PDI"
exeLocation = "C:\Program Files (x86)\PIPC\Procbook\Procbook.exe"
PID = Shell("""" & exeLocation & """", vbNormalFocus)
'How can I use the above process id to open a PDI file??
Exit Sub
errHandle:
End Sub
Basically I want to open the TEST.PDI file on my desktop in an entirely new copy of PI Processbook. I've tried pplication.Displays.Open(filePathAndName, True) but this opens my TEST.PDI into the same instance of Processbook, not a new application instance like I want.
Is there some Shell command or command like switch argument I can use to both open a new instance of the .exe and open up a file at the same time? I, at least, have the process id of the new instance stored in the "PID" variable, so I am thinking this might help.
I have successfully coded some VBA macros for work which basically create a data file, feed it to a program and post-treat the output from this program.
My issue is that the program installation path is hard coded in the macro and the installation may vary accross my colleagues computers.
The first thing I thought is that I can gather from everyone the different installation directories and test for all of them in the code. Hopefully, one of them will work. But it doesn't feel that clean.
So my other idea was to somehow get the installation directory in the code. I thought it would be possible as in Windows, if I right click on a shortcut, I can ask to open the file's directory. What I'm basically looking for is an equivalent in VBA of this right click action in Windows. And that's where I'm stuck.
From what I found, Windows API may get the job done but that's really out of what I know about VBA.
The API FindExecutable seemed not too far from what I wanted but I still can't manage to use it right. So far, I can only get the program running if I already know its directory.
Could you give me some pointers ? Thanks.
Here's another method for you to try. Note that you might see a black box pop up for a moment, that's normal.
Function GetInstallDirectory(appName As String) As String
Dim retVal As String
retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & appName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(2)
GetInstallDirectory = Left$(retVal, InStrRev(retVal, "\"))
End Function
It's not as clean as using API but should get the trick done.
Summary:
retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & appName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(1)
"CMD /C FOR /r ""C:\"" %i IN (*" & appName & ") DO (ECHO %i)" is a command that works in CMD to loop through files rooted at a defined path. We use the wildcard with the appName variable to test for the program we want. (more info on FOR /R here) Here, we have created the CMD application using a Shell object (WScript.Shell) and Executed the command prompt CMD passing arguments to it directly after. The /C switch means that we want to pass a command to CMD and then close the window immediately after it's processed.
We then use .StdOut.ReadAll to read all of the output from that command via the Standard Output stream.
Next, we wrap that in a Split() method and split the output on vbCrLf (Carriage return & Line feed) so that we have a single dimension array with each line of the output. Because the command outputs each hit on a new line in CMD this is ideal.
The output looks something like this:
C:\Users\MM\Documents>(ECHO C:\Program Files\Microsoft
Office\Office14\EXCEL.EXE ) C:\Program Files\Microsoft
Office\Office14\EXCEL.EXE
C:\Users\MM\Documents>(ECHO
C:\Windows\Installer\$PatchCache$\Managed\00004109110000000000000000F01FEC\14.0.4763\EXCEL.EXE
)
C:\Windows\Installer\$PatchCache$\Managed\00004109110000000000000000F01FEC\14.0.4763\EXCEL.EXE
C:\Users\olearysa\Documents>(ECHO
C:\Windows\Installer\$PatchCache$\Managed\00004109110000000000000000F01FEC\14.0.7015\EXCEL.EXE
)
C:\Windows\Installer\$PatchCache$\Managed\00004109110000000000000000F01FEC\14.0.7015\EXCEL.EXE
We're only interested in the third line of the output (the first line is actually blank), so we can access that index of the array directly by using (2) after it (because arrays are zero-indexed by default)
Finally, we only want the path so we use a combination of Left$() (which will return n amount of characters from the left of a string) and InStrRev() (which returns the position of a substring starting from the end and moving backwards). This means we can specify everything from the left until the first occurrence of \ when searching backwards through the string.
Give this a try, assuming you know the name of the .exe:
#If Win64 Then
Declare PtrSafe Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
#Else
Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
#End If
Const SYS_OUT_OF_MEM As Long = &H0
Const ERROR_FILE_NOT_FOUND As Long = &H2
Const ERROR_PATH_NOT_FOUND As Long = &H3
Const ERROR_BAD_FORMAT As Long = &HB
Const NO_ASSOC_FILE As Long = &H1F
Const MIN_SUCCESS_LNG As Long = &H20
Const MAX_PATH As Long = &H104
Const USR_NULL As String = "NULL"
Const S_DIR As String = "C:\" '// Change as required (drive that .exe will be on)
Function GetInstallDirectory(ByVal usProgName As String) As String
Dim fRetPath As String * MAX_PATH
Dim fRetLng As Long
fRetLng = FindExecutable(usProgName, S_DIR, fRetPath)
If fRetLng >= MIN_SUCCESS_LNG Then
GetInstallDirectory = Left$(Trim$(fRetPath), InStrRev(Trim$(fRetPath), "\"))
End If
End Function
Example of how to use, let's try looking for Excel:
Sub ExampleUse()
Dim x As String
x = "EXCEL.EXE"
Debug.Print GetInstallDirectory(x)
End Sub
Output (on my machine anyway) is
C:\Program Files\Microsoft Office\Office14\
Assuming you are working on PC only and the people are working with their own copies and not a shared network copy. I would recommend the following.
Create a Sheet called 'Config', place the path with the exe in there, and then hide it.
Use use FileScriptingObject ('Tools' > 'References' > 'Microsoft Scripting Runtime') to see if the path in 'Config' exists
If it does not, ask the user for the location using a 'open file dialog box' and remember that in the 'Config' Sheet for next time.
The below code may help as a pointer.
Dim FSO As New FileSystemObject
Private Function GetFilePath() As String
Dim FlDlg As FileDialog
Dim StrPath As String
Set FlDlg = Application.FileDialog(msoFileDialogOpen)
With FlDlg
.Filters.Clear
.Filters.Add "Executable Files", "*.exe"
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Select the executable"
.Show
If .SelectedItems.Count <> 0 Then GetFilePath = .SelectedItems(1)
End With
Set FlDlg = Nothing
End Function
Private Function FileExists(ByVal StrPath As String) As Boolean
FileExists = FSO.FileExists(StrPath)
End Function
I'm printing a pdf directly towards a printer, but I want to do this in the background.
At the moment everything is working, but you can see that adobe is starting up and opening a document, while I want that to stay hidden.
For this I tried to use:
Process1.StartInfo.CreateNoWindow = True
But it won't work, while I do everything the same as written in:
MSDN
It is mentioned that you should place
psi.UseShellExecute = False
In order to let CreateNoWindow work, but when I place this in my code I get an error message: The system cannot find the file specified, while without this line, it works.
Below you can find the entire code:
Dim Process1 As New System.Diagnostics.Process
Dim psi As New ProcessStartInfo("AcroRd32.exe", "/t " + temppdf + " " + General.pdfprinter + "")
'psi.UseShellExecute = False
psi.CreateNoWindow = True
Process1.StartInfo = psi
Process1.Start()
Process1.WaitForInputIdle()
Process1.Kill()
Any idea?
How about this?
Define the function ShowWindow:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommands) As Boolean
End Function
And then call it:
Process1.Start()
ShowWindow(Process1.MainWindowHandle, 0)
I am unable to run batch file that is located in mapped network drive using System.Diagnostics.Process in VB.Net. I get:
'<batchfilename>.bat' is not recognized as an internal or external command, operable program or batch file.
Press any key to continue...
I get the same error when navigate to the folder (using explorer) that contains this batch file and double click it. I can successfully run the batch file by changing the directory to that folder through command prompt. I can't figure out why this is happening. I think the problem is Start() function of Process works like double click rather than just run the batch file. I have put my code below. Any help regarding this will be much appreciated.
Private WithEvents batchProcObj As New Process
Private Const SLEEP_AMOUNT As Integer = 100
Public Event Exited As EventHandler
Private eventHandled As Boolean
Public Sub startBatchProcess(ByVal batchProcFilePath As String, ByVal batchParams As String, ByVal domainName As String, ByVal loginName As String, ByVal passwd As String)
' Initialise process start info with batch process path and the parameters
Dim startInfoObj As New ProcessStartInfo()
Dim parentDir As New IO.DirectoryInfo(batchProcFilePath)
' Initialise event to false
eventHandled = False
' Get the batch file name without the extension
batchName = batchProcFilePath
Try
' Assign the batch process properties
startInfoObj.FileName = batchProcFilePath
startInfoObj.Arguments = batchParams
startInfoObj.UseShellExecute = False
startInfoObj.WindowStyle = ProcessWindowStyle.Normal
startInfoObj.WorkingDirectory = "C:\Windows\System32\"
startInfoObj.Domain = domainName
startInfoObj.UserName = loginName
startInfoObj.Password = appHandler.getSecureString(passwd)
startInfoObj.LoadUserProfile = True
batchProcObj.StartInfo = startInfoObj
batchProcObj.EnableRaisingEvents = True
' Start the batch file
batchProcObj.Start()
' Get the start time of the batch process
procStartTime = batchProcObj.StartTime
' Insert audit log
appHandler.writeAuditLog("RunBatchProcess", "START - Batch Process [" & batchName & "]")
Catch ex As Exception
appHandler.writeErrorLog("Start Batch Process: " & batchProcFilePath, ex)
End Try
' Continue running the batch process till it exits
Do While Not eventHandled
elapsedTime += SLEEP_AMOUNT
Thread.Sleep(SLEEP_AMOUNT)
Loop
End Sub
Just use FileIO.FileSystem and copy that into temp file.
Then use Backgroundworker to run.
I currently have this piece of code:
Sub Button1Click(sender As Object, e As EventArgs)
If dlgFolder.ShowDialog = Windows.Forms.DialogResult.OK Then
txtPath.Text = dlgFolder.SelectedPath
Try
Dim CopyFile As String = Path.Combine(Directory.GetCurrentDirectory, "pdftk.exe")
Dim CopyLocation As String = Path.Combine(dlgFolder.SelectedPath, "pdftk.exe")
Dim pyScript As String = Path.Combine(Directory.GetCurrentDirectory, "pdfmerge.py")
Dim pyLocation As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
System.IO.File.Copy(CopyFile, CopyLocation, True)
System.IO.File.Copy(pyScript, pyLocation, True)
Catch copyError As IOException
Console.WriteLine(copyError.Message)
End Try
End If
End Sub
This copies two files in the current working directory (which will be the default install folder) to the selected path from the Fodler Dialog Browser. This works correctly.
Now what I want to do is too run "pdfmerge.py" into the selected folder path. I tried the below code but the script is still running in the current working directory.
Sub BtnNowClick(sender As Object, e As EventArgs)
Dim myProcess As Process
Dim processFile As String = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
myProcess.Start(processFile, dlgFolder.SelectedPath)
End Sub
You can set the process's working directory.
Dim p As New ProcessStartInfo
p.FileName = Path.Combine(dlgFolder.SelectedPath, "pdfmerge.py")
p.WorkingDirectory = dlgFolder.SelectedPath
Process.Start(p)
One question: are you ensuring the dlgFolder.SelectedPath is correct? Without knowing the inner workings of your program, it appears possible to press BtnNow before Button1, meaning dlgFolder.SelectedPath won't have been set by the user.
Try using the overload of Process.Start() that takes 5 arguments.
Start ( _
fileName As String, _
arguments As String, _
userName As String, _
password As SecureString, _
domain As String _
)
You may be able to pass in null for userName and password, but if your directory is outside of the standard ones that you have permission for, you may need to put your username and password in. domain would be the working directory, I think.