Board index » delphi » How to exec system commands

How to exec system commands

How do I execute a system command like DEL?
I tried using exec('del', 'somefile.txt'), but it doesn't work.
 

Re:How to exec system commands


Quote
zhorce wrote:

> How do I execute a system command like DEL?
> I tried using exec('del', 'somefile.txt'), but it doesn't work.

DEL and DIR and COPY and CD etc. are commands built in the
COMMAND.COM (or another shell program, to make all the experts
listening happy). You must invoke COMMAND.COM or - more flexible -
the program which is defined in the COMSPEC variable.

Exec('COMMAND.COM','/C del somefile.txt');
or, as stated above:
Exec(GetEnv('COMSPEC'),'/C del somefile.txt');
The COMSPEC "knows" where COMMAND.COM is on your disk.

More in Timo's FAQ # 9 which you can download from a link
in the TP-links site, page "FAQ":
http://www.geocities.com/SiliconValley/2926/tp.html

Franz Glaser

Re:How to exec system commands


Why not try the DOS.TPU procedures????

If you want to DELETE a file try this

Assign(F,FileName);
Reset(F);
Erase(F);    

this will delete the file and its quicker than using Exec(Speed wise)

hope this helps

Re:How to exec system commands


Quote

> How do I execute a system command like DEL?
> I tried using exec('del', 'somefile.txt'), but it doesn't work.

You have to exec the shell:

Exec(GetEnv('COMSPEC'),'/C del *.*');

And don't forget the $M option!

Andras

Re:How to exec system commands


Try this:

exec('command.com /c','del anyfile.txt')

Quote
zhorce wrote in message <35D38696.713B4...@hotmail.com>...
>How do I execute a system command like DEL?
>I tried using exec('del', 'somefile.txt'), but it doesn't work.

Re:How to exec system commands


On Fri, 14 Aug 1998 11:23:21 +1000, Esky <aaron_rodg...@hotmail.com>
wrote:

Quote
>If you want to DELETE a file try this

Please don't.

Quote
>Assign(F,FileName);
>Reset(F);
>Erase(F);    
>this will delete the file and its quicker than using Exec(Speed wise)

It is an error to delete an _open_ file:

Assign(F,FileName);
Erase(F);

Regards
Horst

Re:How to exec system commands


Quote
Esky wrote:

> Why not try the DOS.TPU procedures????

> If you want to DELETE a file try this

> Assign(F,FileName);
> Reset(F);
> Erase(F);

> this will delete the file and its quicker than using Exec(Speed wise)

> hope this helps

He wanted to execute system commands, not del alone.

Re:How to exec system commands


DOS.TPU and CRT.TPU supports most dos system calls
ie, cd, md, rd, del, ren

Re:How to exec system commands


Quote
> Try this:

> exec('command.com /c','del anyfile.txt')

---    ^^^^^^^^^^^^^^^^
   This is a restrictive technique which not everyone can use (i.e. they
may no have "COMMAND.COM" running their system, or it may not be
accessible from where the Exec'd program is executing).  The
documentation (and my experience) suggests using more global code:

  Exec (GetEnv('COMSPEC'),'/C del anyfile.txt')

   Which uses the system variable "COMSPEC" (more available than
"COMMAND.COM") to find the system's command processor and use it to spawn
the child process.

Quote
> >How do I execute a system command like DEL?
> >I tried using exec('del', 'somefile.txt'), but it doesn't work.

Re:How to exec system commands


Quote
> Why not try the DOS.TPU procedures????

> If you want to DELETE a file try this

> Assign(F,FileName);
> Reset(F);
> Erase(F);    

> this will delete the file and its quicker than using Exec(Speed wise)

yes, it is MUCH faster, but i think i read somewhere in BP help, that file
MUST be closed before erase procedure is called.

Re:How to exec system commands


I know "GETENV", but what is $M function? Compiler directive? For what?

--
Sorry for bad english (I'm from Slovenia)!
Matija Polajnar

Papai Andras je v sporoilu <35D3D06B.3A7F6...@mol.hu> napisal ...

Quote

>> How do I execute a system command like DEL?
>> I tried using exec('del', 'somefile.txt'), but it doesn't work.

>You have to exec the shell:

>Exec(GetEnv('COMSPEC'),'/C del *.*');

>And don't forget the $M option!

>Andras

Re:How to exec system commands


Quote
> I know "GETENV", but what is $M function? Compiler directive? For what?

   It controls (allows you to override) the system defaults for Stack,
Heap_Min, and Heap_Max.  Useful for applications using pointers, Exec
calls, deep subprogram nesting/recursion, heavy uses of procedure-local
data, etc.  In short, if you program in TP/BP much, you'll usually find
occasion to use more or less than the standard compiler defaults for
these resources.
Quote
> >Exec(GetEnv('COMSPEC'),'/C del *.*');
> >And don't forget the $M option!

Re:How to exec system commands


Quote
Matija polajnar wrote:

> I know "GETENV", but what is $M function? Compiler directive? For what?

It is for memory allocation: {$M stack,min,max}
stack: the stack size used by the application
min: the minimum heap size, required by your app
max: the maximum heap size
Default is {$M 16384,0,655360}

If you want to use exec(), you have reduce the memory allocation.
If you don't use the heap, try {$M 16384,0,0}

Quote
> Sorry for bad english (I'm from Slovenia)!

Never mind.

Andras

Re:How to exec system commands


I agree.
I write large menu driven applications for clients.  My main menu program
contains {$M $4000} and each choice on the menu designates a different program
invoked by the exec command.  Without the $M, the main menu program would
consume all of the memory resources leaving none for the "sub" EXE programs.

Jim Culpepper

Re:How to exec system commands


I'm so stupid!!!! IN EVERY MY PROGRAM I USING $M (SO PROGRAM DO NOT USE ALL
OF THE FREE MEMORY AND OTHER PROGRAMS CAN RUNNING), AND NOW I ASKING, WHAT
IT IS? AM I NOT STUPID??????

--
Sorry for bad english (I'm from Slovenia)!
Matija Polajnar

Papai Andras je v sporoilu <35D9152F.CB2D7...@mol.hu> napisal ...

Quote
>Matija polajnar wrote:

>> I know "GETENV", but what is $M function? Compiler directive? For what?

>It is for memory allocation: {$M stack,min,max}
>stack: the stack size used by the application
>min: the minimum heap size, required by your app
>max: the maximum heap size
>Default is {$M 16384,0,655360}

>If you want to use exec(), you have reduce the memory allocation.
>If you don't use the heap, try {$M 16384,0,0}

>> Sorry for bad english (I'm from Slovenia)!
>Never mind.

>Andras

Go to page: [1] [2]

Other Threads