How to make a batch file that lets you use cmd after? - batch-file

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?

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.

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

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

Related

How do I launch a new cmd window which won't auto close after the execution

I'm trying to java in a new cmd window which won't auto close after the execution.
Here is the code in the batch file I tried
start CMD /c "java Hello" && "pause"
I also tried
start CMD /c "java Hello" & "pause"
and
start cmd /k /c "java Hello" & "pause"
Put the whole set of commands in a single set of quotes, for example
start CMD /c "java Hello && pause"
or
start CMD /c "java Hello & pause"
The second, with a single ampersand, will still pause even if the first command fails, whereas the first will close the window before you can read any error messages.
From the Microsoft docs here
/c Carries out the command specified by string and then stops.
/k Carries out the command specified by string and continues.
I think you just need the /k without the /c. That worked in my simple experiment.

Cannot start .exe file propetly because config

So...I have "launcher"..
Its .bat file and i want it to start /ffa/server.exe
but..In ffa/ i have config file.
When i start server via launcher it starts server but it makes new config file
in directory of launcher..How can i fix this?
And "server" its: https://github.com/OgarProject/Ogar
start cmd /k %~dp0\ffa\server.exe
Please help me, its really frustrating..Thanks <3!
You can use pushd to move to the correct directory, start the server, and then pop back:
pushd %~dp0\ffa
start cmd /k server.exe
popd
I'm not familiar with the exact folder structure you're working with, and exactly how you call the script, but you definitely could use push/popd for this.
There are more possible solutions to change working directory of started command prompt window:
In calling script
pushd %~dp0\ffa
start "" cmd /k server.exe
popd
In started cmd itself: note properly escaped & character (see redirection)
start "" cmd /k pushd %~dp0\ffa^&server.exe
Using /D parameter of start command
start "" /D "%~dp0\ffa" cmd /k server.exe

Batch, using Start Cmd to run cmd in new window

So I want to open a new cmd prompt from my batch script and set that as the "active window" to run the following cmds in my script.
I know how to open the new cmd promt:
start cmd \k "cd c:/users"
Now whenever I enter in cmds to perform under that line it executes in the prompt that I used to call my batch script. How can I set it so they all execute in the new window, there will be a lot of cmds executed and i have tried the && and & but it doesn't work.
Any help on this would be appreciated!
Thanks in advance!
This works:
#echo off
start cmd /k "cd c:\users" ^&set/p check=Did it work? ^& call echo %%check%%
note: c:\ not c:/
Or:
#echo off
start cmd /k "cd /d c:\users" ^&^
set/p check=Did it work? ^&^
call echo %%check%%

Keep CMD open after BAT file executes

I have a bat file like this:
ipconfig
That will print out the IP info to the screen, but before the user can read that info CMD closes itself.
I believe that CMD assumes the script has finished, so it closes.
How do I keep CMD open after the script is finished?
Put pause at the end of your .BAT file.
Depending on how you are running the command, you can put /k after cmd to keep the window open.
cmd /k my_script.bat
Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.
Just add #pause at the end.
Example:
#echo off
ipconfig
#pause
Or you can also use:
cmd /k ipconfig
When the .bat file is started not from within the command line (e.g. double-clicking).
echo The echoed text
#pause
echo The echoed text
pause
echo The echoed text
cmd /k
echo The echoed text & pause
Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.
Example :
cmd /k gradlew cleanEclipse
start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example
start cmd /k echo Hello, World!
What I did use in my case
start cmd /k cordova prepare
Update
You could even have a title for this by using
start "My Title" echo Hello, World!
If you are starting the script within the command line, then add exit /b to keep CMD opened
In Windows add '& Pause' to the end of your command in the file.
I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first.
What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:
cmd /k javac my_code.java
So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java
cmd cd C:\xxx\yourbat.bat
the second command make your cmd window not be closed.
The important thing is you still able to input new command
As a sidenote this also works when running a command directly from the search bar in windows.
e.g. directly running ipconfig will directly close the cmd window after the command has exited.
Using cmd \k <command> won't - which was what i was trying to do when i found this answer.
It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.

Start a CMD, then echo off of it, then clear the screen

Here are the steps I need, I am using a batch script
Open a cmd with path set to the desktop
Then set it's echo off
Then clear the screen
Then wait for me to type commands
So I can enter the commands directly without a long line blocking the view
Code I tried so far
#echo off
cd "C:\Documents and Settings\Administrator\Desktop"
cls
cmd
#echo off
cls
But when I run this I get a typical CMD window, where I have to type "echo off" & "cls" again to get a clean window.
The output I get is
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator\Desktop>
The output I want is simple
Imagine a blinking underscore
_
Thank you.
Try this line:
start /D "C:\Documents and Settings\Administrator\Desktop" cmd /k "prompt $"
/D <path> will set your working directory
Prompt $ will set your prompt to "nothing", only the blinking cursor will display
The output I want is simple
_
You can achive this with the prompt command, e.g. prompt $S or prompt $g
Here's an example batch file that would open up a new cmd window with an empty prompt:
#echo Off
start cmd /k "prompt $g"
create a VBS file using notepad
add this lines
Set cmd = CreateObject("WScript.Shell")
cmd.run"cmd"
WScript.Sleep 200
cmd.Sendkeys"echo off{Enter}"
WScript.Sleep 200
cmd.Sendkeys"cls{Enter}"
WScript.Sleep 200
This command can be very problematic (cmd /c "echo off | clip")
USE WITHOUT QUOTES:
cmd /c echo off | clip
goto off
(Save as .bat)
No more problems !!!
I just used
cls
echo off
cls
cmd|echo off
cls
in a batch file. Works for me

Resources