windows BAT文件在当前目录打开CMD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4451668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BAT file to open CMD in current directory
提问by FrustratedWithFormsDesigner
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 inbut 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 文件所在的文件夹中打开一个新的命令行窗口,但它不起作用。它打开一个新窗口,但工作目录不是 .bat 文件所在的目录。这是我在谷歌搜索后得到的(我的 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 上获得的效果。
采纳答案by Alvin SIU
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
; Enter)。创建指向此 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。
回答by Chris
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
这会将您的当前目录设置为您拥有批处理文件的目录
回答by Joey
You can just enter cmd
into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.
您只需进入cmd
资源管理器中的地址栏,它就会在该路径中启动。对于 PowerShell 也是如此。
回答by s-f
There's more simple way
还有更简单的方法
start /d "folder path"
回答by DKnight
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 for 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 并右键单击(这是内置的),您将获得该选项。
回答by Chad
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.
除非您完全符合所有路径。
回答by Michael Kalygin
The simplest command to do this:start
执行此操作的最简单命令: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 文件中。
回答by Dediqated
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
.
资源管理器中最简单的方法是按住 Shift + 鼠标右键单击文件夹或文件夹中的空白区域,然后单击Open command prompt here
。
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。
回答by andyhasit
Another solution is to use a shortcut file to cmd.exeinstead of a batch file.
另一种解决方案是使用cmd.exe的快捷方式文件而不是批处理文件。
Edit the shortcut's start inproperty 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 文件...
回答by mit
You could add a context menu entry through the registry:
您可以通过注册表添加上下文菜单项:
Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
and create a key called "Command Prompt" without the quotes.Set the default string to whatever text you want to appear in the right-click menu.
Create a new key within your newly created command prompt named "command," and set the default string to
cmd.exe /k pushd %1
在您的注册表中导航到
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
并创建一个名为“命令提示符”的键,不带引号。将默认字符串设置为您希望在右键单击菜单中显示的任何文本。
在新创建的命令提示符中创建一个名为“command”的新键,并将默认字符串设置为
cmd.exe /k pushd %1
You may need to add %SystemRoot%\system32\
before the cmd.exe if the executable can't be found.
%SystemRoot%\system32\
如果找不到可执行文件,您可能需要在 cmd.exe 之前添加。
- 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
另见http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm