# Batch File Help



## rcmpayne (Sep 21, 2008)

Hello All, 

I was wondering if anyone could give me a hand in creating a better batch file.

Want I am currently doing is having a program run to monitor a log file, when it finds a specific word in the log file it launched a batch file. The batch file does a net stop and a netstart of a service, then starts the command again.

Want I want to do is rather then restart a service on every time it sees a specific word I want it to do it after it sees the word/logline 3 times.

In the batch file rather then just stopping/starting the service, can we have it create a dummy text file and and add a line of somesort to it, then start the comand again, then add a 2nd logline to the same dummy file and run the command again. On the 3rd time it deletes the temp file restarts the service and starts the regular command again?

Program.exe -watch c:\info.txt -eventid 1223 -command cmd.bat

Bat file currently has this

Netstop 'Service Name'
ping 127.0.0.1 -n 11 -w 11000 > nul
Netstart 'Sevrice Name'
ping 127.0.0.1 -n 11 -w 11000 > nul
Program.exe -watch c:\info.txt -eventid 1223 -command cmd.bat


----------



## TheOutcaste (Aug 8, 2007)

I'm assuming that *Program.exe watch c:\info.txt eventid 1223 command cmd.bat* is the program that monitors the *c:\info.txt* file and calls the batch file *cmd.bat* when *eventid 1223* occurs.

This should do the trick. It creates a temp file in the same folder as *cmd.bat* named *cmdCount.tmp*. If you change the name of the batch file, the tmp file name will change to match.
If you want to restart after a larger number of occurrences, just change the red number to 1 less than the count to restart on, so if you want to start on the 5th occurrence, use 4.
I've changed the Ping statement, as the *-w* parameter won't have any effect when pinging an address that responds. You can use just *-n X*, where *X* will be approximately 1 second per ping, or you can ping a non-existent IP and use the *-w* switch.

```
@Echo Off
SetLocal
Set _CFile=%~dpn0Count.tmp
If NOT Exist "%_CFile%" (Echo.1:>"%_CFile%")&Goto _Watch
For /F "Tokens=1 Delims=:" %%I In ('Type "%_CFile%"') Do Set _Count=%%I
If %_Count%==[COLOR=Red]2[/COLOR] Goto _Restart
Set /A _Count+=1
>>"%_CFile%" Echo.%_Count%:
Goto _Watch
:_Restart
Del "%_CFile%"
Net Stop "Service Name"
Ping 1.0.0.0 -n 1 -w 11000 > Nul
Net Start "Service Name"
Ping 1.0.0.0 -n 1 -w 11000 > Nul
:_Watch
Program.exe -watch c:\info.txt -eventid 1223 -command cmd.bat
```


----------



## rcmpayne (Sep 21, 2008)

hey bud, it kinda worked but getting a error with the program.exe, i sent you a pm with more details.

i thkink we may need to have the batch file close the old program and start a new one. i tried exit but that did not seam to work. is there a way to ctrl+C in a batch file?

:_Watch
exit
Program.exe -watch c:\info.txt -eventid 1223 -command


----------



## TheOutcaste (Aug 8, 2007)

I'm not familiar with the monitoring program, but if it continuously monitors the file then it doesn't need to be restarted by the batch file.
By continuous, I mean that Program.exe monitors the file, and when the event occurs it calls the batch file and keeps running. If the event happens again, it calls the batch file again. In that case, just remove/remark the last line in the above code, see if that will do it.

If not, I'll have to download it and give it a spin to see if I can figure out what's going on.


----------



## rcmpayne (Sep 21, 2008)

the file was designed to be ran from command prompt and to watch a .txt file for anything. once it found the logline or part of that it would call the batch file and once the batch fil was ran it would close the batch file and the first command window. my issue is that, i dont want it to call the batch file on the first instence but rather on the 3rd. 

the program with your batch file opens via cmd and starts watching the log for for the line, when it sees it it calls the batch file but the oringal instance stays open, this is the issue i think as its awaiting on the batch file to compleate. 

i have emailed you the program. if you would like to have a look


----------



## TheOutcaste (Aug 8, 2007)

OK, you want to start the log file monitor, wait for an event to happen 3 times, and on the 3rd time, stop and then restart a service. Seems to me you would want to keep monitoring at that point, so if the event occurs 3 more times, you'll stop and then restart the service again.
I created a folder named C:\Log files, and put the program in there.
cmd.bat is in C:\Scripts
You double click cmd.bat, it checks to see if the monitor is running.


 If not, it starts it in a Minimized Window titled Log File Monitor.
 If it is running, it assumes the event occurs and checks to see if it's the 3rd time.
If not, it outputs the current event count to the Log Monitor Window
If it is the 3rd, it stops the service then restarts it

If it's the 3rd time, it stops the service, waits, then restarts the service, and waits again, then exits. The monitor keeps running. If you want it to stop after the 3rd event, just uncomment the indicated line.
Set the paths and file names in the blue lines, spaces are OK, but do not use quotes. The path must have the trailing slash. Or you can remove the variables and just code the path and names if desired.
Set the event(s) in the red line. Remove the /MIN if you don't want the window to start minimized
I used Echo statements for the net stop, start, and ping commands, so remove the *Echo* command to make them functional

```
@Echo Off
SetLocal
[COLOR=Blue]Set _LogPath=C:\Log Files\
Set _LogName=Log.txt
Set _ProgPath=C:\Log Files\
Set _ProgName=Program.exe[/COLOR]
Set _CFile=%~dpn0Count.tmp
TaskList /FI "IMAGENAME eq %_ProgName%"|Findstr /I "%_ProgName%">Nul
If %ERRORLEVEL%==0 Goto _Event
If Exist "%_CFile%" Del "%_CFile%"
Start "Log File Monitor" /D"%_ProgPath%" [COLOR=Red]/MIN[/COLOR] "%_ProgName%" -l "%_LogPath%%_LogName%" [COLOR=Red]-events 40702[/COLOR] -action %~dp0cmd.bat
Goto:EOF
:_Event
Set _Count=1
If NOT Exist "%_CFile%" (Echo.%_Count%:>"%_CFile%")&Goto _Watch
For /F "Tokens=1 Delims=:" %%I In ('Type "%_CFile%"') Do Set _Count=%%I
If %_Count%==2 Goto _Restart
Set /A _Count+=1
>>"%_CFile%" Echo.%_Count%:
Goto _Watch
:_Restart
Del "%_CFile%"
[B][COLOR=Sienna]Echo[/COLOR][/B] Net Stop "Service Name"
[B][COLOR=Sienna]Echo[/COLOR][/B] Ping 1.0.0.0 -n 1 -w 11000 > Nul
[B][COLOR=Sienna]Echo[/COLOR][/B] Net Start "Service Name"
[B][COLOR=Sienna]Echo[/COLOR][/B] Ping 1.0.0.0 -n 1 -w 11000 > Nul
: :Uncomment the next line if you want to stop monitoring the log file
[COLOR=DarkRed]:Taskkill /FI "IMAGENAME eq %_ProgName%"[/COLOR]
Goto:EOF
:_Watch
Echo Event Occurred %_Count% Time(s)
```
This is the output in the Log File Monitor window when the event occurs 4 times:

```
Event Occurred 1 Time(s)
Event Occurred 2 Time(s)
Net Stop "Service Name"
Net Start "Service Name"
Event Occurred 1 Time(s)
```
Note that if you run the *cmd.bat* file while the program is monitoring, it will act just like the event occurred.


----------



## rcmpayne (Sep 21, 2008)

tried that this morning but it comes back with 'ERROR: Invalid query' when it sees the first entrie in the logs.


----------



## TheOutcaste (Aug 8, 2007)

Sounds like that error is coming from the monitoring tool. I'd try adding the *-debug* switch to the command line that starts it, see if that will provide more info.
Only errors I've been able to generate are these:
Error: Invalid option(s) specified.
Error: BRK::ValidParams - Exiting due to invalid parameter(s)


----------



## rcmpayne (Sep 21, 2008)

Hello, 

I have the batch file working now but have one more question.

Run.cmd

program.exe -l "C:\Program Files\Program\Logs\20090811" -events 30097 -type LOG -action ~cmd.bat

Is there a way to have another batch file or another way to update the command line everyday to the new day "20090811"

20090811 -> is a folder and it rolls to a new day at 12:00 every day


----------



## TheOutcaste (Aug 8, 2007)

```
@echo off
:: Get today's date in YYYYMMDD Format
Call :_GetDate
program.exe -l "C:\Program Files\Program\Logs\%_fdate%" -events 30097 -type LOG -action ~cmd.bat
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_GetDate
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified by The Outcaste from SortDate Written by Rob van der Woude
:: [URL="http://www.robvanderwoude.com/"]http://www.robvanderwoude.com[/URL]
::
Set _Date=%date%
If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
```


----------

