|
-
Jul 23rd, 2004, 03:41 AM
#1
Thread Starter
New Member
sending dos commands to a dos prompt
hi guys,
is there anyway of sending dos commands to a dos prompt using vb? i don't want to execue a batch file using the shell function...this is not what i want. All i want is to send dos commands to the same dos session. plz help.
thanks,
zaf
-
Jul 23rd, 2004, 08:46 AM
#2
Once a Dos Shell is up and running Im not sure how you would pull that off, but I know you can call the command shell 1 time with another DOS command as an argument and it will run it for you once.
-
Jul 23rd, 2004, 09:14 AM
#3
Addicted Member
Easy enough. This works to do a CD and DIR and waits for an Enter to exit:-
Code:
Sub test()
Enter = "~"
retval = Shell("c:\winnt\system32\cmd.exe", vbNormalFocus)
SendKeys "CD C:\TEMP" & Enter, True ' change folder
SendKeys "DIR" & Enter, True ' dir
SendKeys "EXIT". True
End Sub
Regards
BrianB
-------------------------------
-
Jul 23rd, 2004, 09:17 AM
#4
Thread Starter
New Member
Thanks guys, "sendkey" seems to have done the trick.
zaf
-
Jul 23rd, 2004, 09:27 AM
#5
Originally posted by BrianB
Easy enough. This works to do a CD and DIR and waits for an Enter to exit:-
Code:
Sub test()
Enter = "~"
retval = Shell("c:\winnt\system32\cmd.exe", vbNormalFocus)
SendKeys "CD C:\TEMP" & Enter, True ' change folder
SendKeys "DIR" & Enter, True ' dir
SendKeys "EXIT". True
End Sub
This is not DOS. Does this also work on a DOS prompt? DOS is run from Win9x like "Command.com", not "Cmd.exe".
Last edited by Dave Sell; Jul 23rd, 2004 at 09:32 AM.
-
Jul 23rd, 2004, 09:38 AM
#6
Thread Starter
New Member
-
Jul 23rd, 2004, 09:43 AM
#7
Cool, thanks. Can this work with the DOS window minimized?
-
Jul 23rd, 2004, 09:50 AM
#8
Thread Starter
New Member
the command prompt must have focus, else the text will not get printed in the dos prompt:
AppActivate "command.com" 'Command Prompt title bar
zaf
-
Jul 24th, 2004, 01:07 AM
#9
SendKeys - Arrrg! SendKeys is so flakey that if the user
accidentally changes the active application or presses a key, it
will throw SendKeys out of sync.
ShellExecute CDM.exe with parameters of what you want to pass or
execute in the DOS window.
Ex. this will create a text file of your c drive contents and close the
command window. This is very fast, better than using vb commands
to iterate through all the directories and out to a text file.
VB Code:
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 As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Sub Command1_Click()
Dim lRet As Long
lRet = ShellExecute(0, "OPEN", "c:\winnt\system32\cmd.exe", "/C Dir >> C:\Dir_C.txt", "C:\", SW_SHOWNORMAL)
End Sub

HTH
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|