Hello to all vb programmers. I need a little help on how to run a dos command line using visual basic. I have this dos command line:

C:\Documents and Settings\nagatron> echo Main-Class: ProgressBarStep >manifest.txt

where it create a manifest.txt file inside the directory "C:\Documents and Settings\nagatron". How can I execute this COMMAND in VB6? I have a code here:

Private Sub Command1_Click()
Dim command As String
command = "C:\Documents and Settings\nagatron> echo Main-Class: ProgressBarStep >manifest.txt"
Shell "cmd.exe /c command"
End Sub

but it didn't work

I believe line 4 should be a concatenation like so:

Shell "cmd.exe /c " & command

Not sure if your command will execute, but at least the Shell part will be right. Good luck!

Hi,

You need to create a Batch file and shell the batch file...

something like this :

    Dim FN As Integer
    FN = FreeFile
    Open "C:\Mybatc.bat" For Output
    Print #FN, "<YOUR DOS COMMAND HERE>"
    Print #FN, "Exit"
    Close #FN
    AppActivate Shell("C:\Mybatc.bat")

Regards
Veena

commented: The Batch file is not needed +0

Make sure "Windows Script Host Object Model" is selected
in project references, and then use the following code:

Dim wshThisShell As WshShell
Dim lngRet As Long
Dim strShellCommand As String
Dim strBatchPath As String

Sub C0ding()
Set wshThisShell = New WshShell
strBatchPath = "C:\Your Bat File"
strShellCommand = """" & strBatchPath & """"
lngRet = wshThisShell.Run(strShellCommand, vbNormalFocus, vbTrue)
End Sub

Private Sub Command1_Click()
C0ding
End Sub

I Love this code, this is the one i always use with all my batch files :-)

Dim udtShellEX As SHELLEXECUTEINFO
Dim lRet As Long, myCmd As String

myCmd = "/c "C:\Documents and Settings\nagatron> echo Main-Class: ProgressBarStep >manifest.txt"
udtShellEX.lpVerb = "open"
udtShellEX.nShow = SW_SHOWNORMAL
udtShellEX.lpParameters = myCmd

udtShellEX.lpFile = "cmd.exe"
udtShellEX.cbSize = Len(udtShellEX)
lRet = ShellExecuteEX(udtShellEX) 'launch prog


It test ok

correct in post

Dim udtShellEX As SHELLEXECUTEINFO
Dim lRet As Long, myCmd As String

myCmd = "/c C:\Documents and Settings\nagatron> echo Main-Class: ProgressBarStep >manifest.txt"
udtShellEX.lpVerb = "open"
udtShellEX.nShow = SW_SHOWNORMAL
udtShellEX.lpParameters = myCmd
udtShellEX.lpFile = "cmd.exe"
udtShellEX.cbSize = Len(udtShellEX)
lRet = ShellExecuteEX(udtShellEX) 'launch prog

It test ok

Do not include the prompt in the Command you want the Shell to execute. Just start at the echo command. You also need to have the command be concatenated, not part of the string, as noted earlier.

Private Sub Command1_Click()
    Dim command As String

    command = "echo Main-Class: ProgressBarStep > manifest.txt"
    Shell "cmd.exe /c " & command
End Sub

This will write the given text into the file manifest.txt, replacing anything that is already there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.