Experimenter
Experimenter

Reputation: 167

Echo output for each value in given list in bat file

With below code I'm struggling to print calculation for each value in a given list. Currently it prints total combined time for both users, I want to echo individual time for each user which is listed in 2nd row of this code.\ Any help will be appreciated.

@Echo off
For %%U in (a3rgcw shukla) Do (

PushD "H:\Syslogs\" ||(Echo couldn't find dir & Pause & Exit /B 1)

Set "TotalSecs=0"

For %%F in ("*%U%*.txt") Do For /F "delims=" %%A in ('
    findstr /I "system.log.created End.of.session" "%%F"
') Do (
    Set "Flag="
    Echo=%%A|findstr /I "system.log.created" 2>&1>Nul && Set "Flag=Start"
    if defined Flag (
        FOR /F "tokens=11" %%T in ("%%A") Do Call :TimeToSecs Start "%%T"
    ) Else (
        FOR /F "tokens=8" %%T in ("%%A") Do Call :TimeToSecs Stop "%%T"
    )
)
Echo TotalDuration for %%U:%TotalDur%
)
Echo:
PopD
Goto :Eof

:TimeToSecs
Set "%1_HMS=%~2"
Echo:%~2|Findstr "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]" 2>&1>Nul || (Echo wrong format %2&Goto :Eof)
For /F "tokens=1-3 delims=:" %%H in ("%~2"
) Do Set /A "%1=(1%%H-100)*60*60+(1%%I-100)*60+(1%%J-100)"
If %1 neq Stop Goto :Eof
Set /A "Diff=Stop-Start,TotalSecs+=Diff"
Call :Secs2HMS Dur %Diff%
Call :Secs2HMS TotalDur %TotalSecs%
::Echo Session from %Start_HMS% to %Stop_HMS% Duration:%Dur%      TotalDuration:%TotalDur%


Goto :Eof

:Secs2HMS var value
setlocal
set /a "HH=%2/3600,mm=(%2-HH*3600)/60+100,ss=%2 %% 60+100"
Set "HHmmss=   %HH%:%mm:~-2%:%ss:~-2%"
endlocal&set "%1=%HHmmss:~-10%
Goto :Eof

Current output:

TotalDuration for a3rgcw:   7:15:00
TotalDuration for shukla:   7:15:00

Desired output:

TotalDuration for a3rgcw:   5:15:00
TotalDuration for shukla:   2:00:00

Sample file named shukladfdf for 2nd user:

sdsdf system log created on  ghg Thursday, 9 August 2018, 20:30:45 on India 
Standard Time
dfg
drdwewed
end of session as 9 August 2018, 22:30:45 on India Standard Time

Upvotes: 1

Views: 115

Answers (1)

michael_heath
michael_heath

Reputation: 5372

@Echo off
Setlocal

PushD "H:\Syslogs\" ||(Echo couldn't find dir & Pause & Exit /B 1)

For %%U in (a3rgcw shukla) Do (

    Set "TotalDur="
    Set "TotalSecs=0"

    For %%F in ("*%%U*.txt") Do For /F "delims=" %%A in ('
        findstr /I "system.log.created End.of.session" "%%F"
    ') Do (
        Set "FileName=%%F"
        Set "Flag="
        Echo=%%A|findstr /I "system.log.created" 2>&1>Nul && Set "Flag=Start"
        if defined Flag (
            FOR /F "tokens=11" %%T in ("%%A") Do Call :TimeToSecs Start "%%T"
        ) Else (
            FOR /F "tokens=8" %%T in ("%%A") Do Call :TimeToSecs Stop "%%T"
        )
    )

    Set "UsrName=%%U:          "
    Call :Print UsrName
)
Echo:
PopD
Goto :Eof

:Print
If /i "%~1" == "UsrName" (
    Echo TotalDuration for %UsrName:~,10% %TotalDur%
) else If /i "%~1" == "FileName" (
    Echo Session (%FileName%^) from %Start_HMS% to %Stop_HMS% Duration:%Dur%      TotalDuration:%TotalDur%
)
Goto :Eof

:TimeToSecs
Set "%1_HMS=%~2"
Echo:%~2|Findstr "[0-2][0-9]:[0-5][0-9]:[0-5][0-9]" 2>&1>Nul || (Echo wrong format %2&Goto :Eof)
For /F "tokens=1-3 delims=:" %%H in ("%~2"
) Do Set /A "%1=(1%%H-100)*60*60+(1%%I-100)*60+(1%%J-100)"
If %1 neq Stop Goto :Eof
Set /A "Diff=Stop-Start,TotalSecs+=Diff"
Call :Secs2HMS Dur %Diff%
Call :Secs2HMS TotalDur %TotalSecs%
Call :Print FileName
Goto :Eof

:Secs2HMS var value
setlocal
set /a "HH=%2/3600,mm=(%2-HH*3600)/60+100,ss=%2 %% 60+100"
Set "HHmmss=   %HH%:%mm:~-2%:%ss:~-2%"
endlocal&set "%1=%HHmmss:~-10%"
Goto :Eof

Changed %U% to %%U.

Changed Echo TotalDuration for %%U:%TotalDur% to Call Echo TotalDuration for %%U:%%TotalDur%% which delays expansion until time of execution, instead of parse time.

Added missing double quote with 2nd last line to close.

Added Setlocal to top of script as perhaps a 2nd run of the script in the same CMD session could set predined variables with values.

Added label :Print to echo the output to avoid use of delayed expansion.

Upvotes: 2

Related Questions