How To Excute the DOS Commands in Visual Basic

A

Thread Starter

Anonymous

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

Shailesh C Patel

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
 
R

Robert Scott

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
 
B

Brian e Boothe

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 [email protected].

Many thanks for your help in advance.

jasmine
 
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
 
I would like an example. I would especially like to know how to insert different file names each time the batch file is called
 
D

drewvancamp.com

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).
 
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
 
<p>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:
<pre>
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 </pre>
 
T
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
 
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
 
S

suvendu kumar mohapatra

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

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