Recent

Author Topic: Run the command in commadline in pascal  (Read 16144 times)

mohsen

  • New Member
  • *
  • Posts: 11
Run the command in commadline in pascal
« on: July 10, 2016, 10:56:35 pm »
hello,im biginer in pascal
how can i write program that run the command in commadline
i read this topic ,but dident help me
"http://wiki.freepascal.org/Executing_External_Programs"
thank you

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #1 on: July 10, 2016, 11:00:54 pm »
Exactly what command you want to run?

RunCommand('explorer.exe'); // this command in Windows will run the explorer, for example

is like a command line, but it's not visible the command line window.

Code: Pascal  [Select][+][-]
  1. uses
  2.   Process;
  3.  
  4. {$R *.lfm}
  5.  
  6. { TForm1 }
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   s: string;
  11. begin
  12.   RunCommand('explorer.exe', s);
  13. end;  

You need to add 'Process' in the uses section, like in the code snippet.

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #2 on: July 10, 2016, 11:11:52 pm »
"s" What to do             

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #3 on: July 10, 2016, 11:14:20 pm »
"s" What to do             

If the process you execute return some data it will be stored in 's' as text (string).


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Run the command in commadline in pascal
« Reply #4 on: July 10, 2016, 11:16:12 pm »
Runcommand documentation:
Quote
...
function RunCommand(
  const cmdline: string;
  var outputstring: string
):Boolean;
...
outputstring   
     String containing the output of the process

In case you are using Lazarus and have the help installed:
1) open a new project or open an existing one
2) on an appropriate position type runcommand
2) position cursor somewhere on the text runcommand
3) press F1
4) help should show about the command/text that was 'underneath' your cursor.
« Last Edit: July 10, 2016, 11:21:19 pm by molly »

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #5 on: July 10, 2016, 11:25:32 pm »
if i want go to directory like this 'G:\' this is work?!

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #6 on: July 10, 2016, 11:32:59 pm »
i want go to specify directory and run the command
how can i do this?

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #7 on: July 10, 2016, 11:37:24 pm »
if i want go to directory like this 'G:\' this is work?!

Again, if you say what do yo want to do it will be easy for us to help you. What command you wish to create?

Code: Pascal  [Select][+][-]
  1. uses
  2.   Process, LCLIntF;
  3.  
  4. {$R *.lfm}
  5.  
  6. { TForm1 }
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   s: string;
  11. begin
  12.   // This has a parameter to open the E:\ drive
  13.   RunCommand('explorer.exe', ['/open,"E:\"'],s);
  14.  
  15.   // Run someprogram.exe that is in E:\ drive
  16.   RunCommandInDir('E:\', 'someprogram.exe',s);
  17.  
  18.   // Open a file in E:\ drive
  19.   OpenDocument('E:\somefile.txt');
  20. end;

OpenDocument uses LCLIntF unit.

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #8 on: July 10, 2016, 11:54:46 pm »
oh my feriend excuse me i want go to directory and run the command order in command prompt in pascal
i need working with orders of comand prompt in specify directory i need ,in pascal
i read topic that was working with aprocess.add() ,but doesent work for me,
excuse me i cant speak english well

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #9 on: July 10, 2016, 11:57:15 pm »
i want creat application that conecting 7 zip
and running this order
https://www.youtube.com/watch?v=Y7HnXQMP4-

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #10 on: July 10, 2016, 11:58:45 pm »
that orders enterd in above link ,i want enterd with pascal aplication

i cant working with command prompt in pascal
please help me
« Last Edit: July 11, 2016, 12:00:27 am by mohsen »

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #11 on: July 11, 2016, 12:19:04 am »
What language you speak?

Ok this video
Code: Pascal  [Select][+][-]
  1. https://www.youtube.com/watch?v=Y7HnXQMP4-I

Code: Pascal  [Select][+][-]
  1. uses
  2.   Process;
  3.  
  4. {$R *.lfm}
  5.  
  6. { TForm1 }
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   szip: TProcess;
  11. begin
  12.   // This creates a Process object that is usefull to run commands
  13.   szip := TProcess.Create(Self);
  14.  
  15.   // This is the directory you want to work with
  16.   szip.CurrentDirectory:='E:\';
  17.  
  18.   // This is 7z executable
  19.   szip.Executable:='C:\Program Files\7-Zip\7z.exe';
  20.  
  21.   // Here you add the parameters
  22.   szip.Parameters.Add('a'); // add
  23.   szip.Parameters.Add('-t7z'); // 7z format
  24.   szip.Parameters.Add('-r'); // recursive
  25.   szip.Parameters.Add('"E:\test.7z"'); // output file
  26.   szip.Parameters.Add('"E:\*.*"'); // all file types
  27.  
  28.   // Run the command
  29.   szip.Execute;
  30.  
  31.   // This free memory used with Process
  32.   szip.Free;
  33. end;  

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #12 on: July 11, 2016, 01:09:45 am »
I speak spanish, so we need to speak english or I will not understand you and you will not understand me :)

Hopefully the code is working for you.

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #13 on: July 11, 2016, 01:33:31 am »
thank you very much
thats work

mohsen

  • New Member
  • *
  • Posts: 11
Re: Run the command in commadline in pascal
« Reply #14 on: July 11, 2016, 02:47:38 pm »


procedure TForm1.Button1Click(Sender: TObject);
var
  szip: TProcess;
  dir:string;
  lenght:Integer;
  swap:string;
  swap2:string;
  swap3:string;
begin
OpenDialog1.Execute;
dir:=OpenDialog1.FileName;
lenght:=Length(dir);
swap:=LeftStr(dir,lenght-3);
swap2:='"'+swap+'7z'+'"';
//ShowMessage(swap2);
swap3:='"'+swap+'*.*'+'"';
// ShowMessage(swap3);
szip := TProcess.Create(nil);
szip.Executable:='"C:\Program Files\7-Zip\7z.exe"';

szip.Parameters.Add('7z');
szip.Parameters.Add('a'); // add
szip.Parameters.Add('-t7z'); // 7z format
szip.Parameters.Add(swap2);
szip.Parameters.Add(swap3);
szip.Execute;
szip.Free;
//szip.Options := [poUsePipes];
end;

it doesent work why?

 

TinyPortal © 2005-2018