3
votes

what I'm essentially trying to do is make a batch file (or shortcut if possible) to change to a directory, echo a message, and then let you use the cmd window as if you had opened it up normally after.

Example:

  • The batch file/shortcut opens a cmd prompt in C:\test-folder\

  • Echos "This is your message"

  • Awaits user input for commands

I've tried the following command:

start cmd /k cd /d C:\test-folder\

echo test

The folder change will work without the echo line, but if I include the echo line, it will not work at all.

Is there any way to do what I'm trying to accomplish here?

3

3 Answers

2
votes

the order of the switches matters. See start /? and cmd /? for details.

start "My window" /d "d:\" cmd /k "echo Hello&echo use me"

"My window" belongs to start and gives the new window a title.

/d "c:\test-Folder\" also belongs to start and gives the startdirectory

cmd is the command to start

/k belongs to cmd and has to be cmd's (only or) last Switch

"echo Hello&echo use me" is the commandline to execute.

0
votes

Your problem is the echo test is run in the parent window that issues the START command. You want the ECHO to occur in the window that START launches.

One option is to append the ECHO command to the end of the START command, remembering to escape the & so that it is included as part of the CMD /K option.

start cmd /k cd /d C:\test-folder\ ^& echo test

Or you could put quotes around the entire /K option (CMD will remove the quotes before executing the string):

start cmd /k "cd /d C:\test-folder\ & echo test"

Or you could use Stephan's suggestion to let START set your active directory, so that your CMD /K option only needs to ECHO the message.

start /d "C:\test-folder" cmd /k echo test

Or you can create a shortcut and edit the properties such that
"Target" = C:\Windows\System32\cmd.exe /k echo test
and
"Start in" = c:\test-folder

0
votes

If you create a batch file that ends with cmd /k, it will run the commands before cmd /k and then leave you in a command prompt. This is useful if you're performing enough work that makes a one-liner shortcut hard to write.

To get the behaviour you described, you can create a file called "open_test_folder.cmd" with the following:

@echo off
cd C:\test-folder
echo This is your message
cmd /k