Recent

Author Topic: Running 'Exec'-command does not output intended output  (Read 197 times)

Wings2018

  • New Member
  • *
  • Posts: 18
Running 'Exec'-command does not output intended output
« on: December 06, 2024, 02:27:08 pm »
Hello world,

I tried to apply the 'Exec'-command to derive laptop parameters to be used in one of my programs. Unfortunately I'm not able to detect the defined output files on my laptop.
In case somebody can point out what's wrong in below source code that would be great.
In case I run both commands directly by cmd -> set > e:\set_test.txt or wmic bios > e:\bios_test.txt I get the desired output files without a problem.

program test (input,output);

uses

  crt, dateutils, dos, math, strutils, sysutils, video;

var

  iDosError:              integer;

  wDosExitCode:           word;

begin

  { begin of initialization }

  iDosError:=              32767;

  wDosExitCode:=           65535;

  { end of initialization }

  Exec('set',' > e:\set_test.txt');

  wDosExitCode:= lo(DosExitCode);

  iDosError:= DosError;

  Exec('wmic','bios > e:\bios_test.txt');

  wDosExitCode:= lo(DosExitCode);

  iDosError:= DosError;

end.

On first 'Exec'-command the parameter values become:
  wDosExitCode:= 2 => file does not exist
  iDosError:= 0 => no problems


On second 'Exec'-command the parameter values become:
  wDosExitCode:= 0 => no problems
  iDosError:= 234 => runtime error 'More data is available'

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11941
  • FPC developer.
Re: Running 'Exec'-command does not output intended output
« Reply #1 on: December 06, 2024, 02:32:04 pm »
Exec executes a commando. It does not provide shell commands like piping and redirection.

The common way in the dos era to do this is to query the COMSPEC variable and then run over a shell.

Or use Runcommand in the process unit if you don't need the Dos era baggage.

Fibonacci

  • Hero Member
  • *****
  • Posts: 604
  • Internal Error Hunter
Re: Running 'Exec'-command does not output intended output
« Reply #2 on: December 06, 2024, 02:35:38 pm »
Or

Code: Pascal  [Select][+][-]
  1. Exec('cmd', '/c set > e:\set_test.txt');

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: Running 'Exec'-command does not output intended output
« Reply #3 on: December 06, 2024, 02:38:31 pm »
Or

Code: Pascal  [Select][+][-]
  1. Exec('cmd', '/c set > e:\set_test.txt');
Are you sure about the leading "/c"?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Fibonacci

  • Hero Member
  • *****
  • Posts: 604
  • Internal Error Hunter
Re: Running 'Exec'-command does not output intended output
« Reply #4 on: December 06, 2024, 02:40:22 pm »
Are you sure about the leading "/c"?

100% sure

Wings2018

  • New Member
  • *
  • Posts: 18
Running 'Exec'-command does not result in intended output
« Reply #5 on: December 06, 2024, 02:46:25 pm »
Hello marcov and Fibonacci,

Thanks for your super-fast responses :). I'm going to work with them, and get back later.

Hello Zvoni,

I just tried Exec('cmd', '/c set > e:\set_test.txt') with the '/c' which results in the right output. Without the '/c' only literally 'cmd' gets executed and the program stops at the black colored DOS-window.

All enjoy your weekends!

 

TinyPortal © 2005-2018