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

Dani AI

Generated

If your goal is just to create the text file, you do not need CMD at all. VB6 can write it directly, which avoids quoting issues and the dependency on the current directory.

Private Sub Command1_Click()
    Dim f As Integer, path As String
    path = "C:\Documents and Settings\nagatron\manifest.txt"
    f = FreeFile
    Open path For Output As #f
    Print #f, "Main-Class: ProgressBarStep"
    Close #f
End Sub

If you prefer to invoke CMD, quote any path with spaces and either target the file by full path or change to the directory for the duration of the command. Using a full path is simplest:

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

Notes:

  • Redirection (>, >>) is parsed by cmd.exe, not VB6. Use >> to append instead of overwrite.
  • Quoting the output filename ensures paths with spaces are handled correctly.
  • If you must operate relative to a folder, you can wrap with pushd "C:\Documents and Settings\nagatron" && ... and then reference manifest.txt.
  • On newer Windows versions, writing under user profile or AppData is typically safer than system locations due to permissions.

Recommended Answers

All 6 Replies

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.