Recent

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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12891
  • FPC developer.
Re: Run the command in commadline in pascal
« Reply #15 on: July 11, 2016, 02:58:04 pm »
Don't add ""'s to the paramters.add()'s.

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Run the command in commadline in pascal
« Reply #16 on: July 11, 2016, 03:00:33 pm »
it doesent work why?
You have commented out the ShowMessage() lines.
Could you un-comment them and tell me what they say??
(then you probably see why this doesn't work, for instance the extra dot in the wrong place)

After that gives us the complete command line you expect will be carried out.
(also note that if there is already a .7z file in your directory it will be included in the new 7z-file. Was that the intention?)

(debugging it like this will probably give you the answer already)

(Also... please always put code around code-tags)
[code]
Place your code here
[/code]

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Run the command in commadline in pascal
« Reply #17 on: July 11, 2016, 03:17:03 pm »
Don't add ""'s to the paramters.add()'s.
Doesn TProcess use MaybeQuoteIfNotQuoted()? (In which case it wouldn't matter if you added the ""s)
(But they are indeed not necessary because they are added automatically if needed)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12891
  • FPC developer.
Re: Run the command in commadline in pascal
« Reply #18 on: July 11, 2016, 04:54:00 pm »
Don't add ""'s to the paramters.add()'s.
Doesn TProcess use MaybeQuoteIfNotQuoted()? (In which case it wouldn't matter if you added the ""s)
(But they are indeed not necessary because they are added automatically if needed)

Hmm, indeed on windows it might not matter. Unix doesn't reconstruct a commandline though, so there it would be bad,

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #19 on: July 11, 2016, 05:19:20 pm »
This is working for me:

You've mixing concepts of what to store in each 'swap'. Also you've added an extra Add('7z') that's an invalid command.

Note with this code you're adding all files in folder where you choosed the file. In order to add only the file replace Add(swap3) with Add(dir).

Code: Pascal  [Select][+][-]
  1. uses
  2.   Process, LazFileUtils, LazUTF8;
  3.  
  4. {$R *.lfm}
  5.  
  6. { TForm1 }
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   szip: TProcess;
  11.   dir: string;
  12.   swap: string;
  13.   swap2: string;
  14.   swap3: string;
  15. begin
  16.   if OpenDialog1.Execute then
  17.   begin
  18.     dir := OpenDialog1.FileName;
  19.  
  20.     // This is a file without .ext
  21.     swap := FileUtil.ExtractFileNameWithoutExt(dir);
  22.     writeln(swap);
  23.  
  24.     // This is a file.7z
  25.     swap2 := swap + '.7z';
  26.     writeln(swap2);
  27.  
  28.     // This is a folder + \*.*
  29.     swap3 := LazUTF8.UTF8LeftStr(swap, UTF8Length(swap) - UTF8Length(ExtractFileNameOnly(swap))) + '*.*';
  30.     writeln(swap3);
  31.  
  32.     szip := TProcess.Create(nil);
  33.     szip.Executable := 'C:\Program Files\7-Zip\7z.exe';
  34.  
  35.     szip.Parameters.Add('a'); // add
  36.     szip.Parameters.Add('-t7z'); // 7z format
  37.  
  38.     // This will save in swap2 filename.7z
  39.     szip.Parameters.Add(swap2);
  40.  
  41.     // This will add all files in folder where is dir
  42.     szip.Parameters.Add(swap3);
  43.  
  44.     //szip.Options := [poUsePipes];
  45.     szip.Execute;
  46.     szip.Free;
  47.   end;

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Run the command in commadline in pascal
« Reply #20 on: July 11, 2016, 08:31:10 pm »
This is working for me:
Is it? Really? (With those writeln()s in a GUI-application?  ;D)

F.Y.I. there is also a ExtractFilePath() which just extracts the path. That one might be easier.

@mohsen, just remove the writeln()-lines and this example will work.

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Run the command in commadline in pascal
« Reply #21 on: July 11, 2016, 09:36:16 pm »
This is working for me:
Is it? Really? (With those writeln()s in a GUI-application?  ;D)

F.Y.I. there is also a ExtractFilePath() which just extracts the path. That one might be easier.

@mohsen, just remove the writeln()-lines and this example will work.

Really, Project > Options > Compiler options > Config and target > uncheck win32 gui application and you can debug with these writeln. Of course you already know it =)

Nice, I was looking for it but didn't find it. ExtractFilePath.

Code: Pascal  [Select][+][-]
  1. swap3 := ExtractFilePath(dir) + '*.*';

 

TinyPortal © 2005-2018