How to open Explorer with a specific file selected?
Easiest way without using Win32 shell functions is to simply launch explorer.exe with the /select
parameter. For example, launching the process
explorer.exe /select,"C:\Folder\subfolder\file.txt"
will open a new explorer window to C:\Folder\subfolder with file.txt selected.
If you wish to do it programmatically without launching a new process, you'll need to use the shell function SHOpenFolderAndSelectItems
, which is what the /select
command to explorer.exe will use internally. Note that this requires the use of PIDLs, and can be a real PITA if you are not familiar with how the shell APIs work.
Here's a complete, programmatic implementation of the /select
approach, with path cleanup thanks to suggestions from @Bhushan and @tehDorf:
public bool ExploreFile(string filePath) {
if (!System.IO.File.Exists(filePath)) {
return false;
}
//Clean up file path so it can be navigated OK
filePath = System.IO.Path.GetFullPath(filePath);
System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
return true;
}
Reference: Explorer.exe Command-line switches
Open explorer on a file
From Geoff Chappell's The Windows Explorer Command Line
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
Opening a folder in explorer and selecting a file
Use this method:
Process.Start(String, String)
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
For example:
in CMD:
explorer.exe -p
in C#:
Process.Start("explorer.exe", "-p")
Open File explorer from command line in Command Prompt
Either start .
or explorer .
will open Explorer in the current folder.
From PowerShell it's Invoke-Item .
, ii .
, or also explorer .
.
Open Windows Explorer and perform a search
I got it working. It was in fact very simple. Assume I am searching for files with filename containing the term "mat" in folder "C:\temp1". The explorer window has the title "CustomSearch". It works in Windows 10.
string searchLink = "search-ms:displayname=CustomSearch&crumb=filename%3A*mat%20&crumb=location:C%3A%5Ctemp1";
Process.Start(searchLink);
Open Windows Explorer directory, select a specific file (in Delphi)
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;
Open a folder with File explorer using .bat
You can try like this:
%SystemRoot%\explorer.exe "c:\Yaya\yoyo\"
How do I open an Explorer window in a given directory from cmd.exe?
In Windows you can open Explorer with the following command:
C:\Users\Leniel>start %windir%\explorer.exe
If you want it to open a specific folder, do this for example:
C:\Users\Leniel>start %windir%\explorer.exe "C:\Users\Leniel\Desktop"
Open File Explorer at specified folder using R (or specifically R Studio) in Windows
The utils::browseURL()
function is part of base R, since the utils
package is installed as part of R. The function opens a URL, which can be of a website or a local folder.
So to open the current working directory in a File Explorer window:
utils::browseURL(getwd())
# or any other folder
utils::browseURL("myfolder/myfolder2/")
Opening directory in file explorer from WSL2
Answer is in post, but I will type it here again.
Put this function in your ~/.bashrc
start(){
path=$(wslpath -w "$1")
/mnt/c/Windows/explorer.exe "$path"
}
Now when you type start "Some Path" you will open it in file explorer. You can also remove /mnt/c/Windows/ from /mnt/c/Windows/explorer.exe if you want.
PTH (Path to here): Basically what my problem was that I was trying to read user input for the path to recreate the start command from cmd and powershell, but in wsl2 that was a lot harder because it doesn't have GUI so it doesn't know how to open it using xdg-open or other tools. Using read command from bash was not good enough because of the newline it always gives the user to type, but this uses arguments and takes the next thing you type in bash instantly which is awesome. Functions in bash work with arguments like lets say programs in c where you type ./program arg1 arg2 arg3..., where in bash it is the same, the number indicating the argument, so $0 is the zero-th argument which is always the name, so we don't use it. Starting from $1 $2 $3 and so on are the arguments which are usable in bash functions. In our case typing "start Desktop/", $1 is assigned "Desktop/", which is then converted to C:\Users<Your Username>\Desktop and assigned to $path. Then $path is passed to /mnt/c/Windows/explorer.exe to finally open in file explorer. Pretty nifty right? That's what I said first time 1 minute ago when I first saw and understood bash functions.
Related Topics
Django-Registration & Django-Profile, Using Your Own Custom Form
Text Box with Line Wrapping in Matplotlib
Django What Is Reverse Relationship
How to Set Ticks on Fixed Position , Matplotlib
Prevent Python from Caching the Imported Modules
Find the Division Remainder of a Number
Reverse a Get_Dummies Encoding in Pandas
How to Install Pip for Python 2.6
Py_Initialize/Py_Finalize Not Working Twice with Numpy
Copy File or Directories Recursively in Python
Is There a Module for Balanced Binary Tree in Python's Standard Library
Is There an Expression for an Infinite Iterator
Using Self.Xxxx as a Default Parameter - Python
When Do You Use 'Self' in Python