Print lines after the scheduler executes the batch script 10 times - batch-file

I have a windows batch script that is executed by a scheduler after every 1 hour.
I want that certain lines in the batch script to only be executed when the scheduler has executed the batch script 10 times.
myscript.bat sample
#echo off
echo hello world
want the below line to print after every 10 times the batch script(myscript.bat) has been executed.
echo This text will appear after repeating "hello world" for 10 times.
Any help will be highly appreciated!!

You would need to create a file, seeing as the script executes and exits all the time, well each hour in fact.
#echo off
echo Hello World
echo Hello World >> lockfile.txt
setlocal EnableDelayedExpansion
set "string=findstr /R /N "^^" lockfile.txt | find /C "Hello""
for /f %%a in ('!string!') do set number=%%a
echo %number%
IF "%number%"=="10" (ECHO Success! && del /Q lockfile.txt)
So to explain...
We echo Hello world, purely for you to see it in the screen.
We echo Hello world to a lockfile each time the script executes.
We do a count of the rows in the file, once we hit 10 rows (in this case 10 executions, then we echo Success and we delete the lockfile. The reason for the lockfile deletion is purely to make sure that we do not exceed a count of 10
So all of the commands you want to run after hello world was echo'd 10 times will for part of your IF statement. Hope that helps.

You can do it by using looping statements
In your batch file
For eg :-if you want to print hello world 10 time
FOR /F %i IN (1,1,10) DO echo hello_world

Related

command in for loop behaves differently (Batch file)

I'm running a command in a batch file[Just for info: this command runs a python script which generates different outputs]
The code is as follows:
:meshfunc
echo "starting command"
echo "!inst_dir!runME.bat -parameter1 !parameter2! !parameter1v! -script pythonscript.py"
echo start_time=!time!
for /F "delims=" %%i IN ('"!inst_dir!runME.bat -parameter1 !parameter2! !parameter1v! -script pythonscript.py"') DO (
set cmdline=%%i
echo currentENDline=!cmdline!
)
EXIT /B 0
The output of the script is:
PS: I have stripped the output lines for obvious reasons, but they shouldn't matter here
So my questions is :
If i run the same command without a for loop , it will complete in maybe 30 sec without the warning at the end.
However, if I run it inside a for loop, it takes much longer about 3 mins and gives the warning at the end
Why am I'm getting the Warning at the end as shown. Why is this happening?
Because for /F execute the command enclosed in parentheses and store all its output in a temporary disk file until the command ends; after that, it start to repeatedly execute the group of commands. In this way, if the output is very large, the temporary file needs to grow several times and such a process takes some time... It should be faster to redirect the output to a disk file and then process such a file with for /F:
:meshfunc
echo "starting command"
echo "!inst_dir!runME.bat -parameter1 !parameter2! !parameter1v! -script pythonscript.py"
echo start_time=!time!
call "!inst_dir!runME.bat" -parameter1 !parameter2! !parameter1v! -script pythonscript.py > output.txt
for /F "delims=" %%i IN (output.txt) DO (
set cmdline=%%i
echo currentENDline=!cmdline!
)
EXIT /B 0

batch file label running for 30 seconds

I program to both learn how and to make fun files for jokes and tricks. I'm trying to make a batch file that will run a label inside the batch file for a set amount of time like 30 seconds without having to use a for-do statement. what I have so far is shown below and is reduced to a small test, but uses a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
pause
:matrix
echo %random%%random%
use timeout command, it can set amount of time like 30 seconds without having to use a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
timeout 30
:matrix
echo %random%%random%
if you don't want to show count done message, you can use timeout 30 >nul
this starts a second cmd process (minimized), that simply does a timeout 30. It's existence is the signal to repeat the loop.
#echo off
start /min "MyTimer" timeout 30
:loop
echo %random%%random%
tasklist /fi "windowtitle eq MyTimer" | find "Console" >nul && goto :loop
Sadly, tasklist doesn't give useful errorlevels, so we have to use find.
Note: for debugging purposes you may want to remove the start switch /min.

For statement echoing DO ( commands ) in console when batch script runs

Basically when I am running this script, after runprog.exe returns (echos in cmd prompt) everytihng in the do ( ) section.
#echo off
set NODES=(server1.com server2.com)
for %%i in %NODES% do (
echo Log stuff... >> logfile.txt
runprog.exe /switch %%i
if %ERRORLEVEL%==0 (echo success) else (echo fail)
sleep 5
)
Edit: #echo off is at the top of the script.
The problem is in this sleep 5 command: it is a custom batch file that you have.
The funny thing is that if I run your batch file on my computer the exact same thing is happening, and I most probably have a different 'sleep' batch file than you. Mine contains the following:
#echo off
ping -n %1 127.0.0.1 > NUL 2>&1
Replacing sleep 5 with call sleep 5 fixes the problem here.
I have no idea why. Ask Microsoft.
As Mike Nakis said, it's the batch file sleep.
It's not important if sleep.bat contains #echo off or not.
The problem is that starting a batch file from another batch file transfers the control to the new batch file but doesn't return to the caller.
But in your case you have a FOR-loop which is completly cached by the cmd.exe.
That's the cause why the first batch doesn't stops immediately.
But when the second loop runs, the cmd.exe has leaved the batch-file-mode and is now in the cmd-line-mode.
You could control this by adding an echo line.
#echo off
set "world=" -- unset the world variable
for /L %%n in (1 1 3) do #(
call echo Hello %%n %%world%%
sleep.bat 1
)
The output will be
1 Hello
2 Hello %world%
3 Hello %world%
That's because the cmd-line-mode doesn't remove percent expansions when the variable is unset.
The CALL sleep.bat solves the problem, as then control simply returns to the caller, as expected.

Batch Time problems when used in echo

I have a problem with my batch program, I want it to show what time / t does in a echo line but when I do this
#Echo off
Set echotime=time /t
echo (%date%)(%echotime%)
Only need XX:XX cause XX:XX:XX,XX Makes it too long for the log file i wanna have for my program
echo (%date%)(%time:~0,5%)
the :~0,5construct takes five chars beginning with char 0 (the first one)

Run DOS Command for a Time Limit

I want to perform the following operations.
Read 1 word at a time from an input file consisting of many words.
Pass this word as an argument to another command line based application.
Run that application for a fixed amount of time, say 10 seconds.
Abort the execution of the application if it is still running after 10 seconds and go back, pick the next word from the input file and repeat steps 1 to 3.
Here is what I have written though it does not achieve exactly what I want it to:
#echo off
for /f %%i in ('type input.txt') do call:Routine %%i
:Routine
set app="myApp.exe"
set limit=60
%app% %1
goto Delay
:Delay
ping localhost -n %limit% > nul
The above script will introduce the delay after the execution of myApp has completed. However, I want it to run myApp.exe for not more than 10 seconds, if it does, then abort the application using taskkill and move on to the next word from the input file.
I searched for a solution online and came across this:
http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.general/2006-04/msg03609.html
Though it does not answer my query exactly, I would like to make my code do something similar.
Thanks.
The logic in the linked code looks flawed: It either launches 3 download commands, or it delays ~59 seconds and attempts to kill all download commands, but it never does both. The TASKKILL command arguments are not correct - the imagename belongs after the /IM parameter.
In your code, you are not going to kill your task without the TASKKILL command!
You must GOTO :EOF or EXIT /B after your loop finishes, otherwise the code will fall through and execute the subroutine without using CALL. But there really is no need to use a subroutine at all.
You only need to initialize your variables once.
No need to execute a command in your IN() clause. FOR /F has a variation that can read the text file directly. Type HELP FOR from the command line and read the documentation carefully.
PING has roughly a 1 second delay between each echo request. So a count of 11 will yield a delay of roughly 10 seconds.
EDIT - originally forgot the critical START command to start the app in its own process
#echo off
set app="myApp.exe"
set limit=11
for /f %%i in (input.txt) do (
start "" %app% %%i
ping localhost -n %limit% > nul
taskkill /im %app% /f
)

Resources