windows 打开命令提示符窗口并更改当前工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4717352/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:59:32  来源:igfitidea点击:

open command prompt window and change current working directory

windowscommand-linebatch-file

提问by goat

I'm terribly new to scripting on windows. Using windows 7 64.

我对在 Windows 上编写脚本非常陌生。使用 Windows 7 64。

I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cdme to a certain directory.

我正在尝试制作一个可以双击的 .bat 文件,让它打开命令提示符并自动cd进入某个目录。

I tried making a .bat file with

我尝试制作一个 .bat 文件

@ECHO OFF
cmd "cd C:\my\destination"

Which opens what looks like a command prompt, but doesn't seem to let me type any commands.

它打开了一个看起来像命令提示符的东西,但似乎不允许我输入任何命令。

I then tried:

然后我尝试:

@ECHO OFF
start cmd "cd C:\my\destination"

But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.

但这只是让我进入一个循环,打开大量提示,直到我的计算机崩溃:) 如果重要的话,.bat 文件位于目标目录中。

回答by Bernard

This works for me:

这对我有用:

@ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"

The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.

带引号的字符串实际上是两条命令(用双与号隔开):第一条命令是切换到指定目录,第二条命令是切换到指定盘符。

Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.

把它放在一个批处理 (.BAT) 文件中,当你执行它时,你应该在指定的目录中看到一个命令提示符窗口。

回答by goat

Use the /Kswitch:

使用/K开关:

@ECHO OFF
start cmd.exe /K "cd C:\my\destination"

But IMHO, the most useful switch is /?.

但恕我直言,最有用的开关是/? .

Starts a new instance of the Windows XP command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
/S      Modifies the treatment of string after /C or /K (see below)
/Q      Turns echo off
...

And only if it does not work, then Google it, as @Neeraj suggested :D

并且只有当它不起作用时,才用谷歌搜索它,正如@Neeraj 建议的那样:D

回答by Anders

@ECHO OFF
%comspec% /K "cd /D d:\somefolder"

The /D will change folder anddrive and works on 2000+ (Not sure about NT4)

/D 将更改文件夹驱动器并在 2000+ 上工作(不确定 NT4)

If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\"but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheckregistry entry...

如果您在此处查看 Vista 的 open 命令,它会使用cmd.exe /s /k pushd \"%V\"但我认为没有记录 %V 。如果您的路径是 UNC ( \\server\share\folder),则使用 pushd 是个好主意要使 UNC 当前目录工作,您可能必须设置DisableUNCCheck注册表项...

回答by Stefan Seibert

Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.

为什么这么复杂?只需为 cmd.exe 创建一个别名,右键单击该别名并导航到其设置。将“execute in”更改为您想要作为标准路径的路径。它将始终从这条路径开始。

回答by Jorciney

This could be done like that:

这可以这样做:

@ECHO OFF
cd /D "C:\my\destination"
cmd.exe

If you need to execute a file or command after you open the cmd you can just replace the last line with:

如果您需要在打开 cmd 后执行文件或命令,您可以将最后一行替换为:

cmd.exe /k myCommand

回答by Michael

You can create a batch file "go-to-folder.bat"with the following statements:

您可以使用以下语句创建批处理文件“go-to-folder.bat”

rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

回答by blurple

just open a text editor and type

只需打开一个文本编辑器并输入

start cmd.exe

cd C:\desired path

Then save it as a .bat file. Works for me.

然后将其另存为 .bat 文件。对我来说有效。