BAT file to open CMD in current directory
Asked Answered
N

18

62

I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):

cd %CD%
cmd.exe

I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.

Ninon answered 15/12, 2010 at 15:32 Comment(4)
Dos and cmd are two different things.Schoolmate
On Windows 10 there is an easy way for this: In Explorer navigate to your scripts (which you do anyway) then click "File" in upper left corner of Explorer window and then click on "Open command promt" --> "Open command prompt" OR "Open command prompt as administrator" ... Voila!Eaten
Another alternative is to use Cmder or ConEmu (which btw. are way better than cmd.exe). If properly installed they provide you a context menu in windows explorer (e.g. cmder here) which will bring up the console on current path. So no more need to copy & paste or type the path manually.Eaten
@TsetNoitamotua all Windows since Vista have "open command prompt window here" when pressing shift+right click in any folder. The same can be achived with shift+F10, shift+menu or just alt+D > cmd > enterCliffhanger
A
61

Create a file named open_dos_here.cmd with the following lines:

%~d1
cd "%~p1"
call cmd

Put this file at any folder. Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter). Create a shortcut to point to this open_dos_here.cmd

Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.

Albumen answered 16/12, 2010 at 14:28 Comment(7)
That is awesome! Only problem is that the window that opens does not use the size that I set. I set DOS to run in a wider-that-normal window and using this trick, it opens in what looks like a default-width window. Why is it falling back to the default?Ninon
You can go to the send-to folder. Find out the shortcut of open_dos_here.cmd. Then, right-click and select Properties. Then, change the Font, Layout, etc to suit your style. Also, as seen form others post, add double-quotes like this : "%~p1"Albumen
You should explain each line.Intelligible
Actually, when you right-click a file (e.g. C:\myFolder\myFile.txt) and select [ Send To ] and then the shortcut of open_dos_here.cmd, the OS will put the full path file name of the selected file as the first argument to call the batch file open_dos_here.cmd. So, inside the open_dos_here.cmd, it just uses this 1st argument (i.e. %1) to do the work. The first line %~d1 is to extract only the drive letter of %1 (e.g. C:). This goes to C drive. The 2nd line cd to %~p1 which is the path of %1 (i.e. \myFolder). After going to this directory, simply call the cmd to open the DOS command prompt.Albumen
@todofixthis what if I want to execute a dos command with this?Document
Actually, open_dos_here.cmd is to open an interactive DOS prompt shell in the target folder. This is the same as typing "cmd" in the command line of the folder window in Windows 7. The concept is to make the selected file as parameter-1 of open_dos_here.cmd. You can use the same concept to pass parameter-1 to your_own.bat or your_own.cmd file. At the end, add a "pause" command so that you can read the output.Albumen
This opens the cmd with the defaults for size and buffer ) :Halide
M
120

you probably want to do this:

cd /d %~dp0
cmd.exe

this will set your current directory to the directory you have the batch file in

Mansell answered 15/12, 2010 at 15:43 Comment(10)
Yes! It works!! Now where can I find an explanation for this?Ninon
'cd' is a system specific command - implemented by microsoft for their commond-line and '%~dp0' is simply the variable that holds the path the currently executing batch file is located in - therefore executing this command will take you to that directory (apparently this variable is only available from a batch file, which makes sense)Mansell
Chris Shouldn't that be: cd "%~dp0" With the double-quotes?Sacksen
Hi there, have you tried that? For me the upper solution works very well... maybe the quotes help if you have spaces in the file path, but I never had any problems with 'cd /d %~dp0'Mansell
This works fine, can you tell me where I can learn more similar commands?Piranha
@FrustratedWithFormsDesigner: Why don't you set this as the accepted answer, if it solved your problem? It certainly solved it for me!Michealmicheil
can you explain what is "/d"?Gentile
This opens the cmd with the defaults for size and buffer ) :Halide
Cool! Exactly what I needed then I did right-click + "Run As Administrator" on a .bat file in explorer (Window 10) which per default executes in C:\WINDOWS\system32 - this broke paths to other scripts that were called from within this .bat file. Putting cd "%~dp0" at the beginning of .bat file solves this problem!Eaten
Awesome answer @Mansell ... Can we go to parent directory of current directory using above... instead adding cd .. in batch file..??Woosley
A
61

Create a file named open_dos_here.cmd with the following lines:

%~d1
cd "%~p1"
call cmd

Put this file at any folder. Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter). Create a shortcut to point to this open_dos_here.cmd

Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.

Albumen answered 16/12, 2010 at 14:28 Comment(7)
That is awesome! Only problem is that the window that opens does not use the size that I set. I set DOS to run in a wider-that-normal window and using this trick, it opens in what looks like a default-width window. Why is it falling back to the default?Ninon
You can go to the send-to folder. Find out the shortcut of open_dos_here.cmd. Then, right-click and select Properties. Then, change the Font, Layout, etc to suit your style. Also, as seen form others post, add double-quotes like this : "%~p1"Albumen
You should explain each line.Intelligible
Actually, when you right-click a file (e.g. C:\myFolder\myFile.txt) and select [ Send To ] and then the shortcut of open_dos_here.cmd, the OS will put the full path file name of the selected file as the first argument to call the batch file open_dos_here.cmd. So, inside the open_dos_here.cmd, it just uses this 1st argument (i.e. %1) to do the work. The first line %~d1 is to extract only the drive letter of %1 (e.g. C:). This goes to C drive. The 2nd line cd to %~p1 which is the path of %1 (i.e. \myFolder). After going to this directory, simply call the cmd to open the DOS command prompt.Albumen
@todofixthis what if I want to execute a dos command with this?Document
Actually, open_dos_here.cmd is to open an interactive DOS prompt shell in the target folder. This is the same as typing "cmd" in the command line of the folder window in Windows 7. The concept is to make the selected file as parameter-1 of open_dos_here.cmd. You can use the same concept to pass parameter-1 to your_own.bat or your_own.cmd file. At the end, add a "pause" command so that you can read the output.Albumen
This opens the cmd with the defaults for size and buffer ) :Halide
C
30

You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.

Costermonger answered 28/6, 2012 at 12:5 Comment(4)
Just tried that, and the system tried to open up a new Internet Explorer tab at the addess http://cmd/ (which of course didn't help at all). Also, we don't have PowerShell installed here at work.Ninon
Ah. We've still got Windows XP here. I'll keep this tip in mind for home use.Ninon
Note sure if its Windows XP or Win 7 thing, but the suggestion was to type CMD into the "Explorer" address bar, not "internet explorer". This is the file browser you have open when in a folder. It will have the path of the folder in it. It works fine on Windows 7.Infiltrate
@Michael: That's what I tried: Type CMD in the address bar of Explorer (not Internet Explorer) and then that caused WinXP to try to open CMD as a URL in Internet Explorer. So I think it's safe to say this is a Win7+ thing.Ninon
A
16

There's a simpler way -

start /d "folder path"
Annihilation answered 13/5, 2014 at 13:57 Comment(1)
Works on Windows 10.Abdominal
N
7

As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx

In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).

Nnw answered 21/12, 2010 at 17:3 Comment(2)
Can this feature of Windows 7 somehow be changed in order to open PowerShell and not the CMD?Diploid
Looks like the link has changed to windows.microsoft.com/en-us/windows/xp-downloads#2TC=powertoysNinon
S
7

I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.

Better to add this line to your batch file too:

REM Change Current Directory to the location of this batch file 
CD /D %~dp0

unless you are fully qualifying all of your paths.

Sauder answered 4/12, 2012 at 0:5 Comment(2)
This worked perfectly for me, thank you. My problem was that I needed to execute a batch script that was written to assume Current Directory being set to the folder that contained the batch file. This line made that batch file run correctly no matter who runs it. Thanks!Slat
Ideal. Exactly what we needed too. Had a bat that needed admin access but also needs to assume current directory. Same as Cory, now runs correctly no matter who runs it.Showing
W
4

Referring to answer of @Chris,

We can also go to parent directory of batch file and run commands using following

cd /d %~dp0..
<OTHER_BATCH_COMMANDS>
cmd.exe

To understand working of command cd /d %~dp0.. please refer below link

What does it mean by command cd /d %~dp0 in Windows

Woosley answered 6/1, 2017 at 9:5 Comment(0)
A
3

Another solution is to use a shortcut file to cmd.exe instead of a batch file.

Edit the shortcut's start in property to %~dp0.

You achieve the same thing, except it has the Cmd icon (and you can change this).

Some people don't like clicking on batch files without knowing what's in them, and some corporate network drives have a ban on .bat files...

Asphodel answered 18/9, 2012 at 9:47 Comment(1)
This is the most simple and elegant answer.Thirion
A
3

The simplest command to do this:
start

You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.

Alainealair answered 16/6, 2014 at 20:38 Comment(0)
N
3

Most simple way in explorer is to Shift + right mouse click on the folder or on an empty space in the folder and click on Open command prompt here.

CMD will then start in that folder

I must say, I'm not sure if it works for Windows Vista and below, but it surely works for Windows 7, 8, 8.1 and 10.

Nonrigid answered 17/12, 2015 at 15:9 Comment(0)
E
3

When you are in the desired folder , just type CMD in your address bar

Echeverria answered 30/1, 2021 at 6:49 Comment(0)
H
2

You could add a context menu entry through the registry:

  1. Navigate in your Registry to HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell and create a key called "Command Prompt" without the quotes.

  2. Set the default string to whatever text you want to appear in the right-click menu.

  3. Create a new key within your newly created command prompt named "command," and set the default string to

    cmd.exe /k pushd %1
    

You may need to add %SystemRoot%\system32\ before the cmd.exe if the executable can't be found.

  1. The changes should take place immediately. Right click a folder and your new menu item should appear.

Also see http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

Hobnob answered 28/6, 2012 at 12:0 Comment(0)
R
1

A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.

Two solutions with the same first step: First navigate to the location you keep your scripts in and copy the filepath to that directory.

First Solution:

  • Click "Start"
  • Right-click "Computer" (or "My Computer)
  • Click "Properties"
  • On the left, click "Advanced System Settings"
  • Click "Environment Variables"
  • In the "System Variables" Box, scroll down and select "PATH"
  • Click "Edit"
  • In the "Variable Value" field, scroll all the way to the right
  • If there isn't a semi-colon (;) there yet, add it.
  • Paste in the filepath you copied earlier.
  • End with a semi-colon.
  • Click "OK"
  • Click "OK" again
  • Click "OK" one last time

You can now use any of your scripts as if you were already that folder.

Second Solution: (can easily be paired with the first for extra usefulness)

On your desktop create a batch file with the following content.

@echo off
cmd /k cd "C:\your\file\path"

This will open a command window like what you tried to do.


For tons of info on windows commands check here: http://ss64.com/nt/

Rewrite answered 27/3, 2014 at 21:57 Comment(0)
I
1

Create a new file startCmdLine.bat in your directory and put this line in it

call cmd

That is it. Now double click on the .bat file. It works for me.

You can replace call with start, it will also work.

Incomprehensible answered 23/7, 2014 at 12:37 Comment(0)
S
0

this code works for me name it cmd.bat

@echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
Skiest answered 23/6, 2014 at 16:37 Comment(0)
L
0

you can try:

shift + right click

then, click on Open command prompt here

Lanna answered 26/4, 2017 at 13:9 Comment(0)
H
0

Inside given folder click on the top Adddress Bar and type cmd and click enter It will open command prompt with current folder address.

Hephzipa answered 13/3, 2019 at 7:13 Comment(0)
G
-2

You can simply create a bat file in any convenient place and drop any file from the desired directory onto it. Haha. Code for this:

cmd
Gama answered 8/5, 2020 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.