Dos commands from delphi?????

Posted on 1999-10-20
Medium Priority
Last Modified: 2008-03-10
I am writing a delphi program that needs to execute
dos commands such as: MAP xx:=........
How do I execute those type of commands and checking
to see if the command was successfully excuted?
I tried using WinExec but it would execute it open
a dos window and close it immediately, i don't know
if the dos command executed successfully or not.
I put the dos command into a .bat file and execute
the .bat file.  However, I don't know if it executed
right or not. HELP!!!!

I also tried using the ShellExecute, same results, not sure
if i'm doing it right or not.

Question by:waipahu
  • 3
  • 2
  • 2
  • +5

Expert Comment

ID: 2142914
Do you need to be able to execute all available dos commands, or just a few specific ones?  What exactly do you need to use these commands for, there may be a better solution.


Author Comment

ID: 2142951
Right now I need to execute a MAP command.
Copy command

Expert Comment

ID: 2142983
You can use ShellExecute like this:

ShellExecute(Application.Handle, 'open', 'COMMAND.COM /C', ' MAP xxx', nil, ...

I don't know about the return value of MAP though.

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 27

Expert Comment

ID: 2143050
hi waipahu,

sounds like netware mapping.
in a dos shell will this not work,
i guess, after close the shell,
the mapping is gone.

you must do the mapping(s) over the win-api


Expert Comment

ID: 2143412
Doging the original question...
because it has been answered by Phil...

This might be helpfull :(dunno netware?)

To map to different PC harddrives use:

WNetAddConnection("\\Server\Path\", "pwd", "K:");

or WNetAdconnection2 :

procedure TForm1.Button1Click(Sender: TObject);

  with Net do
      dwScope := RESOURCE_CONNECTED ;
      dwType := RESOURCETYPE_DISK;
      lpLocalName := 'K:';
      lpRemoteName := PChar('\\ntdata\foldername');
      lpComment := Pchar('None');
      lpProvider := Nil;

 WNetAddConnection2(Net,'Password','RBertora',CONNECT_UPDATE_PROFILE );


to move files use:


where the filenames include the full path ie

to copy files use similar function copyfile.

Rob ;-)

Expert Comment

ID: 2143433
>>because it has been answered by Phil...

But you or meikl are providing the real help.  ;-)


Author Comment

ID: 2143517
Great!!! The WNetAddConnect worked great!!! Thanks
HOw do I disconnect the connection?

Hey, where do you get all these "WNet" commands?????? also
where can I find window commands for delphi???? There is so much
operating system commands that I want to do, but I don't know where
to get those type of information.

LVL 20

Expert Comment

ID: 2143549
I think it was WNetCancelConnection or something like this. If you want to know more, open the win32.hlp file (contained in your delphi\bin or delphi\help or delphi\mshelp directory). There you see the most important windows APIs. Most of them are already contained in Windows.pas so you can simply call them.

Regards, Madshi.

Author Comment

ID: 2143594
Great!!! The WNetAddConnect worked great!!! Thanks
HOw do I disconnect the connection?

Hey, where do you get all these "WNet" commands?????? also
where can I find window commands for delphi???? There is so much
operating system commands that I want to do, but I don't know where
to get those type of information.

LVL 17

Expert Comment

ID: 2143657
yep it is WNetCancelConnection2(pchar(SomeMappedDrive),0,True);
 to disconnect a mapped drive and for finding these WinAPI functions id advise getting the "tomes of delphi" series of books from amazon etc and also read the help file in :
delphi/borland shared/MShelp/win32.hlp  (24mgs in size)
it will keep you busy for quite some time ;-)
Regards Barry

Accepted Solution

RBertora earned 800 total points
ID: 2144628
Hi waipahu,
as Madshi said:

the win32.hlp file (contained in your delphi\bin or delphi\help or delphi\mshelp directory) is where you find this stuff... thought the examples are all in C you can do it all with delphi too.

I post as answer since my previous comment seemed
to help you.
Rob ;-)

Expert Comment

ID: 2157396
Well, addressing the original question :)  I'll leave you with this (I am the Author):
Many folks have asked on this list how to shut down a command line app
programmatically just like the Close on Exit checkbox in the property sheet
for a command-line/DOS app.  I don't know how many folks are aware of this technique, but
I've never noticed any significant answers to the question on this list.
Most of the time the issue centers around programmatically building a
shortcut to the dos app.  And while there have been many examples on how to
build a shortcut, I've not seen anyone specifically show how to
programmatically check that Close on Exit box.

Well for those of you needing a quick fix try this:

The Win95 command-line interpreter is called command.com and while it
accepts command-line arguments and even command-line switches there is no specific mention of them within the Win95 help.

The WinNT command-line interpreter is called cmd.exe and it also accepts
command-line arguments and switches, but it is fully detailed in the WinNT
help system.

Here is an excerpt:


Starts a new instance of the Windows/NT command interpreter

CMD [/X | /Y] [/A | /U] [/Q] [[/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
/Q      Turns the echo off
/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)
/X      Enable extensions to the Windows NT version of CMD.EXE
/Y      Disable extensions to the Windows NT version of CMD.EXE

Note that multiple commands separated by the command separator '&&'
are accepted for string if surrounded by quotes


Well, it just so happens that at the very least, the /c switch is also
valid under command.com under Win95.  Woo Hoo.  As I'm sure you can tell
already, the answer lies in running the command interpreter and sending it
command-line parameters, not in trying to run the DOS app and letting Win95
start the command interpreter and set it's own command-line parameters.
When you mix this with the use of the /c switch, voila'!

Thus, say you want to use pkunzip to get a listing of the contents and
automatically shut-down the command would be:

ExecuteAndWait('command.com /c pkunzip -v c:\path\zipfile.zip > zipview.txt')


You would then need something like this.  Note: Line Length = 150 chars

Name:         ExecuteAndWait
Description:  Executes an external executable and waits for its completion before continuing, returning the exit code of the process

function ExecuteAndWait(const CommandLine : string) : _DWORD;
  tSI : TStartupInfo;
  tPI : TProcessInformation;
  dw  : _DWORD;
  Result := 0;
  FillChar(tSI, sizeof(TStartupInfo), 0);
  tSI.cb := sizeof(TStartupInfo);
  if CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, tSI, tPI) then begin
    if WaitForSingleObject(tPI.hProcess, INFINITE) = WAIT_OBJECT_0 then begin
      if GetExitCodeProcess(tPI.hProcess, dw) then begin

        Result := dw;
        //if dw = 0 then begin                                                                       // sExe returned errorlevel 0, usually means Ok
        //  Result := True;
        //else begin                                                                                 // sExe returned something else, value in dw
  else begin
    // MessageBox 'CreateProcess Failed'
    // Result := False;

HTH with regard to anwering the ORIGINAL question!  :)

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question