1

i want to have a batch file that run on start up and log when the computer started and for how long it was on. this is the code i use to log when i start the computer:

echo Date:%date% Time:%time% >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt

Here is my idea for a code that will log "time on" as well:

echo Date:%date% Time:%time% >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt

set /a variableA=0

:a

timeout 60 /nobreak

set /a variableA=%variableA%

set /a variableB=%variableA%+1

del "C:\Users\Kristian\Documents\Kristian\HEI\templogfile_%variableA%.txt"

echo.>"C:\Users\Kristian\Documents\Kristian\HEI\templogfile_%variableB%.txt"

goto a

This batch file will start on start up and log for how long the computer was on last time and then add the date and time for this time. then it will start to count the minutes for this time and add them as well next time the computer starts. I only need help with the following part:

What should I put after templogfile_ in

`"C:\Users\Kristian\Documents\Kristian\HEI\templogfile_*" >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt
  • someone just editet my post and changed the question. i dont need to know what i shoud put after templogfile_ – kno010 Sep 13 '16 at 19:07
  • Please dont edit my post if it change the issue. the edited version ask for something else than the original. – kno010 Sep 13 '16 at 19:10
1

As currently written, your batch file always results to

echo.>"C:\Users\Kristian\Documents\Kristian\HEI\templogfile_1.txt"

as %variableB% continues to be 1.

However, next commented code snippet could help with extracting desired value (quoted from your original question):

What comes after "templogfile_" in "C:\Users\Kristian\Documents\Kristian\HEI\templogfile_*"

for /f "delims=" %%G in ('
    dir /b /O:D "C:\Users\Kristian\Documents\Kristian\HEI\templogfile_*"
                         ') do (
    set "_rawNumber=%%~nG"                 gets last file name e.g. templogfile_357
)
    rem there is identical output from next two ECHO commands:
    rem any text remaining in %_rawNumber% variable if `templogfile_` substring is removed
ECHO(%_rawNumber:templogfile_=%
    rem any text following 12th character in %_rawNumber% variable
ECHO(%_rawNumber:~12%

Resources (required reading, incomplete):

0

Thanks :) Here are my final code:

for /f "delims=" %%G in ('
    dir /b /O:D "C:\Users\Kristian\Documents\Kristian\HEI\templogfile_*"
                         ') do (
    set "_rawNumber=%%~nG"               
)  
set variable=%_rawNumber:~12% Minutter
echo %variable% >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt
echo. >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt
echo Date:%date%   Time:%time% >> C:\Users\Kristian\Documents\Kristian\HEI\log.txt

set /a variable=0
:a
timeout 60 /nobreak
del "C:\Users\Kristian\Documents\Kristian\HEI\templogfile_*.txt"
set /a variable=%variable%+1
echo.>"C:\Users\Kristian\Documents\Kristian\HEI\templogfile_%variable%.txt" 
goto a

And here is the VBScript that makes the batchfile run in the background:

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Users\Kristian\Documents\Kristian\HEI\log.bat" & Chr(34), 0
Set WinScriptHost = Nothing

Here is an example from the log:

Date:13.09.2016 Time:21.27.00,57
13 Minutter

Date:13.09.2016 Time:21.40.36,60
6 Minutter

Date:13.09.2016 Time:21.49.47,20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.