I am running a .bat file on my windows pc.
what I am trying to do is have several commands run when .bat file opens.
I also want enable the cmd prompt window to stay open when the .bat file is run.
In order to keep the .bat open when I used cmd /k at the start of my .bat file.
this seem to enable the window to stay open. Then I want to run my commands, however, the first command is the only one that seems to run. my set up is as follows:
cmd /k
echo a
echo b
echo c
the cmd prints a no problem but does not print the others.
i have tried adding & and && to no avail.
I am creating my .bat from a .txt file and saving it with the .bat extension.
any ideas how i can get the remaining echo b and echo c commands to run?
Why not just change the order?
#Echo a
#Echo b
#Echo c
#%SystemRoot%\System32\cmd.exe /D /K
…which in your less robust syntax, would look like this:
echo a
echo b
echo c
cmd /k
Related
I've recently installed cmder (or simply 'Commander') portable console emulator for windows, and really like the functionalities it comes with. But how can you (if at all) run batches through it like you can with the Windows command line?
The details:
If you write...
cd C:\test\
REM executeSomething.exe
Pause
... to a textfile and save it as a .bat file you can do can do pretty much anything just by double-clicking that file. I've got a work-flow where I launch a web-application through a batch file. Sometimes this applicaton launches automatically in Chrome, and sometimes it does not. In that case, I'll have to copy and paste a URL to Chrome manually. And that's a real pain. With cmder.exe that's much easier, but I'll have to manually navigate to a folder and start the application through Commander it manually without the luxury of a .bat file.
The functionality I'm trying to run automatically with cmder.exe is simply changing a folder and starting an executable file, specifically Jupyter Lab:
cd C:\jupyterlab\
jupyter lab
Pause
I've tried various approaches with:
#echo off
set CMDER_ROOT=C:\Cmder
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
as described here, but with no success. The example above does launch commander, but also raises a syntax error:
Current directory: C:\batches
Command to be executed: "C:\Windows\system32\cmd.exe" /k
"C:\Cmder\vendor\init.bat cd C:\batches && "
I'm obviously on to somehting here since I'm able to start Commander, but I'm not sure how to edit the remaining code to run the necessary steps. I do realize that the /title "Homestead VM" part just edits the title of the Commander window:
but I have no idea about the rest.
If this just isn't possible, I'm going to have to try to reassociate .bat files in Control Panel > Default Programs > Associate a filetype, but I'm hoping to avoid that.
Thank you for any suggestions!
System info:
Windows 7, 64 bit
Commander v1.3.12
Edit 1: My (failed) attempt trying to follow a suggestion from Gerhard Barnard
I've saved a file named please.cmd to C:\Windows\System32 that contains this:
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
I've also saved a file named SObatch.bat in C:\batches that contains this:
if not defined myComs set myComs=0 && please %~0
#echo off
ping localhost
echo %userprofile%
pause
Upon double-clicking SObatch.bat a Windows prompt is opened, and the pings are run:
I just narrowed the window to leave out real-world user-names. But the ping functions are being run and the usual responses are returned.
So I guess it still seems that I've broken something.
Create a file called please.cmd and save it to C:\Windows\System32 as administrator. It should then exist as c:\Windows\System32\please.cmd
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %*"
Now when you create a batch files you need to make some changes in the top of each batch file you want. You would need to add the below line to the very top of the script.
"%systemroot%\system32\please.cmd" "%~f0" && goto :eof
You can also from cmd please command to launch it from the cmder shell. Example:
please ping localhost
Apologies if duplicate, I want to run a jar file from command prompt, So instead of going to the specific path can i create a batch file,
Simply double click on batch file will do my task,
I found something at [1]: Run a Command Prompt command from Desktop Shortcut but it do not work in my case, I want to run java -jar squirrel-sql.jar
I would like to make a batch file that:
1)Opens cmd.exe
2)Within that Command Prompt runs java -jar squirrel-sql.jar which is present on desktop
3)Leaves the window open so that I can run additional commands if I wish to
How can I do this?
Save the following as squirrel.bat:
java -jar squirrel-sql.jar
REM /P causes SET to prompt and wait for Enter.
REM The underscore is used to ensure that "sure" is not already
REM an environment variable.
set /p _sure="Do you want to exit? [press Enter]"
REM /I causes a case-insensitive comparison
if /I NOT "_sure"=="y" (
REM /B exits the batch script but not the CMD window
exit /b
) else (
REM Any other modifications
)
This might be very basic but I cannot find the answer in the internet.
I have a cmd/bat file with 3 basics lines to set the working directory as the one of the current folder.Once I run it, I get the CMD window, and I type specific commands (example : "start notepad").
%~d1
cd "%~p1"
call cmd
What should I write within the cmd. or bat. file so the "start notepad" will be already launched as command?
Thank you very much
There are a couple of ways you can achieve this.
You can open notepad directly with the start command and then run cmd, like this:
#echo off
%~d1
cd "%~p1"
start "" notepad
call cmd
You can also include the notepad start command directly in the cmd call, like this:
#echo off
%~d1
cd "%~p1"
call cmd /k start "" notepad
Note that there is a "" after start because start considers the first set of quotes that it encounters to be the window's title.
Try this:
#echo off
Command.Com
When you'll open this batch file it will open up CMD where you can start typing commands like start notepad etc...I think this was what you was looking for?
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.
I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps