Recent

Author Topic: [SOLVED]cd before TProcess execute?  (Read 1636 times)

ShungFeng

  • New Member
  • *
  • Posts: 20
[SOLVED]cd before TProcess execute?
« on: March 13, 2020, 12:01:25 am »
Hello!

I am running Windows 10. I am trying to execute a command-line application (which simple outputs a text file) which is inside a folder in the current user's home folder (C:\Users\*\). On one computer, the following works fine:

Code: Pascal  [Select][+][-]
  1.   AProcess := TProcess.Create(nil);
  2.   AProcess.Executable := 'C:\Windows\System32\cmd.exe';
  3.   AProcess.Parameters.Add('/c');
  4.   AProcess.Parameters.Add('C:\Users\' + Username + '\MyFolder\App.exe >> C:\Users\' +
  5.     ComputerUsername + '\MyFolder\text_output.txt');
  6.   AProcess.Options := [poWaitOnExit];
  7.   AProcess.Execute;  

However, on another computer, the program will not run, as access is denied.

A manual solution is to change directory to MyFolder first and then run the program from the command line:

Quote
App.exe >> text_output.txt

So my question is: how can I cd to MyFolder and then run execute TProcess?

Thanks!
« Last Edit: March 13, 2020, 02:17:50 am by ShungFeng »

eljo

  • Sr. Member
  • ****
  • Posts: 468

jamie

  • Hero Member
  • *****
  • Posts: 6943
Re: cd before TProcess execute?
« Reply #2 on: March 13, 2020, 01:25:18 am »
One of your problems is that you are hard coding the paths to the user folders etc..

Not all computers have the same user folders.

my apps write to the user space and they all work from one pc to another but I don't hard code the names.

GetUserDir

and
GetAppConfigDir

functions.
look them up..
The only true wisdom is knowing you know nothing

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: cd before TProcess execute?
« Reply #3 on: March 13, 2020, 01:46:35 am »
Be aware that the directory returned by GetAppConfigDir is not correct for macOS as it treats macOS as if it was Linux/FreeBSD/Solaris.

See this Wiki article for a cross-platform version which complies with the Apple Guidelines for macOS.

ShungFeng

  • New Member
  • *
  • Posts: 20
Re: cd before TProcess execute?
« Reply #4 on: March 13, 2020, 02:17:37 am »
Thanks eljo.

jamie

  • Hero Member
  • *****
  • Posts: 6943
Re: [SOLVED]cd before TProcess execute?
« Reply #5 on: March 13, 2020, 02:25:06 am »
Well, I suspect he'll have more issues later one ;)

I used to do path hard coding and it is really a pain to keep up with MS and their games

For example. He should be using GetWindowsDIR not hardcoding it..

Etc. and so on.
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5992
  • Compiler Developer
Re: [SOLVED]cd before TProcess execute?
« Reply #6 on: March 13, 2020, 09:33:36 am »
So my question is: how can I cd to MyFolder and then run execute TProcess?

Use the CurrentDirectory property of TProcess before starting the process.

 

TinyPortal © 2005-2018