batch windows command written in prompt but no execution - windows

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). It opens several CMD PROMPT to a specific Directory and launch a command.
For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. Then I'd just have to click on ENTER to launch that command whenever I want later on.
Here is my code:
setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"
With this code it launches the command "cordova build android".
If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\MyPath>", I'd like to write: "cordova build android" behind it without launching the command.
Any idea?

To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option:
#Echo Off
setlocal
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
:loop
Cd /D %CordovaProjPath%
Echo %CommandToRun%
set QUIT=
set /p QUIT=Press ENTER to run command or 'Q' to quit:
if /i "%QUIT%" == "Q" goto :eof
%CommandToRun%
goto :loop
Unlike the original, this runs the target command in the same command-window as the repeating loop. Depending on what the command in question does, this may be more attractive (less windows popping-up). However, some commands may cause the main window to close; if this is the case, you can revert to running the command in its own window in one of two different ways. In each case, replace the line:
...
%CommandToRun%
...
Run in own window and remain open
...
start "%CommandToRun%" /wait cmd /k %CommandToRun%
...
Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause.
Run in own window then close
...
start "%CommandToRun%" /wait cmd /c %CommandToRun%
...
Using /c will mean the command-prompt will close after the target command has run. This may be appropriate if you do not need to see the output of the command, or if it has its own pause.

Would something like this do you:
#Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"
Below is an alternative which may allow for your alternative double-quoting method:
#Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

Related

Strange behavior when running start.bat file in Windows 2019 Core

I have a start.bat which does nothing but
#echo off
Echo "Hello World"
Then I open a cmd window and type in
start "" /D D:\Test start.bat
it opens a new cmd window with "Hello World", but does not close the window automatically.
Now I created another bat file named start2.bat which has the same content as the start.bat above. Then I type in
start "" /D D:\Test start2.bat
it opens a new cmd window with "Hello World", and does close the window automatically.
Why is it happening ???
I try to use some trace tool to analyse, from the tool, it seems like my system turn start "" /D D:\Test start.bat to cmd.exe /K start.bat
TraceTool
Tried in another computer, now, no matter which file name I used, it turns into cmd/exe /K start.bat
start3.bat
Update:
Turns out adding /C does not work for me, attach process tree below, the system still changes /C to /K
Process tree view with /C
Update:
Provide full process view. My start.bat is exactly the same as above
And now I open a cmd.exe window, and typed in
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start.bat or just cmd /C START "test" /D "C:\test" start.bat
And
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start2.bat or just cmd /C START "test" /D "C:\test" start2.bat
From the process tree view, it can be seen that the first command turns into
C:\windows\system32\cmd.exe /K start.bat
While the second command turns into
C:\windows\system32\cmd.exe /c "c:\test\start2.bat" "
cmd.exe
Full Process Tree View
From the usage information of the start command (excerpt from the description of the command/program argument):
[...]
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
[...]
Hence when starting an internal command or a batch file, cmd.exe together with its /K option is used, so start "" "script.bat" is equivalent to:
start "" cmd.exe /K "script.bat"
If you want the new console window to become closed, use this instead:
start "" cmd.exe /C "script.bat"

Windows Registry Run Key

According to http://msdn.microsoft.com/en-au/library/aa376977(v=vs.85).aspx:
"Run and RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line."
Should I then be able to add a key with:
Name: MyName
Data: START /MIN "Title" "cmd.exe" /c "#echo off && "C:\TestApplication.exe" -Arg1 "Arg2"
with the goal being to start my console application "C:\TestApplication.exe" minimized with arguments "-Arg1 "Arg2"" when Windows starts?
I ask because I cannot seem to get it working.
The documentation is misleading, though not strictly incorrect. The command line is passed directly to CreateProcess() rather than being passed to cmd.exe so commands internal to cmd.exe, such as start, are not valid. This means you need to add cmd /c at the start of the command line.
You're also missing a quote mark at the end, and you don't need to put quotes around cmd.exe. This works:
cmd /c START /MIN "Title" cmd /c "#echo off && "C:\TestApp.exe" -Arg1 "Arg2""
However, since the target application is an executable, not a batch file, you don't need the #echo command either:
cmd /c START /MIN "Title" cmd /c ""C:\TestApp.exe" -Arg1 "Arg2""
(Note that the command line passed to /c is never echoed.)

Prevent CMD Start command from terminating after completion

I have the following CMD script,
#echo off
set APP_HOME=C:\Temp\Hammer
rem Set APP_HOME to the place where you installed Hammer.
if "%~1"=="" (
echo You must provide a Python script to run.
pause
) else (
#start /min java.exe -jar %APP_HOME%\lib\bolt.jar
#start java.exe -cp "%APP_HOME%\lib\jython.jar;%APP_HOME%\lib\Hammer.jar;%APP_HOME%\lib\gmsecapi.jar;%APP_HOME%\examples\Python;%APP_HOME%\jython2.5.3\Lib;%CLASSPATH%" bridenstine.hammer.main.Main %*
)
After the program, which is executed by the second java.exe command, finishes the terminal closes which prevents the user from reading the outputted text of the program.
I've looked over these commands but haven't found anything that would keep the window open even after the program ends.
You must start the command prompt. You are only starting a console - that's just a window. Preface your commands with cmd /k. See Start /?.

How do I minimize the command prompt from my bat file

I have this bat file and I want to minimize the cmd window when I run it:
#echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
I want the command window minimized immediately. Any ideas on how to do this?
There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit
How it works
When the script is being executed IS_MINIMIZED is not defined (if not DEFINED IS_MINIMIZED) so:
IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.
Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:
"" - empty title for the window.
/min - switch to run minimized.
"%~dpnx0" - full path to your script.
%* - passing through all your script's parameters.
Then initial script finishes its work: && exit.
For the started copy of the script variable IS_MINIMIZED is set by the original script so it just skips the execution of the first line and goes directly to the script logic.
Remarks
You have to reserve some variable name to use it as a flag.
The script should be ended with exit, otherwise the cmd window wouldn't be closed after the script execution.
If your script doesn't accept arguments you could use argument as a flag instead of variable:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
or shorter
if "%1" == "" start "" /min "%~f0" MY_FLAG && exit
Use the start command, with the /min switch to run minimized. For example:
start /min C:\Ruby192\bin\setrbvars.bat
Since you've specified a batch file as the argument, the command processor is run, passing the /k switch. This means that the window will remain on screen after the command has finished. You can alter that behavior by explicitly running cmd.exe yourself and passing the appropriate switches if necessary.
Alternatively, you can create a shortcut to the batch file (are PIF files still around), and then alter its properties so that it starts minimized.
The only way I know is by creating a Windows shortcut to the batch file and then changing its properties to run minimized by default.
Using PowerShell you can minimize from the same file without opening a new instance.
powershell -window minimized -command ""
Also -window hidden and -window normal is available to hide completely or restore.
source: https://stackoverflow.com/a/45061676/1178975
If you want to start the batch for Win-Run / autostart, I found I nice solution here https://www.computerhope.com/issues/ch000932.htm & https://superuser.com/questions/364799/how-to-run-the-command-prompt-minimized
cmd.exe /c start /min myfile.bat ^& exit
the cmd.exe is needed as start is no windows command that can be executed outside a batch
/c = exit after the start is finished
the ^& exit part ensures that the window closes even if the batch does not end with exit
However, the initial cmd is still not minimized.
One way to 'minimise' the cmd window is to reduce the size of the console using something like...
echo DO NOT CLOSE THIS WINDOW
MODE CON COLS=30 LINES=2
You can reduce the COLS to about 18 and the LINES to 1 if you wish.
The advantage is that it works under WinPE, 32-bit or 64-bit, and does not require any 3rd party utility.
If you type this text in your bat file:
start /min blah.exe
It will immediately minimize as soon as it opens the program. You will only see a brief flash of it and it will disappear.
You could try running a script as follows
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c setrbvars.bat", WindowStyle_Hidden)
save the file as filename.js
Yet another free 3rd party tool that is capable of minimizing the console window at any time (not only when starting the script) is Tcl with the TWAPI extension:
echo package require twapi;twapi::minimize_window [twapi::get_console_window] | tclkitsh -
here tclkitsh.exe is in the PATH and is one of the tclkit-cli-*-twapi-*.exe files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lighter min.exe mentioned in Bernard Chen's answer because I use TWAPI for countless other purposes already.
You can minimize the command prompt on during the run but you'll need two additional scripts: windowMode and getCmdPid.bat:
#echo off
call getCmdPid
call windowMode -pid %errorlevel% -mode minimized
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
One option is to find one of the various utilities that can change the window state of the currently running console window and make a call to it from within the batch script.
You can run it as the first thing in your batch script. Here are two such tools:
min.exe
http://www.paulsadowski.com/wsh/cmdprogs.htm
cmdow
http://www.commandline.co.uk/cmdow/index.html
Another option that works fine for me is to use ConEmu, see http://conemu.github.io/en/ConEmuArgs.html
"C:\Program Files\ConEmu\ConEmu64.exe" -min -run myfile.bat
try these
CONSOLESTATE /Min
or:
SETCONSOLE /minimize
or:
TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN
http://conemu.github.io/en/ConEmuArgs.html download flagged by Virus Total.
May have Malware.

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.
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.
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
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
#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...
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.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
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

Resources