Perform summation of integers on each line in a file in `cmd` -


time command line fu in least fu conducive shell, cmd.exe.

how can perform summation of integers contained in file?

you might consider following work:

taskkill /f /im rsync.exe echo %errorlevel% > %temp%/kill_site.log taskkill /f /im ssh.exe echo %errorlevel% >> %temp%/kill_site.log taskkill /f /im 7za.exe echo %errorlevel% >> %temp%/kill_site.log set /a errorresult=1 /f "tokens=*" %%g in (%temp%/kill_site.log) set /a errorresult=%%g+%errorresult% 

but, appears %errorresult% value before for loop during for loop. meaning, resulting %errorlevel% has [the integer value of last line in %temp%/kill_site.log] + [the %errorlevel% set, 1].

in case of exit codes provided taskkill, if taskkill succeeds in killing existing process, exit code 0, resulting %errorresult% in case 1. if process doesn't exist when taskkill called kill it, exit code 128; in case %errorresult% be129`.

i'd %errorresult% total of integers contained on lines in %temp%/kill_site.log. how do this?

[update]

thanks stephen's answer below, have final script follows wanted include future reference other users:

setlocal enabledelayedexpansion taskkill /f /im rsync.exe echo %errorlevel% > %temp%/kill_site.log taskkill /f /im ssh.exe echo %errorlevel% >> %temp%/kill_site.log taskkill /f /im 7za.exe echo %errorlevel% >> %temp%/kill_site.log set /a errorresult=1 /f "tokens=*" %%g in (%temp%/kill_site.log) set /a errorresult=%%g+!errorresult! if %errorresult% lss 255 sendmail.vbs && eventcreate /l application /so backup_scripts /t warning /id 1 /d "website rsync has killed because long running." endlocal 

it utilizes endlocal.

i realized bit backwards, should checking if processes running previous taking invalid action against non-existent processes, question still resolved. finding if specific running process exists batch script uses similar method of checking %errorlevel% quite easy.

within forloop need delayed expansion variables: use

setlocal enabledelayedexpansion

at beginning of batchfile , change for-loop to

for /f "tokens=*" %%g in (%temp%/kill_site.log) set /a errorresult=%%g+!errorresult!

this because in for-loop %errorresult% use value @ parse-time. !errorresult! use value @ run-time.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -