I have a procedure to open a folder in Windows Explorer that gets passed a directory path:
procedure TfrmAbout.ShowFolder(strFolder: string);
begin
ShellExecute(Application.Handle,PChar('explore'),PChar(strFolder),nil,nil,SW_SHOWNORMAL);
end;
Is there a way to also pass this a file name (either the full file name path or just the name + extension) and have the folder open in Windows Explorer but also be highlighted/selected? The location I am going to has many files and I need to then manipulate that file in Windows.
Click the first file or folder you want to select. Hold down the Shift key, select the last file or folder, and then let go of the Shift key. Hold down the Ctrl key and click any other file(s) or folder(s) you would like to add to those already selected.
By default, File Explorer opens to Quick Access. If you'd rather have File Explorer open to This PC, go to the View tab and then select Options. In the Open File Explorer to list, select This PC, and then select Apply.
There are three ways you can open File Explorer from the Start menu. The first is to click the Start button, and then scroll down the list of apps and click “Windows System.” In the submenu, click “File Explorer” to open it. You can also pin File Explorer to the sidebar above the Start button.
You can change directories in Command Prompt by using the cd command and running the start . command once in the desired directory to open that folder in File Explorer. However, you can also open any folder in File Explorer by running the start command, followed by the path of the folder you'd like to open.
Yes, you can use the /select
flag when you call explorer.exe
:
ShellExecute(0, nil, 'explorer.exe', '/select,C:\WINDOWS\explorer.exe', nil,
SW_SHOWNORMAL)
A somewhat more fancy (and perhaps also more reliable) approach (uses ShellAPI, ShlObj
):
const
OFASI_EDIT = $0001;
OFASI_OPENDESKTOP = $0002;
{$IFDEF UNICODE}
function ILCreateFromPath(pszPath: PChar): PItemIDList stdcall; external shell32
name 'ILCreateFromPathW';
{$ELSE}
function ILCreateFromPath(pszPath: PChar): PItemIDList stdcall; external shell32
name 'ILCreateFromPathA';
{$ENDIF}
procedure ILFree(pidl: PItemIDList) stdcall; external shell32;
function SHOpenFolderAndSelectItems(pidlFolder: PItemIDList; cidl: Cardinal;
apidl: pointer; dwFlags: DWORD): HRESULT; stdcall; external shell32;
function OpenFolderAndSelectFile(const FileName: string): boolean;
var
IIDL: PItemIDList;
begin
result := false;
IIDL := ILCreateFromPath(PChar(FileName));
if IIDL <> nil then
try
result := SHOpenFolderAndSelectItems(IIDL, 0, nil, 0) = S_OK;
finally
ILFree(IIDL);
end;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With