Member Login
member
passwd
remember me on this computer.

- join now -

Search

Neat Stuff

Visit our shop for nerds in control lifestyle products.

Cool stuff
Select a topic of interest:
...and press:
Fortune
The faster we go, the rounder we get.
-- The Grateful Dead
RSS Feed
RSS feed Use this link to get an RSS feed of the Control.com article flow, for private, non-commercial use only:
www.control.com/rss
Select a Page Style
Select one of the following styles:
- BluFu
- Classic
(cookies required)
from the Programming languages department...
How To Excute the DOS Commands in Visual Basic
Languages topic
advertisement
Posted by Anonymous on 25 August, 2005 - 2:40 pm
hello,

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

Posted by Anonymous on 26 August, 2005 - 3:13 pm
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.

Posted 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.zip abc.doc)
get more information about how to use, read book for VB.

Regards,
Shailesh C Patel
FDC - Control Systems And Instrumentation
Reliance Engineering Associates(P) Ltd.
Jamnagar Refinery Complex
Jamnagar,
India
Ph:-0288-3011683

Posted by DrogoNevets on 30 September, 2005 - 1:09 am
Shell execute is what you want. If you want to download a sample then go to http://www.sharemation.com/drogonevets
its under VBResources. you just change the bit where it says "WinWord" or whatever for your command.

Drogo

Posted by Anonymous on 26 August, 2005 - 5:14 pm
Try the Shell function?
Shell(pathname[,windowstyle])

Posted 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

Posted by Acid_9X on 31 October, 2005 - 5:05 pm
Hey,
Dude all you have 2 do 2 run CMD is type
Shell ("cmd.exe")
and pop! it will come right up

Posted 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.

Posted by jassy on 25 April, 2006 - 1:07 am
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 jasmine.ali@eds.com.

Many thanks for your help in advance.

jasmine

Posted by Anonymous on 28 September, 2006 - 1:24 am
I would like an example. I would especially like to know how to insert different file names each time the batch file is called

Posted by Xigma on 14 September, 2006 - 11:35 pm
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 @:

xigmasystems.bravehost.com

Posted by drewvancamp.com on 22 November, 2006 - 10:35 pm
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).

Posted by Anonymous on 6 March, 2007 - 11:09 pm
Thank you for this example, it helped out tremendously.

Posted 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

Posted by Zmark on 14 January, 2007 - 12:30 pm
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

Posted by Charlie Booth on 29 April, 2008 - 12:54 am
It worked. The switches were good also. Eliminates the whole bat file thing.

Posted by edgar on 15 January, 2007 - 5:53 pm
hello,
visit www.planetsourcecode.com, this site will help you about visual basic.

Posted by Vitor on 9 November, 2007 - 1:06 am

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
oText.Close

Set oText = Nothing
Set Fso = Nothing

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

End Function

Posted by ED BERRY on 7 March, 2008 - 12:34 am
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"
Else
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

From Control Engineering magazine...
Related articles from Control Engineering magazine
Above articles copyright 2008 Reed Business Information. Subject to its Terms of Use.

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2008 Control Technology Corporation. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, PostgreSQL and Apache. Be happy.

Advertisement
Our Advertisers
Help keep our servers running...
Patronize our advertisers!