Henry2003
asked on
How to Run DOS Command line Using VB
Hello all!
I want to run this DOS command line using VB.
gpg -o c:\gnupg\outsourceone_rsmd _mr_elig.8 34c.gpg -r A01183A7 -e "f:\servcent\vendor\
bc ca\outsourceone_rsmd_mr_el ig.834c"
I used to manually run it in DOS Commmand. I want to run this from my VB program but I don't have any clue.
Your help is very appreciate
Thank you in advance.
I want to run this DOS command line using VB.
gpg -o c:\gnupg\outsourceone_rsmd
bc ca\outsourceone_rsmd_mr_el
I used to manually run it in DOS Commmand. I want to run this from my VB program but I don't have any clue.
Your help is very appreciate
Thank you in advance.
You may have to give a fully qualified path to your "gpg" app.
Idle_Mind
Idle_Mind
Just in case you are using a script (.vbs)
set WshShell = WScript.CreateObject("WScr ipt.Shell" )
WshShell.Run ("cmd /c gpg -o c:\gnupg\outsourceone_rsmd _mr_elig.8 34c.gpg -r A01183A7 -e ""f:\servcent\vendor\" & _
"bc ca\outsourceone_rsmd_mr_el ig.834c""" ,
set WshShell = WScript.CreateObject("WScr
WshShell.Run ("cmd /c gpg -o c:\gnupg\outsourceone_rsmd
"bc ca\outsourceone_rsmd_mr_el
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just use the following program
Public Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Declare Function CreateProcessA Lib "Kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Sub StartProcess(CommandLine As String, Optional Hide As Boolean = False)
Const STARTF_USESHOWWINDOW As Long = &H1
Const SW_HIDE As Long = 0
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
'Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
If Hide Then
Start.dwFlags = STARTF_USESHOWWINDOW
Start.wShowWindow = SW_HIDE
End If
'Start the shelled application:
CreateProcessA 0&, CommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc
End Sub
Public Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Declare Function CreateProcessA Lib "Kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Sub StartProcess(CommandLine As String, Optional Hide As Boolean = False)
Const STARTF_USESHOWWINDOW As Long = &H1
Const SW_HIDE As Long = 0
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
'Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
If Hide Then
Start.dwFlags = STARTF_USESHOWWINDOW
Start.wShowWindow = SW_HIDE
End If
'Start the shelled application:
CreateProcessA 0&, CommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc
End Sub
Public Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Declare Function CreateProcessA Lib "Kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Sub StartProcess(CommandLine As String, Optional Hide As Boolean = False)
Const STARTF_USESHOWWINDOW As Long = &H1
Const SW_HIDE As Long = 0
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
'Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
If Hide Then
Start.dwFlags = STARTF_USESHOWWINDOW
Start.wShowWindow = SW_HIDE
End If
'Start the shelled application:
CreateProcessA 0&, CommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, proc
End Sub
Private Sub Command1_Click()
call StartProcess "cmd"
End Sub
--laser--
If your going to use Shell(),
Private Sub CmdRun_Click()
Dim aa
aa = Shell("c:\mybat.bat", vbNormalFocus)
MsgBox "batch file executed"
Kill ("c:\mybat.bat")
End Sub
why bother making the batch file? This the same thing in one line:
Private Sub Command1_Click()
Dim retval As Double
retval = Shell("gpg -o c:\gnupg\outsourceone_rsmd _mr_elig.8 34c.gpg -r A01183A7 -e ""f:\servcent\vendor\" & _
"bc ca\outsourceone_rsmd_mr_el ig.834c""" , vbNormalFocus)
End Sub
Idle_Mind
If your going to use Shell(),
Private Sub CmdRun_Click()
Dim aa
aa = Shell("c:\mybat.bat", vbNormalFocus)
MsgBox "batch file executed"
Kill ("c:\mybat.bat")
End Sub
why bother making the batch file? This the same thing in one line:
Private Sub Command1_Click()
Dim retval As Double
retval = Shell("gpg -o c:\gnupg\outsourceone_rsmd
"bc ca\outsourceone_rsmd_mr_el
End Sub
Idle_Mind
ASKER
Hello laser!
I try to execute the .bat file using Shell function as your tell me to do. something like:
Set a = fso.CreateTextFile("C:\myb at.bat", True)
a.WriteLine ("cd c:\gnupg")
a.WriteLine ("del *.gpg")
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\" & Format(Now, "MMDDYY") & "MBI.mbi")
aa = Shell("c:\mybat.bat", vbNormalFocus)
I got the error on last line "Invalid procedure call or argument".
My conerning is Shell function only execute the .exe file only?
Please help
Thanks!
Henry
I try to execute the .bat file using Shell function as your tell me to do. something like:
Set a = fso.CreateTextFile("C:\myb
a.WriteLine ("cd c:\gnupg")
a.WriteLine ("del *.gpg")
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\"
aa = Shell("c:\mybat.bat", vbNormalFocus)
I got the error on last line "Invalid procedure call or argument".
My conerning is Shell function only execute the .exe file only?
Please help
Thanks!
Henry
You are missing the quote on the end of your command line.
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\" & Format(Now, "MMDDYY") & "MBI.mbi")
should be
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\" & Format(Now, "MMDDYY") & "MBI.mbi""")
Try that and see if it makes a difference.
Idle_Mind
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\"
should be
a.WriteLine ("gpg -o c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg -u FC1459C7 -r A01183A7 -e ""f:\servcent\vendor\MBI\"
Try that and see if it makes a difference.
Idle_Mind
When you use shell command, it's safer to use shortname path to the file. It seems to me that "outsourceone_rsmd_mr_elig .834c.gpg" is too long to be well known. Try to give shortname first like "outsou~1.gpg".
Here is how to do it using short file names without a batch file. It uses the GetShortPathName() API.
Idle_Mind
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Sub Command1_Click()
Dim files As New Collection
Dim file As Variant
Dim path As String, pattern As String
Dim retval As Double
Dim cmd As String
Dim file1 As String
Dim file2 As String
' Delete all .gpg files in c:\qgnupg\
path = "c:\gnupg\"
pattern = "*.gpg"
Set files = getFilesInDirectory(path, pattern)
For Each file In files
Kill path & file
Next file
' change directory and execute command line using short file names
ChDir path
file1 = GetShortPath("c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg")
file2 = GetShortPath("f:\servcent\ vendor\MBI \" & Format(Now, "MMDDYY") & "MBI.mbi")
cmd = "gpg -o " & file1 & " -u FC1459C7 -r A01183A7 -e """ & file2 & """"
retval = Shell(cmd, vbNormalFocus)
End Sub
Private Function getFilesInDirectory(target Directory As String, filePattern As String) As Collection
Dim fileCollection As New Collection
Dim curFile As String
On Error GoTo noSuchDirectory
ChDir targetDirectory
On Error GoTo 0
curFile = Dir(filePattern)
Do Until curFile = ""
fileCollection.Add curFile, curFile
curFile = Dir()
Loop
Set getFilesInDirectory = fileCollection
Exit Function
noSuchDirectory:
MsgBox "Invalid Directory: " & targetDirectory
Set getFilesInDirectory = fileCollection
End Function
Private Function GetShortPath(longPath As String) As String
Dim chars As Long, shortPath As String
shortPath = String$(Len(longPath), 0)
chars = GetShortPathName(longPath, shortPath, Len(shortPath))
GetShortPath = Left$(shortPath, chars)
End Function
Idle_Mind
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private Sub Command1_Click()
Dim files As New Collection
Dim file As Variant
Dim path As String, pattern As String
Dim retval As Double
Dim cmd As String
Dim file1 As String
Dim file2 As String
' Delete all .gpg files in c:\qgnupg\
path = "c:\gnupg\"
pattern = "*.gpg"
Set files = getFilesInDirectory(path, pattern)
For Each file In files
Kill path & file
Next file
' change directory and execute command line using short file names
ChDir path
file1 = GetShortPath("c:\gnupg\" & Format(Now, "MMDDYY") & "MBI.mbi.gpg")
file2 = GetShortPath("f:\servcent\
cmd = "gpg -o " & file1 & " -u FC1459C7 -r A01183A7 -e """ & file2 & """"
retval = Shell(cmd, vbNormalFocus)
End Sub
Private Function getFilesInDirectory(target
Dim fileCollection As New Collection
Dim curFile As String
On Error GoTo noSuchDirectory
ChDir targetDirectory
On Error GoTo 0
curFile = Dir(filePattern)
Do Until curFile = ""
fileCollection.Add curFile, curFile
curFile = Dir()
Loop
Set getFilesInDirectory = fileCollection
Exit Function
noSuchDirectory:
MsgBox "Invalid Directory: " & targetDirectory
Set getFilesInDirectory = fileCollection
End Function
Private Function GetShortPath(longPath As String) As String
Dim chars As Long, shortPath As String
shortPath = String$(Len(longPath), 0)
chars = GetShortPathName(longPath,
GetShortPath = Left$(shortPath, chars)
End Function
Doh...since I converted the filenames to short ones, we no longer need the quotes around the second file name. It should be this then:
cmd = "gpg -o " & file1 & " -u FC1459C7 -r A01183A7 -e " & file2
It should work as it was though.
Idle_Mind
cmd = "gpg -o " & file1 & " -u FC1459C7 -r A01183A7 -e " & file2
It should work as it was though.
Idle_Mind
You could write a batch file that would wite a batch file that would write a batch file that would execute the program. Then your program could execute the first batch file which would run the second batch file which would write the third batch file which could (and here's the beatuy of the scheme) then *run itself*. That would be pretty cool. Do I get the points?
hi..
i have use the following
retval = Shell("net use z: \\pc19\test password /user:pc19\loginname", vbNormalFocus)
it work.. but how to i run it a silent mode??
thks..
i have use the following
retval = Shell("net use z: \\pc19\test password /user:pc19\loginname", vbNormalFocus)
it work.. but how to i run it a silent mode??
thks..
I Am running a VB Script that call a batch file like this
run = WshShell.Run ("\\sh-nav\vplogon\vplogon .bat",1,fa lse)
But when the Batch is finished i have a problem the "MS Dos window" is still on the user desktop and he have to close it Manualy ..
How can i set that the Window will be closed automaticly ? like (Close on exit flag)
run = WshShell.Run ("\\sh-nav\vplogon\vplogon
But when the Batch is finished i have a problem the "MS Dos window" is still on the user desktop and he have to close it Manualy ..
How can i set that the Window will be closed automaticly ? like (Close on exit flag)
Why when i do: Shell("c:\mybat.bat", vbMaximizedFocus) i get an error (i can't say it exactly because it is in greek) that says that it can't use the file because it is used by another proccess???
just try running the shell command.
Hope this will help you
Hope this will help you
Private Sub Command1_Click()
Dim retval As Double
retval = Shell("gpg -o c:\gnupg\outsourceone_rsmd
"bc ca\outsourceone_rsmd_mr_el
End Sub
Idle_Mind