Board index » delphi » Using Dos's copy command.

Using Dos's copy command.

I've written a program which uses the following commands.

 mkdir('pip');
 exec('command','/c copy sss.txt pip');

The mkdir command works fine but when I use the exec command given
above,
and then check the sub-directory "pip", it's empty. It should have
sss.txt in it. What's the problem??? Thanks in advance.

-Patrick-

 

Re:Using Dos's copy command.


I've written the following program.

--------------------------------------------
program mtest;
uses dos;
var orig:string[12];
begin
 orig:='sss.txt';
 mkdir('pip');
 exec('command','/c copy sss.txt pip');
 halt;
 end.
---------------------------------------------------

It successfully makes the subdirectory "pip" but it DOESN'T copy
"SSS.TXT" into it. I have "SSS.TXT" in the directory where the sub
directory is being created, but it doesn't get copied into pip and pip
stays empty. Why? Thanks in advance.

-Patrick-

Re:Using Dos's copy command.


Try this:

{$M $4000,0,0 }  {Dont take up all avalable memory!}

program mtest;

uses dos;

var
  i : Integer;
  CommandPath : string;
begin

 {This get command.com the full path name}
  for i := 1 to EnvCount do
    if Pos('COMSPEC=',EnvStr(i)) > 0 then begin
      CommandPath := EnvStr(i);
      Delete(CommandPath, 1 , 8);
    end;
  {$I-}
   mkdir('pip');
  {$I+}
   If IoResult <> 0 then begin
    {Do nothing - Directory already exists}
    {Io check must be made to clear io flag}
    {avoids runtime error}
   end;
   {Gota do this}
   SwapVectors;
   {Give path to command.com}
   exec(CommandPath,'/C copy gxclr16.pas pip');
   {Gota do this}
   SwapVectors;
 end.

Note : If you simply want to copy a file, take a look at the example for
BlcokRead() in the help file.

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/

Re:Using Dos's copy command.


Quote
Patrick D. Rockwell wrote:
> I've written the following program.

> --------------------------------------------
> program mtest;
> uses dos;
> var orig:string[12];
> begin
>  orig:='sss.txt';
>  mkdir('pip');
>  exec('command','/c copy sss.txt pip');
>  halt;
>  end.
> ---------------------------------------------------

> It successfully makes the subdirectory "pip" but it DOESN'T copy
> "SSS.TXT" into it. I have "SSS.TXT" in the directory where the sub
> directory is being created, but it doesn't get copied into pip and pip
> stays empty. Why? Thanks in advance.

There are several possible reasons.  First, you may not have left enough
memory available to DOS, so it can't run an external program.  Limit the
heap maximum using the $M directive.

Second, 'command' is a meaningless program name.  The program name you
want to call using the exec() procedure should be fully qualified,
meaning it should have a path if not in the current directory, and it
should also have a file extention.  However, rather than using a string
literal, the usual method is something like:

  exec (getenv('COMSPEC'),'/c sis.txt pip');

Third, make sure you add "swapvectors;" immediately before AND after the
call to exec().  This might not be necessary with calling COMMAND.COM,
but it can protect against some programs that may change interrupt
vectors and not restore them.

Also, whenever you call an external program, check the value of DosError
to see whether the call was successful or not, and if not, why didn't it
work.  Possible values are documented.

Quote
> -Patrick-

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:Using Dos's copy command.


In article <3372C036.4...@ix.netcom.com>,
Scott Earnest  <set...@ix.netcom.com> wrote:

Quote
>Patrick D. Rockwell wrote:

>> I've written the following program.

>> --------------------------------------------
>> program mtest;
>> uses dos;

>meaning it should have a path if not in the current directory, and it
>should also have a file extention.  However, rather than using a string
>literal, the usual method is something like:

>  exec (getenv('COMSPEC'),'/c sis.txt pip');

Ok. Thanks. Actually, I have both TP4.0 and BP7.01. I can make the
program work in BP7.01, but not TP4.0. The reason that I'm so interested
in this is because at one time, I was unable to get the EXEC command to work
for me. Then, when I got my new computer, it DID work. But then I deleted the
program which used it so, I don't remember EXACTLY why it worked for me
in TP4.0. Now, for some reason it didn't work, maybe for the reasons that you
gave.

TP4.0 doesn't have swapvectors, or getenv. :-)
-Patrick-

Re:Using Dos's copy command.


<<<<<<<<

Try this:

{$M $4000,0,0 }  {Dont take up all avalable memory!}

program mtest;

uses dos;

var
  i : Integer;
  CommandPath : string;
begin

 {This get command.com the full path name}
  for i := 1 to EnvCount do
    if Pos('COMSPEC=',EnvStr(i)) > 0 then begin
      CommandPath := EnvStr(i);
      Delete(CommandPath, 1 , 8);
    end;
  {$I-}
   mkdir('pip');
  {$I+}
   If IoResult <> 0 then begin
    {Do nothing - Directory already exists}
    {Io check must be made to clear io flag}
    {avoids runtime error}
   end;
   {Gota do this}
   SwapVectors;
   {Give path to command.com}
   exec(CommandPath,'/C copy gxclr16.pas pip');
   {Gota do this}
   SwapVectors;
 end.

Note : If you simply want to copy a file, take a look at the example for
BlcokRead() in the help file.

Quote

Ok. I tried it and it works. Thanks.

One other question, I was having the problem while writing under TP 4.0.
Is there a way that I can write the program so that it works under TP 4.0?
For some reason, my EXEC command DID work under TP 4.0, and now it
doesn't. TP 4.0 doesn't have a swapvector command or an envstr variable,
or an envcount
variable.

Thanks again in advance for any advice

Re:Using Dos's copy command.


Quote
patri48...@aol.com (Patri48975) wrote:
>One other question, I was having the problem while writing under TP 4.0.
>Is there a way that I can write the program so that it works under TP 4.0?
>For some reason, my EXEC command DID work under TP 4.0, and now it
>doesn't. TP 4.0 doesn't have a swapvector command or an envstr variable,
>or an envcount
>variable.

You'll find a GetEnv function in the util unit at
http://users.southeast.net/~rdonais/index.html.  Then instead of

Quote
>  for i := 1 to EnvCount do
>    if Pos('COMSPEC=',EnvStr(i)) > 0 then begin
>      CommandPath := EnvStr(i);
>      Delete(CommandPath, 1 , 8);
>    end;

Use the following for all versions of TP:

   CommandPath := GetEnv('COMSPEC');

Since swapvectors isn't available in TP 4.0 and you don't have
access to any saved interrupt vectors, you'll have to omit
swapvectors from your TP 4.0 program.  You can use a conditional
define and combine the above into:

   {$IFNDEF VER40} SwapVectors; {$ENDIF}
   {Get path and name of shell program (command.com) }
   exec(GetEnv('COMSPEC'),'/C copy gxclr16.pas pip');
   {$IFNDEF VER40} SwapVectors; {$ENDIF}

Naturally the above assumes that you have included DOS and UTIL in
the uses clause for the unit/program.

    ...red

Re:Using Dos's copy command.


Patrick D. Rockwell <prockw...@thegrid.net> wrote in article
<33712030.6...@thegrid.net>...

Quote
> I've written the following program.

> --------------------------------------------
> program mtest;
> uses dos;
> var orig:string[12];
> begin
>  orig:='sss.txt';
>  mkdir('pip');
>  exec('command','/c copy sss.txt pip');
>  halt;
>  end.
> ---------------------------------------------------

> It successfully makes the subdirectory "pip" but it DOESN'T copy
> "SSS.TXT" into it. I have "SSS.TXT" in the directory where the sub
> directory is being created, but it doesn't get copied into pip and pip
> stays empty. Why? Thanks in advance.

> -Patrick-

Use SwapVectors and the $M directive, there is a good example in the online
help of exec

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  mail to : mre...@ici.net                      
  WWW: http://www.ici.net/cust_pages/mrealm/BANDP.HTM
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
' It is better to die on your feet, than to live on your knees! '

Re:Using Dos's copy command.


Quote
> > I've written the following program.

> > --------------------------------------------
> > program mtest;
> > uses dos;
> > var orig:string[12];
> > begin
> >  orig:='sss.txt';
> >  mkdir('pip');
> >  exec('command','/c copy sss.txt pip');
> >  halt;
> >  end.
> > ---------------------------------------------------

> > It successfully makes the subdirectory "pip" but it DOESN'T copy
> > "SSS.TXT" into it. I have "SSS.TXT" in the directory where the sub
> > directory is being created, but it doesn't get copied into pip and pip
> > stays empty. Why? Thanks in advance.

> There are several possible reasons.  First, you may not have left enough
> memory available to DOS, so it can't run an external program.  Limit the
> heap maximum using the $M directive.

> Second, 'command' is a meaningless program name.  The program name you
> want to call using the exec() procedure should be fully qualified,
> meaning it should have a path if not in the current directory, and it
> should also have a file extention.  However, rather than using a string
> literal, the usual method is something like:

>   exec (getenv('COMSPEC'),'/c sis.txt pip');

> Third, make sure you add "swapvectors;" immediately before AND after the
> call to exec().  This might not be necessary with calling COMMAND.COM,
> but it can protect against some programs that may change interrupt
> vectors and not restore them.

> Also, whenever you call an external program, check the value of DosError
> to see whether the call was successful or not, and if not, why didn't > it work.  Possible values are documented.

    (Pssst, do you think we oughta tell him that the Halt statement
isn't needed - or of any value - here?)  8<}}

Re:Using Dos's copy command.


Quote
Mike Copeland wrote:
> > but it can protect against some programs that may change interrupt
> > vectors and not restore them.

> > Also, whenever you call an external program, check the value of DosError
> > to see whether the call was successful or not, and if not, why didn't > it work.  Possible values are documented.

>     (Pssst, do you think we oughta tell him that the Halt statement
> isn't needed - or of any value - here?)  8<}}

Yeah, I forgot to take the halt statement out before submiting that
program in my post. Oh well, I've solved my problem with the exec
procedure. Thanks!
-Patrick-

Re:Using Dos's copy command.


In article <19970509070301.DAA08...@ladder01.news.aol.com>,

Quote
Patri48975 <patri48...@aol.com> wrote:
><<<<<<<<

>Try this:

>{$M $4000,0,0 }  {Dont take up all avalable memory!}

>program mtest;

>uses dos;

>var
>  i : Integer;
>  CommandPath : string;
>begin

> {This get command.com the full path name}
>  for i := 1 to EnvCount do
>    if Pos('COMSPEC=',EnvStr(i)) > 0 then begin
>      CommandPath := EnvStr(i);
>      Delete(CommandPath, 1 , 8);
>    end;

What if the user has an environment variable

SCOMSPEC=foo

?

When you are going to reinvent the wheel, make sure that it is round.

Osmo

Re:Using Dos's copy command.


In article <3372C036.4...@ix.netcom.com>,
Scott Earnest  <set...@ix.netcom.com> wrote:

Quote

>Third, make sure you add "swapvectors;" immediately before AND after the
>call to exec().  This might not be necessary with calling COMMAND.COM,
>but it can protect against some programs that may change interrupt
>vectors and not restore them.

Any program that sets interrupt vectors and does not restore then is so
buggy that it is not worth executing. (I am not talking about TSR's
which should not be executed under exec() anyway)

Osmo

Re:Using Dos's copy command.


Quote
> >I've written a program which uses the following commands.

> > mkdir('pip');
> > exec('command','/c copy sss.txt pip');

> >The mkdir command works fine but when I use the exec command given
> >above,
> >and then check the sub-directory "pip", it's empty. It should have
> >sss.txt in it. What's the problem??? Thanks in advance.

> The syntax I have reads:-
>         exec('\command.com','/c copy [source file] [destination file]

> You should specify the paths to these files.

   That's still not quite right, since "COMMAND.COM" isn't likely to be
in the directory from where the program is being executed.  And you
shouldn't hard-code any path/file name into a program which has the
potential of being executed on other systems (where key files and paths
could well be different).  A far better way to handle the situation is
to do the following:

  Exec (GetEnv('COMSPEC'),'/C copy [source file] [destination file]')

   This uses the GetEnv function to obtain the system variable COMSPEC
to locate and use the system's command processor (which doesn't have to
be COMMAND.COM, BTW).

Re:Using Dos's copy command.


On Tue, 06 May 1997 17:47:01 -0700, "Patrick D. Rockwell"

Quote
<prockw...@thegrid.net> wrote:
>I've written a program which uses the following commands.

> mkdir('pip');
> exec('command','/c copy sss.txt pip');

>The mkdir command works fine but when I use the exec command given
>above,
>and then check the sub-directory "pip", it's empty. It should have
>sss.txt in it. What's the problem??? Thanks in advance.

>-Patrick-

The syntax I have reads:-
        exec('\command.com','/c copy [source file] [destination file]

You should specify the paths to these files.

Frank Wood
wood...@dircon.co,uk

Re:Using Dos's copy command.


In article <33823d5c.8002...@news.dircon.co.uk>, Frank Wood <woodf-
l...@dircon.co.uk> writes

Quote
>On Tue, 06 May 1997 17:47:01 -0700, "Patrick D. Rockwell"
><prockw...@thegrid.net> wrote:

>>I've written a program which uses the following commands.

>> mkdir('pip');
>> exec('command','/c copy sss.txt pip');

You seem to have missed out the SwapVectors command...look it up in the
pascal help.
--
Richard Adams

Turnpike evaluation. For information, see http://www.turnpike.com/

Go to page: [1] [2]

Other Threads