cmd.exe doesn't terminate when using a .bat file - shell

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).

The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.

You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.

I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command 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 start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

Related

How to Make a .bat file with arguments

For my work I have to run a string in "run".
I want to make this in a bat file but the arguments are not used but without them the program will not run.
This is the string:
"C:\Program files (x86)\MicroTouch\MT7\TwUI.exe" TwUICP.dll CPMain
Can someone help me with this problem?
You can simply put the same line of code into your bat file:
"C:\Program files (x86)\MicroTouch\MT7\TwUI.exe" TwUICP.dll CPMain
There are some options you can use. For example if you want the program to run hidden in background you can add start /b before the line. start /w will make the cmd window stay open and wait for your program to finish. You can even definde which CPU cores the program should use. For more information open the console and type start /? or check out http://ss64.com/nt/start.html.
Have the same problem, same software i think...
"C:\Program Files (x86)\MicroTouch\MT 7\TwUI.exe" twuicp.dll CPMain
Running that line through task manager -> new task will open a tool to configure touch-screen options, calibrate, etc...
Well same line doesnt work in a bat or cmd file. If i try to launch this tool through cmd.exe it doesnt work either.
The answer is always the same:
USAGE: twui <DLL name> <UI symbol name>
This works, but same line in cmd.exe dont

How to avoid command window popping on cmd.exe

I have a command as:
cmd.exe /c ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
But when executed it opens a command window. Is there a way to avoid the command window from popping up?
PS: I cannot remove cmd.exe from there. As you can see, I am trying to tie two commands one after the other in the same string.
Thanks.
Edit: Sorry. Its not a bat file. I want to execute 2 commands in the "UninstallString" of msiexec. I was trying so many things that my question got a bit carried away.
The command is:
msiexec <product> <package> && reg delete /xxx
The simplest option is to start the thing minimized. Shortcut Target:
cmd /c START /MIN \path\to\test.bat
or
cmd /c START /MIN cmd /k ( ping 1.1.1.1 -w 10000 -n 1 && #ECHO All OK)
It's not hidden or anything, but it doesn't show up on the desktop or—worse—steal the focus.
If you want the window to go away on its own, "cmd /c ..." will make that happen. "cmd /k ..." will leave the window open.
Plan B is referred to by #CodyGray in the SU link he posted. There are programs that don't open windows, like wperl, pythonw, or wscript (natively available on Windows). If you can pass your command through to one of those things, then you could effectively double-click an icon and have it run "silently."
If Perl's available, I'd certainly go with that because you can craft some pretty powerful one-liners that won't require creating other files.
wperl -MWin32 -MNet::Ping -e "$p=Net::Ping->new('icmp',10000); if ($p->ping('192.168.1.1')) { Win32::MsgBox('Ping Successful', 1 + MB_OK, 'All Good'); }"
In your example, you're chaining commands together, the latter is a notification. If you don't want to have a window open for the first command, it would be awkward to do it for the second when you're notifying the user of something. Having the process call "cmd /c start cmd /c #echo Everything's OK" would probably do it, but using CMD windows for user notification is probably not something the HCI guys would smile at.
No, all batch files open in command-line windows; this has nothing to do with the presence of cmd.exe in your particular file. A batch file is simply a number of command-line commands, one per line.
I don't understand why you write test.bat the way you do. I'd rather expect
ping 1.1.1.1 -n 1 -w 10000
echo second command goes here
If, for some bizzare reason, you really need to use only a single line, you can simply do
ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
As Andreas Rejbrand already explained, the command prompt window is not from the explicit cmd.exe invocation within your script but from executing the .bat script itself. (And despite your claim, you haven't provided any evidence why explicitly invoking cmd.exe is necessary. The whole point of a .bat script is to batch commands together.)
That said, the silentbatch program that Paul Miner and I wrote can execute batch scripts and suppress the command prompt window. To use it, you would have to create a Windows shortcut that invokes silentbatch.exe test.bat and double-click on that rather than double-clicking on test.bat directly, however.
You could also create a shortcut to you batch script and then in the properties select to start the app minimized. It is explained here: CNET: How to automatically start a program minimized in Windows
Step 1: Right-click on the shortcut of the program you want to start minimized and select Properties.
Step 2: Click on the drop-down menu under Run.
Step 3: Select "Minimized," then click the OK button.
From win cmd:
start /b cmd.exe /c "ping 1.1.1.1 -n 1 -w 10000 & echo second command goes here"
runs both commands without opening another cmd window, leaving parent window unblocked for further commands without waiting.

Gambit Scheme: gsi and gsc windows closes after end!

I'm experimenting with Gambit scheme and I have problem! My OS is Windows 7.
When I try to interpret script I do:
gsi.exe myscript.scm
This works, but GSI's console window is shown and closed just after script finished. I can't see results my program prints! I may do (read-line) at the end, but... when I try to compile with GSC.exe the behaviour is the same: it opens console window, does something, prints about errors and closes it immediately - I can't read something! In this case I can't even do (read-line) hack, you see. How can I view what Gambit writes?
This doesn't works, though:
gsc.exe 1.scm > 1.txt
You should run Gambit in an existing terminal window. Open up your terminal first, and then run Gambit. When Gambit terminates, your terminal will be still up.
Create a batch file with the commands. Set the properties on the batch file such that the window doesn't close after executing(right click, properties on the batch file's icon).
You can always, add "pause" at the end of the bath file, to keep the window open.
Alternatively, just open a DOSBOX box, and run the script from there. The box will remain open when the scrip completes.
UPDATE
terminal
To open a terminal(Command Prompt, DOS Box,etc.) use the [Start] button. Enter cmd in the "Run" field.
This will open a terminal with a command line interpreter. You can run gsc or gsi from there.
batch files
Here is the sample program hello.scm:
(display "HELLO WORLD")
(newline)
Method 1--using pause. This example is only for calling binary executable(.EXE) files such as gsc, or gsi.exe:
#echo off
gsi hello.scm
pause
Method 2--using cmd /k. The pause method (above) is preferred as this starts another cmd shell:
#echo off
cmd /k gsi hello.scm
properties
Sorry, setting the "Close on exit" property of a command apparently only exists for true DOS commands via .pif files.
To the same end right-click hello.scm, then associate it with cmd /k gsi hello.scm.
Any of the above batch files may be modified to take a filename argument (as %1, or %* for all args) and run gsc %1 instead of gsc hello.scm. After making the batch file generic in this way, associate the .SCM extension with it.
Associate .SCM with run-gsi.bat:
#echo off
gsi %*
pause

CMD Script: How to close the CMD

I have created a small command that will let me launch Internet Explorer. However, I wish to close the small command prompt that shows up when I launch IE. How can I do this? This is my current code:
"%ProgramFiles%\Internet
Explorer\iexplore.exe"
http://localhost/test.html
PAUSE
I am guessing if I take out the Pause. It will close the CMD box upon closing IE??
Also is there another command that I can use to simply create a command that will let me add something to the Menu with a small icon, which in turn runs the above. Is this complicated? Any tutorials I can use?
Thanks all
Use the start command:
start "title" "%ProgramFiles%\Internet Explorer\iexplore.exe" http://www.example.com
you need this on the end
&& exit
For example
"%ProgramFiles%\Internet Explorer\iexplore.exe" http://google.co.uk && exit
#echo off
start "" "%ProgramFiles%\Internet Explorer\iexplore.exe" "http://www.example.com"
exit /b
But you really should not force IE, but use the default browser:
#echo off
start http://www.example.com
exit /b
exit /b does not work on win9x IIRC, so if you need to support every version of windows and close the terminal window if the user double clicks your batch file, go with:
#echo off
start http://www.example.com
cls
You can also launch your program with the /c switch, which terminates the cmd once its finished executing
for example
cmd /c "%ProgramFiles%\InternetExplorer\iexplore.exe" http://localhost/test.html
You have to add 'start' in front of every program you launch, elsewhere your script is going to wait until it's finished.
A little late here, but running it in minimized mode or invisible mode might be another option. Source: https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Running .BAT or .CMD files in minimized mode
To run a batch file in a minimized window state, follow these steps:
Create a shortcut to the .BAT or .CMD file. To do so, right click on the file, click Send To, Desktop (create shortcut)
Right click on the shortcut and choose Properties
In the Run: drop down, choose Minimized
Click OK
Double-click the shortcut to run the batch file in a minimized window state.
"Mind the gap!"
Command Prompt always takes the empty space as separator, unless it's enclosed in double quotes.
So, if any Path, or Program/File Name, or anything includes empty space/es, must closed in quotes.
eg. "C:/Program files/..." path/directory or "Any Program/Command/File.exe/cmd/txt..." Program/Command/File Name includes space/es.
Syntax:
> start /?
Starts a separate window to run a specified program or command.
START ["title"] [/D path] (start swiches here...) [command/program] (com/prog-parameters here)
start "" /d "Drive:/the/Program/Path/..." "Command/Program Name.extension" "File-Name.extension"
So, it's usual fault:
If you don't set the 1st set of quotes "" for title (even if there's nothing to enclose), then the START command takes whats inside the 1st quotes set (eg. path! or Program Name!) and sets it as title... and of course, it messing up.

create a program that can run a cmd prompt

I have a program that changes my desktop wallpaper by dragging the picture file onto it. I also have a wireless network program that can auto open programs everytime it connects to a certain network.
I want to change my desktop everytime it connects to a certain network, but running the wallpaper program doesn't do anything unless I drag the picture onto it. However, I can also run a cmd prompt "c:/program.exe picture.jpg"
I tried creating a batch file START C:/PROGRAM.EXE PICTURE.JPG, but it doesn't work.
So basically I am trying to create a program that can run the cmd prompt "c:/program.exe picture.jpg" - can you help, please?
Remove the "start" from the batch file, and make sure any paths with spaces in them are enclosed in quotes, otherwise they'll be broken into arguments.
For example:
"C:\Program Files\MyProgram.exe" "C:\Documents and Settings\Me\MyPicture.jpg"
A batch job should work. Try skipping that START from your example.
Start - Run - Type :
cmd /c "start /max ""C:\Program Files\MyProgram.exe"" ""C:\Documents and Settings\Me\MyPicture.jpg""" .
The cmd /c - starts a new cmd instance and quits

Resources