So if 'tracert google.com >>tracelog.txt' works from a command line, what do I have to do to get it to redirect to a file from inside a batch script, considering it's being called by schtasks.exe?
I read on the schtasks 'help' screen something about two sets of quotes, one for cmd and one for schtasks, but I don't understand how that applies here.
The object is to run tracert on a problematic url to discover any overt latency in any of the hops. The task is meant to run once, repeating every x minutes for a duration of t time while appending the results to a log file (debugging an online game).
So that's the part that's got me, getting a log file out of it.
As always, any help is appreciated.
@echo off
if [%1]==[] goto usage
set "START=13:10"
set "DUR=00:06"
set TRACE="tracert.exe %1 >>C:\urltrace\tracelog.txt"
SCHTASKS /CREATE /TN "URLtrace Task" /TR %TRACE% /RL HIGHEST /SC ONCE /RI 2 /DU %DUR% /ST %START% /F
exit /B 1
:usage
@echo Syntax: urltrace <url> Examp: urltrace google.com
exit /B 1
The task can be done with a batch file with just two command lines:
@if "%~1" == "" echo Syntax: %~n0 ^<domain name ^| IP address^>& echo Example: %~n0 google.com& exit /B
@%SystemRoot%\System32\tracert.exe %1 >>"%~dp0TraceLog.txt"
START is an internal command of cmd.exe
processing a batch file. It is possible using the string START
as label or as name of an environment variable but it is advisable not doing that. There should be used a different variable name like StartTime
. A label is not really needed here.
The not necessary definition of the environment variable TRACE can be wrong depending on value of %1
(first argument string passed to the batch file) because of %1
could contain "
at the beginning and the end of the argument string which requires escaping them in the command line executing %SystemRoot%\System32\schtaks.exe
.
The trace route tool should never be run with a complete url. It should be run always with an IP address or a domain name of which %SystemRoot%\System32\tracert.exe
first accesses the primary and if not available the secondary DNS (domain name system server) to get the IP address of that domain name. An IP address as well as a domain name cannot contain a space character or other characters with a special meaning for cmd.exe
and it is possible to pass that argument string always without surrounding "
to tracert.exe
which means no quotes escaping would be necessary in schtasks.exe
argument string. There is nevertheless used in the second command line %1
instead of %~1
as the user calling the batch file could pass an argument string to the batch file containing even an ampersand character in which case the argument string was enclosed in "
by the user. The string passed to the batch file is passed further as is to the trace route tool for that reason.
There is no reason for creating a scheduled task just to run the trace route executable. The access of external networks does usually not depend on a user account or a specific access time.
The square brackets have no special meaning for the Windows Command Processor cmd.exe
processing a batch file. The condition if [%1]==[]
is of syntax from COMMAND.COM
of MS-DOS, Windows 95, Windows 98 and Windows ME. There should be used "
on comparing two strings instead of [
and ]
because of the double quotes have a special meaning for cmd.exe
in comparison to COMMAND.COM
. The character "
restricts the beginning and the end of an argument string on which all characters between should be interpreted literally by cmd.exe
with exception of %
(environment variable, loop variable or batch argument reference) and !
on delayed variable expansion is enabled too. Please read symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files. This answer explains in full details how a string comparison is done by command IF of cmd.exe
and why if "%~1" == ""
is best to check if a batch file was started without any argument string or with an empty argument string by using ""
.
The first command line uses this quite safe string comparison and outputs two lines for the user if the batch file is executed without passing an IP address or a domain name to the batch file.
The output lines reference with %~n0
the file name of the batch file without file extension to output the usage information always with correct name.
The characters <
and >
and |
are interpreted as redirection operators on found by cmd.exe
in a command line outside a double quoted argument string and not escaped with character ^
for being interpreted as literal characters which is the reason for escaping them all with ^
in the first output usage information line.
A space character between the end of a text output with command ECHO and the unconditional command operator &
would be output also by ECHO as trailing space because of ECHO does not interpret a space as argument string separator. This is the reason for no space left to the two &
although the trailing space cannot be seen by a user of the batch file.
%~dp0
references the drive and path of argument 0 which is the full path of the batch file. This path always ends with a backslash. The file TraceLog.txt
is created respectively extended in the batch file directory and not in the current directory with the standard output of the trace route tool. It could be a good idea to append on the second command line also 2>&1
to get an error output of the trace route tool also written into the file TraceLog.txt
.
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.
call /?
cmd /?
echo /?
exit /?
if /?
tracert /?
See also: