goat
goat

Reputation: 31854

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using 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 cd me to a certain directory.

I tried making a .bat file with

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

Upvotes: 43

Views: 99938

Answers (7)

Jorciney
Jorciney

Reputation: 740

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.exe /k myCommand

Upvotes: 6

Michael
Michael

Reputation: 31

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

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

Upvotes: 0

Stefan Lindblad
Stefan Lindblad

Reputation: 420

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.

Upvotes: 2

blurple
blurple

Reputation: 1

just open a text editor and type

start cmd.exe

cd C:\desired path

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

Upvotes: 0

Anders
Anders

Reputation: 101764

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

The /D will change folder and drive and works on 2000+ (Not sure about 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...

Upvotes: 3

user159088
user159088

Reputation:

Use the /K switch:

@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

Upvotes: 11

Bernard
Bernard

Reputation: 7961

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.

Upvotes: 82

Related Questions