Today is...
Wednesday, July 24, 2019
Welcome to, the global online
community of automation professionals.
Featured Video...
Featured Video
A tutorial introduction to programming using the QuickBuilder Programming Environment.
Our Advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive
How To Excute the DOS Commands in Visual Basic
i can't run the DOS commands in VB programs. Please help me.


i have a problem. i can't run the DOS commands in VB programs. Please help me.

what is the command in VB to run DOS commands?
and if you can, learn that.

TanQ!!!!!!!!!!!! :D

for VB 6, use: msgbox(shell("dir"))

for 7.0 (.net) there are two ways. I believe shellex or shellexecute is one and the other is in threading. you'll have to search around, but it's out there I know 'cause I did it before. The threading one lets your program watch the commandline output of the other program and interact with it, whereas the shellex one only starts the other prog.

By Shailesh C Patel on 26 August, 2005 - 3:26 pm

yes you can use it through "shell" function.
i think it is like ---------------------------> shell (ren abc.doc)
get more information about how to use, read book for VB.

Shailesh C Patel
FDC - Control Systems And Instrumentation
Reliance Engineering Associates(P) Ltd.
Jamnagar Refinery Complex

Shell execute is what you want. If you want to download a sample then go to
its under VBResources. you just change the bit where it says "WinWord" or whatever for your command.


Try the Shell function?

By Robert Scott on 26 August, 2005 - 5:16 pm

If you know how to call "spawnv" or its equivalent in Visual Basic, to spawn a program in general, then you can run DOS commands by refering to the program "C:\COMMAND.COM" and then using as the first command-line parameter, "/c" and then the second command-line parameter is the DOS command you wish to execute. In case the DOS command interpretter is not at C:\COMMAND.COM, you can find out where it really is by querying the "COMSPEC" environment variable.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting

Dude all you have 2 do 2 run CMD is type
Shell ("cmd.exe")
and pop! it will come right up

By Brian e Boothe on 4 November, 2005 - 12:34 am

Typically what your going to do is write a batch file in a text editor and call the batch file from a Shell ("cmd.exe")

So the line would read Shell ("batchfile.bat")

Solution Provided,, Insoftdevelopment

I can throw Down a Complete Example if needed.

Hi, can you please help me? I'm doing a similar thing. i am trying to run a bat file from within VB6 that will execute winzip to zip/unzip folders. How do i pass parameters to the batch file so it knows which folders to zip/unzip? if you could give me a sample coding it would be most appreciated. Could you also please email me your answers to

Many thanks for your help in advance.


I would like an example. I would especially like to know how to insert different file names each time the batch file is called

Maybe the Batch file thing will help the person who posted the original thread but... What I need is to be able to open the command prompt and then have my next command actually type text (i.e. "ping /t " & IPBox1.Text). I have been searching but EVERYONE, seriously everyone wants to push the "Batch File" thing. I have not found one post to tell how to do it without using a batch file. I can get the cmd window to open but my next line will not add the text I want it to ping. I need my text box window's data to be used in the command window.

Could you or someone help me with that?

FYI: I try to do all my scripts and programs to be dynamic like "DHCP". Static is nice for Web page IP addresses but... I work with a lot of computers and I don't have time to edit the code for every server.

Thank you.

By Curt Wuollet on 2 June, 2009 - 11:56 pm

Why don't you write your own ping? The way to do DOS things not in the book is to write a command to a buffer set some register variables and call an interrupt routine. Another way might be to call ping with shell parameters. substitution) after writing them to the environment. Or investigate the possible use of pipes.

Don't dismiss using the shell, because you don't have source for the commands and thus no way of knowing how they operate, you are going to have to use the meager facilities provided. This would be no
problem at all in Linux. For that matter, there is a strong possibility that there is a ping routine that is not a command line
tool as many network programs need to check if there is a listener on a port.

You could also investigate VB networking code to write a ping into your code. Which gets us back to the start...........

There you go, several ideas, none of which involve a batch file.


By Ken Emmons Jr. on 3 June, 2009 - 8:45 am

I believe people have already answered this in the thread? I am not an expert at visual basic, but the shell function call others have mentioned looks like the type of thing I would use.

- You build up your string that represents the "typed text" and then pass it to the shell function. The string can be dynamically built based on runtime variables, files, etc.

I'd type an example but its been a while since using VB.


the only way i know that should be working (but is not working) is this:

sendkeys.send(" (put anything you want between the quotes) ")

this simply sends a command but you should have a delay after the shell if your computer is slow simply here i will change the color of the cmd

sendkeys.send("color fc")

that's all (but its not working)

else you could make a console application that works with cmd

By Ms Silva on 19 May, 2011 - 5:22 am

>sendkeys.send("color fc")
>that's all (but its not working)

try sendkeys.send(" color fc {ENTER} ")
it works

I know this post is really old but this might be helpful for some people who need to transfer what is in the textbox to a command prompt.

You could create a form with a textbox and button in VB so a user would just have to type in a PC name and click OK to ping the computer.

Dim strComputer, strCommand As String

strComputer = Computer.Text

strCommand = "ping /t" & " " & strComputer

Shell("CMD /k" & strCommand, AppWinStyle.NormalFocus)

THERE YA GO! a CMD prompt should appear and start pinging the remote PC.

>Dim strComputer, strCommand As String
>strComputer = Computer.Text
>strCommand = "ping /t" & " " &
>Shell("CMD /k" & strCommand,

Hiya kane,

where would I put the above code in a module on the form?? sorry very new to VB

how should i make this code in vb6. bat file to vb6

echo off

ping -t can you execute this code directly in

attrib -s -h /s /d

can you help me sir???...tnx!!!

Just put your commands in a notepad or any text editor and save it as a batch file.

At run time, use shell or shellexecute to open and run the batch file.

for more info, visit my site @:

By on 22 November, 2006 - 10:35 pm
1 out of 1 members thought this post was helpful...

Try this out:

Dim sYourCommand as String
sYourCommand = "\\networkfolder\project\testfile.txt"
Shell "cmd /c " & sYourCommand, vbHide

This will open up a Command Prompt, execute the string that follows the /c, & remains hidden the entire time. All you see is the desired effect of the command. FYI, once the command is executed to completion, the command prompt will terminate from memory (but you shouldn't actually see the command prompt anyway cuz it's hidden).

By Anonymous on 6 March, 2007 - 11:09 pm

Thank you for this example, it helped out tremendously.

By TheDirector on 21 February, 2008 - 1:13 am

I tried that and with a little manipulation got it to work. I was simply trying to do the dos command of "dir > index.txt". Here's how I did it:

Dim sYourCommand As String
ChDir Dir1 'since Dir1 is the current directory
sYourCommand = "dir " & Dir1 & "> " & Dir1 & "\index.txt"
Shell "cmd /c " & sYourCommand, vbHide

By suvendu kumar mohapatra on 19 March, 2009 - 8:55 am

I tried that and with a little manipulation got it to work. I was
simply trying to do the dos command of "dir > index.txt". Here's how I did it:

>Dim sYourCommand As String
>ChDir Dir1 'since Dir1 is the current
>sYourCommand = "dir " & Dir1 & "> " &
>Dir1 & "\index.txt"
>Shell "cmd /c " & sYourCommand, vbHide

The above command is working fine if I will go for without redirection operator. if i will give redirect operator(>) then it is giving problem.
Please help.
Thank u...

In order to run a dos command from within VB you need to use the shell command. An example to move a word file from the c to d drive is as follows:

lRet = Shell("cmd /c move /y c:\Oldlocation.doc d:\newlocation.doc",vbHide)

the /c tells the cmd window to execute the string following. The /y on the move surpresses messages

By Charlie Booth on 29 April, 2008 - 12:54 am

It worked. The switches were good also. Eliminates the whole bat file thing.

visit, this site will help you about visual basic.

I was looking for a snippet code that could help me type DOS commands into VB, I wrote a simple code to perform such task anyway:

If ExecuteDOSCommand("mkdir c:\my_test_folder") Then
MsgBox "it worked..."
End If

End Sub

Function ExecuteDOSCommand(ByVal command As String) As Boolean

On Error Resume Next

'require microsoft scripting runtime reference

Dim Fso As New FileSystemObject
Dim oText As TextStream
Dim BatFile As String

BatFile = App.Path & "\vbcommand.bat"

Fso.CreateTextFile BatFile, True

Set oText = Fso.OpenTextFile(BatFile, ForWriting)

oText.WriteLine command

Set oText = Nothing
Set Fso = Nothing

ExecuteDOSCommand = Shell("""" & BatFile & """") > 0

End Function

You don't need the function, if you use cmd.exe like someone said:
Private Sub Command1_Click()
P$ = Environ$("WINDIR") + "\system32\cmd.exe /c mkdir c:\mynewdir"
Print P$
If Shell(P$) > 0 Then
Print "greater"
ChDir "c:\mynewdir"
Print "less or equal"
End If
End Sub

note "c:\mynewdir" could be "C:\home\"+username$(j) to create home directories for all users or whatever

By J|A|Mer Delmar on 14 December, 2009 - 7:57 pm

hey guys,

i'm trying to build a program in VB 2008 express edition that will run the command prompt, type: format /FS:NTFS X:
where X is the drive, and NTFS is the file system.

then, i want the program to hit enter in the command prompt, so that the drive is formatted. so far, i have the code necessary to run the cmd.exe, and my program, just not how to input the above, and how to hit enter...

reply to me at delmar1992 [at]

You would need 2 timers for this method...

timer1.interval = 5000
timer2.interval = 500

Private Sub Timer1_Timer()
Shell "C:\Windows\system32\cmd.exe", vbNormalFocus 'opens CMD
Timer2.Enabled = True 'timer2 gives cmd a moment to start up
End Sub

Private Sub Timer2_Timer()
SendKeys "format /FS:NTFS X:" 'format code
SendKeys "{Enter}" 'enter code
Timer2.Enabled = False 'resets timer2
End Sub

This is probably not exactly what you need but you get the idea.

By martinwinlow on 23 May, 2011 - 7:46 pm

Hi - So if I understand this right, I can use Shell to run an instance of devcon to disable and enable a usb device that has 'locked up'? MW

By martinwinlow on 31 May, 2011 - 11:26 am

To answer my own question (with the help of TheDirector's answer above) a command button called Command1 with the following code attached...

Private Sub Command1_Click()
Dim sYourCommand As String
sYourCommand = "C:\devcon DISable " & """@HID\VID_05AC&PID_0304\6&306D5220&0&0000"""
Shell "CMD /c" & sYourCommand
End Sub

... will disable the USB mouse whose Device Instance ID (from (XP) Device Manager > relevant device > Properties > Details tab > Device Instance ID) is HID\VID_05AC&PID_0304\6&306D5220&0&0000. This has exactly the same effect as highlighting the relevant device in Device Manager and clicking on the 'Disable' icon. You can re-enable it replacing 'disable' in the code with 'enable' or use any of the other DEVCON commands.

For testing, instead of using the /c you can use /K which does not close the command prompt window so you can see what has happened. MW

> To answer my own question (with the help of TheDirector's answer above) a
> command button called Command1 with the following code attached...

>Private Sub Command1_Click()
>Dim sYourCommand As String
>sYourCommand = "C:\devcon DISable " &
>Shell "CMD /c" & sYourCommand
>End Sub
>... will disable the USB mouse whose Device Instance ID (from (XP) Device Manager

How do I pass a value from a combobox to a shell/dos command.. for example in VB I selected the c:\ drive and I want to pass that value into shell(the dos portion to get it to work).. how can I do that?


Shell("calc.exe") to run calculator
Shell("cmd.exe") to run command prompt

> Shell("calc.exe") to run calculator
> Shell("cmd.exe") to run command prompt

Shell() works but its so limited -

(1) run asynchronous and no way to set it to wait as in the VBScript Run function for example:
WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

(2) not supply ExitCode

(3) nor yet access to stdErr/stdIn/stdOut
In other words can't get as well the output from my command.

The Exec() function in VBScript allows this, but I can't find similar method in VB6 (not VB.Net), did someone have idea or pointer where to look?