BAT文件在当前目录中打开CMD。

时间:2021-01-22 02:06:33

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到它们所在目录的路径。这很乏味(它们在一个相当深的文件系统中,所以键入完整的路径是一种痛苦,复制+粘贴更好,但不是很多)。我尝试创建一个。bat文件,我可以双击它,在文件夹中打开一个新的命令行窗口,但它不起作用。它打开一个新窗口,但是工作目录不是.bat文件所在的目录。以下是我在google上搜索到的东西(我的cmd技能不是很好):

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.

从我使用Linux的时候,我知道Konqueror有一个“命令行窗口”特性,这就是我想要在Windows上得到的效果。

16 个解决方案

#1


40  

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

创建一个名为open_dos_here的文件。cmd有以下几行:

%~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

把这个文件放在任何文件夹。然后,转到你的发送到文件夹(Win+E;Alt + D;shell:sendto;输入)。创建指向这个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.

然后,在任何文件夹中,选择任何文件或子文件夹。右键单击并选择“发送到”,然后选择open_dos_here。cmd打开该文件夹中的DOS。

#2


83  

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

这将把当前目录设置为已包含批处理文件的目录。

#3


23  

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

您可以在浏览器中输入cmd到地址栏,它就会在这条路径中启动。PowerShell同样。

#4


8  

There's more simple way

有更简单的方法

start /d "folder path"

#5


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

作为一个更通用的解决方案,您可能想要查看Microsoft Power Toy (XP),当您右击: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).

在Vista和Windows 7中,如果按住shift键并右键单击(这是内置的),就会得到这个选项。

#6


5  

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.

除非你完全符合你的所有路径。

#7


2  

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

另一个解决方案是使用一个快捷文件到cmd。exe而不是批处理文件。

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

编辑快捷方式从属性开始到%~dp0。

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

您实现了相同的事情,除了它有Cmd图标(您可以更改它)。

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...

有些人不喜欢在不知道它们的情况下点击批处理文件,而一些公司的网络驱动器也禁止了。bat文件……

#8


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.

    在您的注册表中导航到HKEY_LOCAL_MACHINE/软件/类/文件夹/Shell,并创建一个名为“命令提示符”的键,没有引号。

  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.

您可能需要在cmd之前添加%SystemRoot%\system32\。如果无法找到可执行文件,请执行exe。

  1. The changes should take place immediately. Right click a folder and your new menu item should appear.
  2. 这些变化应该立即发生。右击一个文件夹,你的新菜单项就会出现。

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

也看到http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

#9


2  

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.

您可以在命令行中运行此命令,以便在相同的位置打开新的命令行窗口。或者你可以把它放到。bat文件中。

#10


2  

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.

在资源管理器中最简单的方法是按住鼠标右键点击文件夹或者在文件夹的空白区域,点击这里的Open命令提示符。

CMD will then start in that folder

CMD就会从那个文件夹开始。

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.

我必须说,我不确定它是否适用于Windows Vista和以下,但它肯定适用于Windows 7、8、8.1和10。

#11


2  

May be a little old quest, but for the ones like me googling for this, you can hold shift and right click the folder. That way the content menu ill show open command window here.

可能是一个小小的旧任务,但对于像我这样的谷歌搜索,你可以按住shift,右键点击文件夹。这样,内容菜单将显示打开的命令窗口。

#12


0  

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.

使用相同的第一步的两个解决方案:首先导航到保存脚本的位置,并将filepath复制到该目录。

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.
  • 在前面复制的filepath中粘贴。
  • End with a semi-colon.
  • 以分号结束。
  • Click "OK"
  • 单击“OK”
  • Click "OK" again
  • 再次点击“OK”
  • Click "OK" one last time
  • 最后一次点击“OK”。

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/

关于windows命令的大量信息,请访问这里:http://ss64.com/nt/。

#13


0  

this code works for me name it cmd.bat

这段代码为我命名为cmd.bat。

@echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop

#14


0  

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

创建一个新的文件startCmdLine。在你的目录中加入这条线。

call cmd

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

就是这样。现在双击.bat文件。它适合我。

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

你可以用start代替call,它也可以工作。

#15


0  

Referring to answer of @Chris,

关于@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

了解命令cd /d %~dp0的工作。请参考下面的链接

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

在Windows中,命令cd /d %~dp0意味着什么?

#16


0  

you can try:

你可以尝试:

shift + right click

shift +右击

then, click on Open command prompt here

然后,单击此处的Open命令提示。

#1


40  

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

创建一个名为open_dos_here的文件。cmd有以下几行:

%~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

把这个文件放在任何文件夹。然后,转到你的发送到文件夹(Win+E;Alt + D;shell:sendto;输入)。创建指向这个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.

然后,在任何文件夹中,选择任何文件或子文件夹。右键单击并选择“发送到”,然后选择open_dos_here。cmd打开该文件夹中的DOS。

#2


83  

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

这将把当前目录设置为已包含批处理文件的目录。

#3


23  

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

您可以在浏览器中输入cmd到地址栏,它就会在这条路径中启动。PowerShell同样。

#4


8  

There's more simple way

有更简单的方法

start /d "folder path"

#5


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

作为一个更通用的解决方案,您可能想要查看Microsoft Power Toy (XP),当您右击: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).

在Vista和Windows 7中,如果按住shift键并右键单击(这是内置的),就会得到这个选项。

#6


5  

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.

除非你完全符合你的所有路径。

#7


2  

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

另一个解决方案是使用一个快捷文件到cmd。exe而不是批处理文件。

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

编辑快捷方式从属性开始到%~dp0。

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

您实现了相同的事情,除了它有Cmd图标(您可以更改它)。

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...

有些人不喜欢在不知道它们的情况下点击批处理文件,而一些公司的网络驱动器也禁止了。bat文件……

#8


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.

    在您的注册表中导航到HKEY_LOCAL_MACHINE/软件/类/文件夹/Shell,并创建一个名为“命令提示符”的键,没有引号。

  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.

您可能需要在cmd之前添加%SystemRoot%\system32\。如果无法找到可执行文件,请执行exe。

  1. The changes should take place immediately. Right click a folder and your new menu item should appear.
  2. 这些变化应该立即发生。右击一个文件夹,你的新菜单项就会出现。

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

也看到http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

#9


2  

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.

您可以在命令行中运行此命令,以便在相同的位置打开新的命令行窗口。或者你可以把它放到。bat文件中。

#10


2  

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.

在资源管理器中最简单的方法是按住鼠标右键点击文件夹或者在文件夹的空白区域,点击这里的Open命令提示符。

CMD will then start in that folder

CMD就会从那个文件夹开始。

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.

我必须说,我不确定它是否适用于Windows Vista和以下,但它肯定适用于Windows 7、8、8.1和10。

#11


2  

May be a little old quest, but for the ones like me googling for this, you can hold shift and right click the folder. That way the content menu ill show open command window here.

可能是一个小小的旧任务,但对于像我这样的谷歌搜索,你可以按住shift,右键点击文件夹。这样,内容菜单将显示打开的命令窗口。

#12


0  

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.

使用相同的第一步的两个解决方案:首先导航到保存脚本的位置,并将filepath复制到该目录。

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.
  • 在前面复制的filepath中粘贴。
  • End with a semi-colon.
  • 以分号结束。
  • Click "OK"
  • 单击“OK”
  • Click "OK" again
  • 再次点击“OK”
  • Click "OK" one last time
  • 最后一次点击“OK”。

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/

关于windows命令的大量信息,请访问这里:http://ss64.com/nt/。

#13


0  

this code works for me name it cmd.bat

这段代码为我命名为cmd.bat。

@echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop

#14


0  

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

创建一个新的文件startCmdLine。在你的目录中加入这条线。

call cmd

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

就是这样。现在双击.bat文件。它适合我。

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

你可以用start代替call,它也可以工作。

#15


0  

Referring to answer of @Chris,

关于@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

了解命令cd /d %~dp0的工作。请参考下面的链接

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

在Windows中,命令cd /d %~dp0意味着什么?

#16


0  

you can try:

你可以尝试:

shift + right click

shift +右击

then, click on Open command prompt here

然后,单击此处的Open命令提示。