简体   繁体   中英

Count number of times my BATCH file is run

This is one of my last problems for my tool.

I need to be able to count the amount of times it has run. It has to store this information remotely and this has to be able to run in my batch script.(No other languauges)

It will also be running on around ~30 other machines. Does anyone have any ideas about how I go running this?

I thought maybe I could initiate a TELNET to target server and they server count the amount of time it receives a connection on a certain port.

Any help is appreciated!

The easiest method is indeed to simply log the execution pc/user/date/time to your remote pc:

echo %ComputerName% %UserName% %date% %time% >>\\counting_pc\share\run.log
run_my_tool

There is a risk of concurrent write access to the file though, it can be mitigated via a simple mutex file:

set "log=\\counting_pc\share\run.log"
set "mutex=\\counting_pc\share\mutex.log"

:: run_my_tool in current console
start /b run_my_tool

for /L %%a in (1,1,10) do if not exist "%mutex%" (goto log) else (timeout 1 >nul)

:log
echo >"%mutex%"
echo %ComputerName% %UserName% %date% %time% >>"%log%"
del /q "%mutex%"

To count the numbers just open the log file in notepad and see how many lines it has or you can filter it by computer name, user name, time and date.

Counting in a batch file:

  • overall: for /f "tokens=2 delims=:" %%a in ('find /c /v "" run.log') do echo %%a
  • by user: for /f "tokens=2 delims=:" %%a in ('find /c " someuser " run.log') do echo %%a
  • by PC: for /f "tokens=2 delims=:" %%a in ('find /c "somepc " run.log') do echo %%a

This is an example which isn't 100% failproof but demonstrates the idea.

You can measure the Count number of Opening BATCH file This Like =

Set Q=%temp%\1.kj
Echo :>>%Q%
FOR /F "tokens=1,2* delims=:  " %%i in ('find /v /c "*" %Q%') do (Set C=%%k)
IF %C% GEQ 5 & Del "%Q%" & ::Do a Something ...

I found the solution to my problem with PSEXEC.

I remote into a Server for logging, update a txt file with an increment of +1.

Then I have another batch that constantly monitors those log files.

A snippet for anyone interested:

psexec.exe \\xx.xx.85.242 /accepteula -u USERNAME-p PASSWORD"C:\DIRECTORY\GOES\HERE.bat"

The /accepteula makes it so there is less interaction by the user, it agrees to the usage policies.

Thanks for the replies!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM