can I use vb.net code to run an MSDOS command?

j00sh

Active member
Joined
Mar 29, 2007
Messages
35
Programming Experience
Beginner
:)messagetooshortblah

edit: I've done a little searching and all I can really find out so far is to use a batch file and use shell command to execute it but I don't think this will accomplish what I am wanting to do.
 
You can also use the Process class.
 
j00sh

Dont use Shell(), use System.Diagnostics.Process.Start() instead (Shell is old vb)



Some dos commands are built into CMD.exe (the dos prompt interpreter in XP) and to perform these commands you HAVE to tell CMD to /Carry them out for you:

System.Diagnostics.Process.Start("CMD.EXE /C " & yourCommand)

Other commands are implemented as separate EXE and these can be called normally:

System.Diagnostics.Process.Start("ATTRIB.EXE <arguments>")


Here is the help of CMD:
VB.NET:
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]
/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
/S      Modifies the treatment of string after /C or /K (see below)
/Q      Turns echo off
/D      Disable execution of AutoRun commands from registry (see below)
/A      Causes the output of internal commands to a pipe or file to be ANSI
/U      Causes the output of internal commands to a pipe or file to be
        Unicode
/T:fg   Sets the foreground/background colors (see COLOR /? for more info)
/E:ON   Enable command extensions (see below)
/E:OFF  Disable command extensions (see below)
/F:ON   Enable file and directory name completion characters (see below)
/F:OFF  Disable file and directory name completion characters (see below)
/V:ON   Enable delayed environment variable expansion using ! as the
        delimiter. For example, /V:ON would allow !var! to expand the
        variable var at execution time.  The var syntax expands variables
        at input time, which is quite a different thing when inside of a FOR
        loop.
/V:OFF  Disable delayed environment expansion.
Note that multiple commands separated by the command separator '&&'
are accepted for string if surrounded by quotes.  Also, for compatibility
reasons, /X is the same as /E:ON, /Y is the same as /E:OFF and /R is the
same as /C.  Any other switches are ignored.
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:
        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          the two quote characters
        - the string between the two quote characters is the name
          of an executable file.
    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
        and/or
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
Command Extensions are enabled by default.  You may also disable
extensions for a particular invocation by using the /E:OFF switch.  You
can enable or disable extensions for all invocations of CMD.EXE on a
machine and/or user logon session by setting either or both of the
following REG_DWORD values in the registry using REGEDT32.EXE:
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\EnableExtensions
        and/or
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions
to either 0x1 or 0x0.  The user specific setting takes precedence over
the machine setting.  The command line switches take precedence over the
registry settings.
The command extensions involve changes and/or additions to the following
commands:
    DEL or ERASE
    COLOR
    CD or CHDIR
    MD or MKDIR
    PROMPT
    PUSHD
    POPD
    SET
    SETLOCAL
    ENDLOCAL
    IF
    FOR
    CALL
    SHIFT
    GOTO
    START (also includes changes to external command invocation)
    ASSOC
    FTYPE
To get specific details, type commandname /? to view the specifics.
Delayed environment variable expansion is NOT enabled by default.  You
can enable or disable delayed environment variable expansion for a
particular invocation of CMD.EXE with the /V:ON or /V:OFF switch.  You
can enable or disable completion for all invocations of CMD.EXE on a
machine and/or user logon session by setting either or both of the
following REG_DWORD values in the registry using REGEDT32.EXE:
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion
        and/or
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion
to either 0x1 or 0x0.  The user specific setting takes precedence over
the machine setting.  The command line switches take precedence over the
registry settings.
If delayed environment variable expansion is enabled, then the exclamation
character can be used to substitute the value of an environment variable
at execution time.
File and Directory name completion is NOT enabled by default.  You can
enable or disable file name completion for a particular invocation of
CMD.EXE with the /F:ON or /F:OFF switch.  You can enable or disable
completion for all invocations of CMD.EXE on a machine and/or user logon
session by setting either or both of the following REG_DWORD values in
the registry using REGEDT32.EXE:
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\CompletionChar
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\PathCompletionChar
        and/or
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\CompletionChar
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\PathCompletionChar
with the hex value of a control character to use for a particular
function (e.g.  0x4 is Ctrl-D and 0x6 is Ctrl-F).  The user specific
settings take precedence over the machine settings.  The command line
switches take precedence over the registry settings.
If completion is enabled with the /F:ON switch, the two control
characters used are Ctrl-D for directory name completion and Ctrl-F for
file name completion.  To disable a particular completion character in
the registry, use the value for space (0x20) as it is not a valid
control character.
Completion is invoked when you type either of the two control
characters.  The completion function takes the path string to the left
of the cursor appends a wild card character to it if none is already
present and builds up a list of paths that match.  It then displays the
first matching path.  If no paths match, it just beeps and leaves the
display alone.  Thereafter, repeated pressing of the same control
character will cycle through the list of matching paths.  Pressing the
Shift key with the control character will move through the list
backwards.  If you edit the line in any way and press the control
character again, the saved list of matching paths is discarded and a new
one generated.  The same occurs if you switch between file and directory
name completion.  The only difference between the two control characters
is the file completion character matches both file and directory names,
while the directory completion character only matches directory names.
If file completion is used on any of the built in directory commands
(CD, MD or RD) then directory completion is assumed.
The completion code deals correctly with file names that contain spaces
or other special characters by placing quotes around the matching path.
Also, if you back up, then invoke completion from within a line, the
text to the right of the cursor at the point completion was invoked is
discarded.
The special characters that require quotes are:
     <space>
     &()[]{}^=;!'+,`~

And here is the HELP of the command line (type HELP at the dos prompt) that are not implemented in their own EXE (made by comparing the HELP list with the list of all exes in c:\windows\system32):
VB.NET:
ASSOC    Displays or modifies file extension associations.
BREAK    Sets or clears extended CTRL+C checking.
CALL     Calls one batch program from another.
CD       Displays the name of or changes the current directory.
CHCP     Displays or sets the active code page number.
CHDIR    Displays the name of or changes the current directory.
CLS      Clears the screen.
COLOR    Sets the default console foreground and background colors.
COPY     Copies one or more files to another location.
DATE     Displays or sets the date.
DEL      Deletes one or more files.
DIR      Displays a list of files and subdirectories in a directory.
DISKCOMP Compares the contents of two floppy disks.
DISKCOPY Copies the contents of one floppy disk to another.
ECHO     Displays messages, or turns command echoing on or off.
ENDLOCAL Ends localization of environment changes in a batch file.
ERASE    Deletes one or more files.
EXIT     Quits the CMD.EXE program (command interpreter).
FOR      Runs a specified command for each file in a set of files.
For more information on a specific command, type HELP command-name
forcedos.exe
FORMAT   Formats a disk for use with Windows.
FTYPE    Displays or modifies file types used in file extension associations.
GOTO     Directs the Windows command interpreter to a labeled line in a
GRAFTABL Enables Windows to display an extended character set in graphics
IF       Performs conditional processing in batch programs.
MD       Creates a directory.
MKDIR    Creates a directory.
MODE     Configures a system device.
MORE     Displays output one screen at a time.
MOVE     Moves one or more files from one directory to another directory.
PATH     Displays or sets a search path for executable files.
PAUSE    Suspends processing of a batch file and displays a message.
POPD     Restores the previous value of the current directory saved by PUSHD.
PROMPT   Changes the Windows command prompt.
PUSHD    Saves the current directory then changes it.
RD       Removes a directory.
REM      Records comments (remarks) in batch files or CONFIG.SYS.
REN      Renames a file or files.
RENAME   Renames a file or files.
RMDIR    Removes a directory.
SET      Displays, sets, or removes Windows environment variables.
SHIFT    Shifts the position of replaceable parameters in batch files.
START    Starts a separate window to run a specified program or command.
TIME     Displays or sets the system time.
TITLE    Sets the window title for a CMD.EXE session.
TYPE     Displays the contents of a text file.
VER      Displays the Windows version.
VERIFY   Tells Windows whether to verify that your files are written
VOL      Displays a disk volume label and serial number.
Any command in this list, must be CMD.EXE /C
All other commands can be run on their own
Consider carefully if you are using a command in this list, should you actually be using the proper .NET code to do it?
 
this is the code i have, it's working perfectly so far

VB.NET:
    Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click

        Dim path As String = "." ' default same path as this program

        If txtDir.Text <> "" Then ' if it's not blank, set dir to textbox
            path = txtDir.Text

        End If

        ' strip \ off end of path
        Dim intLength As Integer
        intLength = Microsoft.VisualBasic.Len(path)
        Dim strEnd As String = Microsoft.VisualBasic.Right(path, 1)
        If strEnd = "\" Then
            path = Microsoft.VisualBasic.Left(path, intLength - 1)
        End If

        'build array
        Dim dir As New DirectoryInfo(path)
        Dim f As FileInfo
        For Each f In dir.GetFiles("*.wav")
            Dim name As [String] = f.Name

            Dim sCommand As String
            sCommand = "c:\pvconv.exe -r ffr " & path & "\" & name
            Shell("cmd.exe /c " & sCommand)
        Next f
    End Sub
how would I modify this to do what you're saying?

like this?
VB.NET:
System.Diagnostics.Process.Start("c:\pvconv.exe -r ffr " & path & "\" & name)
 
A few general tips:
path.Length - using the Length property of the String class instead of old Len function.
path.Substring(params) - using the SubString function method of String class instead of old Left and Right functions.
Also you don't need to strip trailing backslash to use the path with DirectoryInfo.

You can declare the variable in the defining loop:
VB.NET:
For Each f As FileInfo In New DirectoryInfo(path).GetFiles("*.wav")
'working with each 'f'
Next
Instead of this block:
VB.NET:
Dim name As [String] = f.Name
Dim sCommand As String
sCommand = "c:\pvconv.exe -r ffr " & path & "\" & name
I would have written like this:
VB.NET:
Dim sCommand As String = "c:\pvconv.exe -r ffr " & f.FullName
Process.Start(sCommand)
 
well I strip it because in the cmd line it will write c:\whatever\name.wav, and if the person puts a \ in the directory text box without me stripping it, it will write c:\whatever\\name.wav and not work

edit: ah.. I see what you're saying though, using f.FullName instead of concatenating all that, gotcha

thanks for the other tips though :D I learned to use left/right/len in my vb class this week, didn't realize there was newer stuff, I'll have to slap my instructor
 
Also if you happened to get in a situation where you needed to combine paths you should use the IO.Path.Combine method.
 
anyways I published the working code and it works fine on my computer, but I send the install files to my brother (on the same os as me) and the button does nothing :/

edit: also the only way I can get the command to run is with the /c in there

everything I'm reading is saying to use clickonce for publishing but I don't see anything about that on the publish tab
 
Last edited:
If there is a space in the path you probably need to quote it so the commander read it as one parameter:
VB.NET:
Dim sCommand As String = "c:\pvconv.exe -r ffr """ & f.FullName & """"
There is another sometimes more readable way to code this also where you can put string parameters as {0} {1} {2} etc and list them after the string like this:
VB.NET:
Dim sCommand As String = String.Format("c:\pvconv.exe -r ffr ""{0}""", f.FullName)
 
well I can't get that to work still with the quotes, same error, so I'm sticking with shell for now

thanks for all the help though :) really appreciated

I think my program's pretty much done
 
Oops, someone didn't think earlier, with the Process the first parameter is the filepath, the second its parameters. Process.Start("filepath", "params")
 
Back
Top