Let's say there is some .bat file that is required to run inside .cmd batch script. This inner .bat file has a series of user interactions on a local host, and are only able to be ended using ctrl+c.
The question is: Is there some way to make the outer batch script resume after the inner script is terminated? Or is the ctrl+c the end all be all?
I've tried giving the inner script a different way out only to be told I'm not allowed to change that file. I've also done a fair amount of research and haven't found a solution. Forgive me if I've overlooked something! I'd like to avoid having two windows or extraneous termination messages pop up.
The only way I can think of to handle this is to use the following line in outer.cmd to call inner.bat -- with the disadvantage of receiving a new command prompt window for the execution of inner.bat:
start "" /WAIT cmd /C "inner.bat"
(Exchanging start and cmd does not work as the new window might unintentionally remain open.)
Note that for inner.bat, all the console input and output are handled via the new window, hence any redirections for outer.cmd (e. g., outer.cmd > "return.txt") will not include data from inner.bat.
Related
I'm trying to make a batch script for running multiple command line antivirus scans in a specific order.
I use it this way:
echo Running Sophos virus scan...
start "Sophos Scan" /wait /d "%~dp0sophos" SVRTcli.exe -yes >>%~dp0logs\sophoslog.txt
This will popup a new console window running sophos and wait till it's done.
If I close the new console window, the main window of my batch file will prompt for terminating the batch script. I want to avoid it and just automatically continue with my next command which is similar to the previous (different anti virus engine).
Is it possible? I tried with various solutions on the net. Every time it's the same result. You close the new console and it comes up on your batch cmd.
In a batch script redirecting the output to a file like so
set "output=C:\output.txt"
echo Blah blah blah. >> %output%
Is it required that the file is closed after the redirection of writing stuff to it is completed (similar to the approach in other programming languages)?
I have tried searching for related information online but could not find something on it; I assume the fact that most scripts are closed after they finish their tasks (commands) is maybe the reason why.
But if say a script is to run in an endless loop where a different output file is written (e.g. by appending the time to the output file name) or if new output is constantly being redirected to the same output file, could the "not closing of the file" potentially lead to problems, memory or other?
No, you don't have to close any file handles in batch scripts. You don't know the file handle value so you could not even close it if you wanted to.
On Windows, all open kernel handles are closed when a process ends/crashes but since a batch file is interpreted by cmd.exe without starting a new cmd.exe process in most cases, it cannot take advantage of the automatic handle cleanup and will manually close the file handle after each redirected operation.
As far as I am concerned (after digging a bit on the topic) that in order to redirect a user to a command prompt after the batch file completed execution it is a common practice to simply put "cmd /k" at the very end of the batch file.
However, the history of the user input is lost since it is not passed on to a child process, which is created by "cmd /k". I was wondering if there is another way of bringing up command prompt after the batch file finished working?
Just so we are on the same page, I double click the batch file. I know that if I call it from the open terminal I won't be having this problem. However, I am curious how to circumvent that in the case I mentioned. Your help will be greatly appreciated.
Is it possible for a batch program and shell script to know if a previous instance of itself is still running and wait until that program ends before triggering the next step in the command line?
We have a bat file and a corresponding shell that is triggered by an application which writes logs into a file, what happens when multiple users use the same application and triggers the program at the same time is that the logs get jumbled and is not readable by the program that turns the log into a PDF.
Thanks.
So I wrote a tool with some batch commands, nothing specific. In the beginning the user can choose which task to perform thanks to a loop.
In that loop I included the "Q" option, as to quit the batch file. When this happens, it gets written to a logfile to check when the user started the script(s), and when it ended.
The issue is this only happens if the user actually quits/exits with Q. If (s)he quits by just closing the batch file, this won't be logged.
In short: how can I record when the user has quit the batch file without using the build-in function?
a batch file can't receive the "exiting"-event. What you can do is:
Make a launcher.bat file, that starts the original (yourfilename).bat file with:
start /wait (yourfilename).bat
the launcher.bat file will now wait until you close the second (yourfilename).bat file. place your log-information on the next line of launcher.bat
convert launcher.bat to launcher.exe using bat to exe converter (and make it invisible).