This is my Windows script which is started every x minutes:
cd C:\
cd Speedtest
speedtest.exe -s 18571 -f csv>>Speedtest.log
How to add a timestamp to every log line?
This is my Windows script which is started every x minutes:
cd C:\
cd Speedtest
speedtest.exe -s 18571 -f csv>>Speedtest.log
How to add a timestamp to every log line?
Let us assume the application speedtest
runs just one test run on each execution and outputs one or more lines without empty lines and on each line the current time on starting this application should be written first into the CSV file with ,
as list separator (default in most countries for CSV files). In this case the following batch file could be used for this task:
@cd /D "C:\Speedtest" 2>nul && @for /F delims^=^ eol^= %%I in ('speedtest.exe -s 18571 -f csv') do >>"Speedtest.log" @echo %TIME%,%%I
The first command CD changes the current directory to C:\Speedtest
. This command fails if the directory does not exist. The error message output on changing directory failed is redirected with 2>nul
from handle STDERR (standard error) to device NUL to suppress it. There is nothing output by CD if the current directory could be successfully changed to the specified directory.
The operator &&
results in executing the next command FOR only if CD exited with 0
indicating a successful change of the current directory which means the specified directory C:\Speedtest
really exists.
FOR with option /F
and a set specified between two '
results in execution of one more command process in background with %ComSpec% /c
and the strings between the two '
appended as further arguments. So executed is on Windows installed to C:\Windows
:
C:\Windows\System32\cmd.exe /c speedtest.exe -s 18571 -f csv
The started Windows command process executes speedtest.exe
with the four specified arguments and then closes itself because of option /c
.
FOR captures all output written to handle STDOUT (standard output) and processes it after started cmd.exe
terminated itself.
FOR ignores all empty lines on processing the captured lines.
FOR would split up by default each line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab separated string of a non-empty line to specified loop variable I
. This string splitting behavior is not wanted here. For that reason option delims=
is used to define an empty list of string delimiters which disables line splitting behavior.
FOR would ignore by default all lines on which first substring starts with a semicolon because of eol=;
is the default for end of line option. It is unknown if speedtest
outputs the lines with a semicolon at beginning. For that reason the option eol=
is used to define no end of line character which means all lines except empty lines are assigned completely to specified loop variable I
.
The two options delims=
and eol=
are specified usually in a double quoted argument string to get the two equal signs and the space character between interpreted as literal characters and not as argument separators by Windows command processor. But "delims= eol="
cannot be used here as FOR would interpret in this case "
as end of line character. Therefore the two options must be written without "
which requires escaping the two equal signs and the space character with ^
to be interpreted as literal character instead of argument separators by Windows command processor.
Windows command processor parses the entire line before execution of command CD and replaces during this parsing process %TIME%
by the current value of the local time in format defined by the country configured for the account used to run this batch file. So the time written into the CSV file is the time on which the entire command line was parsed by cmd.exe
before executing the command CD and on success next the other commands and executables.
The redirection operator >>
with the file name Speedtest.log
is specified left to command ECHO to be 100% safe that the line is written correct into the file without a trailing space even on ending with number 1
to 9
. It can be seen on running this batch file from within a command prompt window without the three @
that cmd.exe
moved >>"Speedtest.log"
to end of the command line with inserting left to this string a space and 1
before executing command CD. It must be always taken into account how a command line looks like after processing it by Windows command processor before executing the command and not how it is written in the batch file.
Note: If the configured country is Germany, Austria, Switzerland, Luxembourg or Liechtenstein with time format HH:mm:ss,ms
and list separator ;
, it is necessary to use a semicolon instead of a comma between %TIME%
and %%I
to produce a valid CSV file. speedtest
outputs the data in this case hopefully also with ;
instead of ,
. Otherwise if speedtest
outputs the data independent on list separator of the configured country always with a comma, it would be necessary to use on command ECHO either "%TIME%",%%I
or %TIME:,=.%,%%I
to get a valid CSV file using comma as separator.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
cd /?
echo /?
for /?
set /?
See also: