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

时间:2021-06-06 23:07:04

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

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

I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me 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文件位于目标目录中,如果这很重要的话。

7 个解决方案

#1


62  

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)文件中,当执行该文件时,您应该会看到指定目录下的命令提示窗口。

#2


6  

Use the /K switch:

使用/ K开关:

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

But IMHO, the most useful switch is /?.

但是IMHO,最有用的开关是/?

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建议的那样谷歌。

#3


3  

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

The /D will change folder and drive 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 DisableUNCCheck registry entry...

如果你看一下Vista的open命令,它会使用cmd。exe /s /k pushd \“%V\”,但我不认为%V是有记录的。使用pushd是一个好主意,如果您的路径是UNC (\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\

#4


2  

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,右键单击别名并导航到它的设置。将“执行”更改为您希望作为标准路径的路径。它总是从这条路径开始。

#5


1  

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

#6


0  

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文件。为我工作。

#7


0  

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

您可以创建一个批处理文件“goto -folder”。“bat”有以下声明:

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

#1


62  

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)文件中,当执行该文件时,您应该会看到指定目录下的命令提示窗口。

#2


6  

Use the /K switch:

使用/ K开关:

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

But IMHO, the most useful switch is /?.

但是IMHO,最有用的开关是/?

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建议的那样谷歌。

#3


3  

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

The /D will change folder and drive 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 DisableUNCCheck registry entry...

如果你看一下Vista的open命令,它会使用cmd。exe /s /k pushd \“%V\”,但我不认为%V是有记录的。使用pushd是一个好主意,如果您的路径是UNC (\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\

#4


2  

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,右键单击别名并导航到它的设置。将“执行”更改为您希望作为标准路径的路径。它总是从这条路径开始。

#5


1  

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

#6


0  

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文件。为我工作。

#7


0  

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

您可以创建一个批处理文件“goto -folder”。“bat”有以下声明:

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