batch file start a program when closed - batch-file

I need help writing a batch file that starts a program when I close the Command Prompt (batch file).
I know how to start a program when the batch file is running:
#echo off
Start [adress of application]

I believe it's not possible, because when you terminate the batch (by pressing the red 'X' on top), it will end imediately without doing anything.
But you can Read the user input, and if the user writes "Exit", it will run a program and close. I think you don't want that, but If you, then look at this: In Windows cmd, how do I prompt for user input and use the result in another command?

Related

Why can't I use the command prompt after running a batch file on it?

So I have a batch file that simply runs an exe file. I want to be able to open the command prompt, run the batch file, then... I want to type another command in the command prompt.
here is the code that is in the batch file called "sublime.bat":
"C:\Sublime Text 3\sublime_text.exe"
I open cmd in the directory with my bat file and I type:
"sublime.bat"
It works by opening sublime text but the cmd cursor starts flashing and I can no longer type anything until I close sublime text.
I want to be able to open sublime text and type commands out while still having sublime text open. Please help, thanks.
Command prompt doesn't execute a further line, while a command is on execution. It executes commands serially, not parallelly. So if you want a command to be executed cmd should return from executing previous one.
Here, in "sublime.bat" you have called a batch file which contains a command of executing another program. So, cmd waits for the result of executing the bat file and thus stuck there.
You can use start "/k" "C:\Sublime Text 3\sublime_text.exe" in your "sublime.bat". This holds only the start command and cmd gets free after starting the file.

How to disable input in batch script?

I have a batch script (mine) that launches another batch script (theirs). Their batch script queries user input as part of the process, and I do not have access to modify it.
I need to suppress their batch script from querying input, meaning that when their batch script outputs: Press enter for step 2..., I want the user to be unable to interact with the script using their keyboard, hence the script should look like it's frozen.
How do I call their script from my script in such a way that the user is unable to interact with the input requests of their script?
Actually summing up the comments to make this not an unanswered question. You can try using the | (pipe) operator:
echo Haha! You will not be able to press any key!! | their.bat
Which will redirect STDOUT (Standard Out) of command echo Haha! You will not be able to press any key!! (Haha! You will not be able to press any key!!) to STDIN (Standard Input) of command theirs.bat.
Or, even read from nul:
their.bat < nul
I recommend reading https://ss64.com/nt/syntax-redirection.html and What does "&&" in this batch file? (second answer is better here.).

how to execute cmd commands and compile it in a .bat file

Hello I'm getting sick from repeating commands in the cmd window so I want a .bat file that makes cmd opens then execute the command, and would be great if it's closed after executing the commands like example:
ipconfig/release
ipconfig/renew
Then closes the cmd window
Thanks.
Easy:
Put both commands in a text file, name it "new.bat".
Add a #echo off and a exit, and you are done:
#echo off
ipconfig/release
ipconfig/renew
exit
Every time, when you enter new, it will execute these commands.
And you can even do it with a double-click in WindowsExplorer.
You could save the .bat file to the desktop, so you can reach it easy with your mouse.

c console programs

I have a *.exe console file.
I enter my inputs, and everything is great.
but when I enter the last input, the command window closes (because the program has ended)
before I can read the last output.
is there a way to run that *.exe file and force it to stay open after the program ends?
note: this is not my program. I can't edit the source code, so I'm not looking for answers like "add while(1) or scanf at the end".
Thanks ahead.
Just open a command prompt and run it, the way it's meant to be used.
Start -> Run -> cmd.exe
or Win+R -> cmd
Run the program from command prompt (Start-Run-cmd.exe)
Make a batch file (*.bat), with the command you want to execute, followed by pause:
myconsoleapplication.exe
pause
Save it, and run. The command window wil wait for enter to be pressed before closing.
Run it from the console, or in a batch file.
Start >> Run >> cmd.exe
Then the console window is already open.
This should work:
system("pause");
At the top of your program, include stdlib.h:
#include <stdlib.h>
You can open a command prompt window and navigate (the cd command) to the directory containing the .exe file. Then, run the program by typing its name. The window will not close after the program finishes running.
You could execute this program from one of your own, redirecting stdio and adding your own pause after it terminates. I've done this with .NET but assume that it can be done through other means. Of course a batch file, as mentioned already, is wicked simpler.

getting returned results after running .BAT file

when i run a .BAT file, it displays a message but immediately closes the window
how i can i force it to keep the window open so that i can see the message reeturned?
One way is to conclude the batch file with the
PAUSE
command.
You can also wrap it another batch file that calls the original and then pauses:
FOO.BAT
PAUSE
This also works for read-only batch files and compiled executables.
An alternative is to execute this bat file from an already opened command prompt (shortcut: Windows Key + R, type cmd, press ENTER)

Resources