Hi
anyone tell me ?
can we use DOS Command in VB. If yes than How?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaganathan BantheswaranPosted Jul 13, 2011, 1:46 AM
Yes we can use DOS commands.
Some of the ways.
You can run another application by using the Shell statement.
'// this code calls c: est.exe
Shell "c:\test.exe", vbNormalFocus
You can also call any program in the windows directory, such as regsvr32, notepad, and explorer
So,
Shell "notepad", vbNormalFocus
Runs notepad, and
Shell "explorer", vbNormalFocus
runs explorer. You can also pass any command parameters you need. For example:
Shell "notepad C:\test.txt", vbNormalFocus
runs notepad, and tells it to open C:\test.txt. Note that if the path you pass contains spaces, you need to surround it by quotes:
Shell "notepad ""C:\dir with spaces\test.txt""", vbNormalFocus
which is actually the equivalent of passing this:
notepad " C:\dir with spaces\test.txt"
The second parameter specifies the startup position, and can be one of the following:
Constant - Value - Description
vbHide 0 Window is hidden and focus is passed to the hidden window.
vbNormalFocus 1 Window has focus and is restored to its original size and position.
vbMinimizedFocus 2 Window is displayed as an icon with focus.
vbMaximizedFocus 3 Window is maximized with focus.
vbNormalNoFocus 4 Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus 6 Window is displayed as an icon. The currently active window remains active.
The shell function also returns a TaskID (double), that you can use with the AppActivate statement:
Private dblNotePadID As Double
'// runs word
Private Sub cmdRunNotePad_Click()
dblNotePadID = Shell("notepad", vbNormalFocus")
End Sub
'// activates word at a later stage
Private Sub cmdActivateNotePad_Click()
AppActivate dblNotePadID
End Sub
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Sub Command1_Click()
ShellExecute Me.hwnd, vbNullString, "ExeName.Exe", vbNullString, "C:\", SW_SHOWNORMAL
End Sub
Sam HobbsPosted Jul 13, 2011, 5:14 PM
If the command is one of the few that the shell (usually cmd.exe) processes then you must do it in a manner such as what Vulpes shows. If not then you don't need to execute cmd.exe.
Also note that if the command is one of them that is executed by cmd.exe, then there is probably an equivalent .Net class you can use.
VulpesPosted Jul 13, 2011, 4:23 AM
For example, the following program runs the 'dir' command for the current directory, sorting the files by date order - oldest first, and redirects the output to a string which is then printed to the console and saved to a file called dir.txt: