Looping a batch file that calls another batch file - windows

My embedded C compiler generates a batch file to run various build outputs in a processor simulator from the command line. I'd like to create a batch file to call that auto generated batch file that is somewhere convenient, and I'd like to be able to rerun it just by clicking a key as often as I want all day long.
Here's what I've tried
cd "C:\foo\project name\settings\"
:repeat
cls
"Project Name.Unit Test Ouput.cspy.bat" & pause
goto repeat
I see the output I expect from the batch file, followed by a:
Press any key to continue . . .
When I press enter the script ends and never executes the goto.
If I remove the pause statement the script just ends immediately.
If I type & goto repeat the script still ends immediately.
CDing to the batch file from the command line, running it, and then click the up arrow and enter effectively does what I want... I am trying to automate it a tiny bit more.

To run a batch file from another batch file, you have to use the call statement. If you don't, then your outer batch file will end when the inner one does.
call "Project Name.Unit Test Ouput.cspy.bat" & pause

Related

Trying to make SYSLOG DAEMON in Windows

Objective:
This daemon maintain logs at what time which text file is opened.
Step I:
I made one empty batch file and this file would be going to log the events into one temporary log file.
Step II:
Associating text files with this batch file program
i) I right clicked on one text file.
ii) I clicked on "open with" option
iii) Then I chose my batch file program.
Now as I open any text file my batch file program starts running.
Step III:
Batch file development:
i) I opened the batch file which I created as empty in Step I
ii) I wrote following batch file commands in order to above objective:
#echo off
echo %1 >>logs.txt
time >> logs.txt
notepad %1
exit
Step IV:
i) As I open any txt file, then this batch file runs, but the problem is as it comes to "time" command I have to manually press enter, so is there any command in batch file programming which can produce keystrokes.
ii) Second thing which I need is as I open my text file, cmd window pops up, I want it to remain hidden from user, so that user doesn't know about logging of events.
iii) Third thing which I need is to start the notepad process in background, so that my batch file script can proceed further.
Use %time% (see set /?) on same line as %1 to have time / filename on same line. Else type time /? for help esp time /t.
Console programs have a console. Use a different technology. VBScript can start programs hidden but you would do everything in vbscript. 0 is hidden.
Set WshShell = WScript.CreateObject("WScript.Shell")
msgbox FormatNumber(WshShell.Run("cmd /k dir c:\windows\*.*", 0, false))
As above but with 6 or 8.

Batchfile: Using "start" Command to Start a batch file on a specific label

Inside this batch files, exist over 25 lables, each for different purposes..
Specifically the label called ":beep" which make a beep noise in the computer.
I wanted to scheduling the start of this batch file, but only the label "beep",
or maybe another batch, but only starting with the label "beep".
I have already used the "call command", which is not what I need.
Is there anybody who knows how to use "Start" command to run a batch file on a specific label?
Example:
start C:\interface.cmd [goto beep]
OK, so if there are no other parameters, just pass beep as a parameter e.g. batchfile beep and have a line goto %1
If there are other parameters you need to pass, time to get more creative. Have the start of your bat file, something like:
#echo off
setlocal
set p1=%1
if x%p1:~0,1% equ x: (
shift
goto %p1%
)
Then you can run: batchfile :beep "as many" other "params as you like"
Maybe i'm just being too lazy...
i will just copy the specific label and its command,
create new batch file..
paste it in there, save it,
and just run the new batch file from Shortcut, Hot Key, task schedule.. which will give me better results.. and just run the commands i needed
more work, but less stress
LOLL.....

Logging the exit of a batch file

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).

How to use a batch file so that results remain visible on completion

I run batch files and they exit immediately. I dont want that to happen so that i can see my output. Can someone tell me how to make this happen ?
I use windows 7.
Put this on the very last line of the Batch:
cmd /k
Adding pause is a good answer. Here are some other ways as well..
Rather than double-clicking on them to execute you can run from a command line:
Press the windows key + r (this opens the "run" window)
Type: cmd into the text input and press enter (or click ok)
Change to the directory that contains the batch file, e.g: cd c:\scripts\foo
Execute the batch file by typing it's name and pressing enter, e.g: somename.bat
If there is a lot of output and it scrolls off the screen you can direct the output to a text file instead like so:
somename.bat > output_filename.txt
Then you can open the 'output_filename.txt' file in any text editor to view/search all of the output. This is better than pause when there more output than what is available in the scrollback.
Add the pause command at the end of your batch file. This waits for you to key something in.
(The nice thing is that if you're running the batch file from a non-interactive process, such as a automated build system or scheduled task, the pause is simply skipped.)
The help message for pause is:
C:\>help pause
Suspends processing of a batch program and displays the message
Press any key to continue . . .
If there is lots of output and you can't scroll far enough back, adjust the screen buffer height of the command window. This can be done via right-click on the c:\ icon go to properties -> layout:

Pause command not working in .bat script

The batch file has only two lines:
c:\program.exe ...
pause
but pause does't run after "program" completes... I don't see that "press any key message" :(
If I move pause on the first line, then it magically works.
It fails because you are not running an executable directly - you are running it via the phpunit.bat batch file (based on information in comment added to question).
You must CALL a batch file from within another batch file if you want to return to the caller
call phpunit
pause
Try saving before opening the bat file, it worked for me

Resources