Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open an Explorer window in a given directory from cmd.exe?

I see how to launch many other programs from a batch file, but I can't find a command like open on Mac OS X. Does such a tool exist on Windows? Powershell, or a Windows API call from an executable would also work.

Or, put another way, how can I invoke Windows default "Open" handler for a file from a batch file or powershell script?

like image 602
Robert Karl Avatar asked Aug 03 '10 21:08

Robert Karl


People also ask

How do I open Windows Explorer from command prompt?

Open Start. Search for Command Prompt and click the top result to open the console. Type the following command and press Enter: explorer.

How do I get Windows Explorer to open in a certain folder?

With File Explorer open, tap or click the File option at the top of the window and choose Change folder and search options. Once the Folder Options window opens, tap or click the dropdown box for Open File Explorer to and make your choice. Hit OK to save it.

How do I navigate to a folder in command prompt?

If the folder you want to open in Command Prompt is on your desktop or already open in File Explorer, you can quickly change to that directory. Type cd followed by a space, drag and drop the folder into the window, and then press Enter. The directory you switched to will be reflected in the command line.

How do I open Explorer EXE?

In the Task Manager window, click the “File” menu and then click “New Task (Run…)”. In the Create New Task window, type “explorer.exe” into the “Open” box and then click “OK.” Your taskbar and notification area should reappear and hopefully, whatever problem you were having will be resolved. You can close Task Manager.


2 Answers

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" 
like image 93
Leniel Maccaferri Avatar answered Sep 23 '22 01:09

Leniel Maccaferri


The direct equivalent of OS X's open is start in cmd.

start foo.txt 

would launch Notepad (or whatever text editor you're using),

start http://example.com 

would launch your browser,

start \someDirectory 

will launch Explorer, etc.

Care has to be taken with arguments in quotes, as start will interpret the first quoted argument as the window title to use, so something like

start "C:\Users\Me\Folder with spaces\somedocument.docx" 

will not work as intended. Instead prepend an empty quoted argument in that case:

start "" "C:\Users\Me\Folder with spaces\somedocument.docx" 

Note that start isn't a separate program but a shell-builtin. So to invoke this from an external program you have to use something like

cmd /c start ... 

The equivalent in PowerShell is either Start-Process or Invoke-Item. The latter is probably better suited for this task.

Invoke-Item foo.txt  # launches your text editor with foo.txt Invoke-Item .        # starts Explorer in the current directory 

As for the Windows API, you're looking for ShellExecute with the open verb.

like image 30
Joey Avatar answered Sep 23 '22 01:09

Joey