Recent

Author Topic: How to open Folder and select item?  (Read 2034 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 518
How to open Folder and select item?
« on: June 07, 2022, 05:44:57 am »
Hi dear

When I right-click an icon on the Windows desktop and open the file location, I go to the destination folder and then to the selected icon.
how can i implement this?
I can only open a folder or open a program like these code:
Code: Pascal  [Select][+][-]
  1.   OpenURL(edtDirectory.Text+'\'+Rd_Name);
  2.   SysUtils.ExecuteProcess(UTF8ToSys('explorer.exe'), stg100.Cells[2,stg100.Row], \['\']);// <= what is this ???
  3.   WinExec(PChar('explorer.exe /e,'+ stg100.Cells[2,stg100.Row]),SW_SHOWNORMAL);
  4.  
but I can't select the target icon?
« Last Edit: September 08, 2024, 02:07:25 pm by majid.ebru »

Coldzer0

  • Jr. Member
  • **
  • Posts: 53
Re: How to open Folder and select item?
« Reply #1 on: June 07, 2022, 06:40:01 am »
Here's a very simple way of doing this  8)

Code: Pascal  [Select][+][-]
  1. Program OpenFileLocation;
  2.  
  3. Uses
  4.   Windows,
  5.   shlobj,
  6.   ActiveX;
  7.  
  8. Var
  9.   SelectedFile : LPITEMIDLIST;
  10. Begin
  11.   CoInitializeEx(nil, COINIT_MULTITHREADED);
  12.   Try
  13.     SelectedFile := ILCreateFromPath('C:\Windows\System32\cmd.exe');
  14.     If SelectedFile <> nil Then
  15.     Begin
  16.       SHOpenFolderAndSelectItems(SelectedFile, 0, SelectedFile, 0);
  17.       ILFree(SelectedFile);
  18.     End;
  19.   Finally
  20.     CoUninitialize;
  21.   End;
  22. End.
  23.  

majid.ebru

  • Hero Member
  • *****
  • Posts: 518
Re: How to open Folder and select item?
« Reply #2 on: June 07, 2022, 10:49:33 am »
thank you Coldzer0 ;D ;D ;D ;D


but i should change this code:
Code: Pascal  [Select][+][-]
  1. SelectedFile := ILCreateFromPath(PChar( myAddress ));
  2.  
:) ;) :D

loaded

  • Hero Member
  • *****
  • Posts: 853
Re: [Solved] How to open Folder and select item?
« Reply #3 on: June 07, 2022, 11:17:39 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.    ExecuteProcess('explorer.exe', '/select,'+PChar('C:\Windows\System32\cmd.exe'), []);
  4. end;  
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

majid.ebru

  • Hero Member
  • *****
  • Posts: 518
Re: [Solved] How to open Folder and select item?
« Reply #4 on: August 10, 2022, 01:07:47 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.    ExecuteProcess('explorer.exe', '/select,'+PChar('C:\Windows\System32\cmd.exe'), []);
  4. end;  

thank you

but last code is faster that this code

majid.ebru

  • Hero Member
  • *****
  • Posts: 518
Re: How to open Folder and select item?
« Reply #5 on: August 28, 2024, 08:07:56 am »
Hi
please help and guide me

if file names contain UTF8 char , this code does not open the file?

Here's a very simple way of doing this  8)

Code: Pascal  [Select][+][-]
  1. Program OpenFileLocation;
  2.  
  3. Uses
  4.   Windows,
  5.   shlobj,
  6.   ActiveX;
  7.  
  8. Var
  9.   SelectedFile : LPITEMIDLIST;
  10. Begin
  11.   CoInitializeEx(nil, COINIT_MULTITHREADED);
  12.   Try
  13.     SelectedFile := ILCreateFromPath('C:\Windows\System32\cmd.exe');
  14.     If SelectedFile <> nil Then
  15.     Begin
  16.       SHOpenFolderAndSelectItems(SelectedFile, 0, SelectedFile, 0);
  17.       ILFree(SelectedFile);
  18.     End;
  19.   Finally
  20.     CoUninitialize;
  21.   End;
  22. End.
  23.  

majid.ebru

  • Hero Member
  • *****
  • Posts: 518
Re: How to open Folder and select item?
« Reply #6 on: September 08, 2024, 02:07:50 pm »
Hi
please help and guide me

Wallaby

  • Jr. Member
  • **
  • Posts: 96
Re: How to open Folder and select item?
« Reply #7 on: September 08, 2024, 02:14:48 pm »
Use the Unicode APIs:

Code: Pascal  [Select][+][-]
  1. Var
  2.   SelectedFile: PItemIDList;
  3. Begin
  4.   CoInitializeEx(nil, COINIT_MULTITHREADED);
  5.   Try
  6.     SelectedFile := ILCreateFromPathW(PWideChar(UTF8Decode('Z:\temp\хуй.pdf')));
  7.     If SelectedFile <> nil Then
  8.     Begin
  9.       SHOpenFolderAndSelectItems(SelectedFile, 0, SelectedFile, 0);
  10.       ILFree(SelectedFile);
  11.     End;
  12.   Finally
  13.     CoUninitialize;
  14.   End;
  15. End.      

paweld

  • Hero Member
  • *****
  • Posts: 1219
Re: How to open Folder and select item?
« Reply #8 on: September 08, 2024, 02:28:48 pm »
you can solve this in 2 ways:
- way one is to change the code page from UTF8 to Windows - you need to add the LazUTF8 unit to the uses section, and to the dependencies you need to add the LCLBase or LCL package (if there is none)
- way two is to replace the path with WideString and use ILCreateFromPathW
Code: Pascal  [Select][+][-]
  1. program OpenFileLocation;
  2.  
  3. uses
  4.   Windows, shlobj, ActiveX,
  5.   {LazUTF8} //uncomment for way 1
  6.   ;
  7.  
  8. var
  9.   SelectedFile: LPITEMIDLIST;
  10. begin
  11.   CoInitializeEx(nil, COINIT_MULTITHREADED);
  12.   try
  13.     ////way1
  14.     //SelectedFile := ILCreateFromPath(PChar(UTF8ToWinCP(ParamStr(0))));
  15.     //way 2
  16.     SelectedFile := ILCreateFromPathW(PWideChar(WideString(ParamStr(0))));
  17.     if SelectedFile <> nil then
  18.     begin
  19.       SHOpenFolderAndSelectItems(SelectedFile, 0, SelectedFile, 0);
  20.       ILFree(SelectedFile);
  21.     end;
  22.   finally
  23.     CoUninitialize;
  24.   end;
  25. end.    
  26.  
Best regards / Pozdrawiam
paweld

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: How to open Folder and select item?
« Reply #9 on: September 09, 2024, 01:57:02 am »
- way two is to replace the path with WideString ...

Or better, UnicodeString.  You should stay away from WideString when you are not interacting directly with ActiveX/COM objects.  UnicodeString is more efficient than WideString for non-COM work.  You don't need COM to use ILCreateFromPathW() or SHOpenFolderAndSelectItems().
« Last Edit: September 09, 2024, 01:58:44 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018