Skip to main content

Windows gpx2tcx Batch File

· 4 min read
gpx2tcx.cmd screenshot

I've previously presented a GPX + HRM to TCX AWK script (see this post). And while this script can be very useful on its own, I've also written a Windows batch file, and a Bash script to make use of that AWK script a little easier. In this quick post, I'll be presenting the former of the two - ie the Windows batch file.

Let's get straight to the script:

:: gpx2tcx.cmd by Paul Colby (https://colby.id.au), no rights reserved ;)
:: $Id: gpx2tcx.cmd 298 2012-02-24 22:09:33Z paul $
@echo off

:: Optional: uncomment the following line to change UTC timezones.
set TIMEZONE=+11:00

:: Optional: uncomment the following line to set the altitude of the first point.
:: This will do nothing if your HRM files actually include altitude data, but helps
:: devices like the RCX5 with no altitude data, and sites like Strava that don't like that.
set ALTITUDE=1.0

:: Update this path to include the location(s) UnxUtils is insstalled.
set PATH=%PATH%;C:\Program Files\UnxUtils\usr\local\wbin;C:\Program Files (x86)\UnxUtils\usr\local\wbin

:: Jump the to "main" block.
goto main

:: UnxUtils' sed does not support the -i flag, so we perform the equivalent manually.
:: usage: call:sedInPlace script filename
:sedInPlace
sed.exe -re "%~1" "%~2" > "%~2.tmp"
diff.exe -qs "%2" "%2.tmp" > nul
if ERRORLEVEL 1 copy "%2.tmp" "%2" > nul
del "%2.tmp"
goto :EOF

:convert
if not exist "%~1.gpx" goto :EOF
echo Processing %~1...
gawk.exe -f "gpx2tcx.awk" -v ALTITUDE=%ALTITUDE% -v HRMFILE=%~1.hrm "%~1.gpx" > %~1.tcx
if defined TIMEZONE call:sedInPlace "s/([>""""][0-9]{4}(-[0-9]{2}){2}T([0-9]{2}:){2}[0-9]{2})Z([<""""])/\1%TIMEZONE%\4/g" %~1.tcx
goto :EOF

:main
FOR /f %%A IN ( 'ls.exe -1 *.gpx *.tcx 2^>^&1 ^| sed.exe -e "s/\.[^.]*$//" ^| uniq.exe -c ^| sed -ne "s/^ *1.//p"' ) DO call::convert %%A

pause

It's pretty simply really. To use it, all you do is drop the batch file into a directory containing GPX and HRM files, along with the requisite gpx2tcx.awk script, double-click the batch file, and it will automatically convert all GPX files it finds that do not already have corresponding TCX files.

Here's an example screenshot:

gpx2tcx.cmd screenshot

As you can see, it's very simple and easy to use. There are a few things to note, however.

Although the basic gpx2tcx.awk script that this batch file uses only required one command line utility not found on Windows by default (ie gawk), this script requires a lot more. Specifically, this script requires: diff, gawk, ls, sed, and uniq. All of these utilities are included in the excellent opens-source UnxUtils package, which I highly recommend (even if you're not using any of my batch files).

Either way, all of the above command line utilities will need be in your system path, or if you prefer, you can edit the set PATH line in the above batch file to include the location you installed UnxUtils (or equivalent) to.

Another interesting thing to note, is that the above batch file has an option to override the TCX file's timezone by uncommenting the set TIMEZONE line. The reason that option is available, is because I set my RCX5's time to my own local time (who wouldn't), yet the exports GPX files from the TCX5 claim that it's timestamps are all UTC, which they are not. So, if TIMEZONE is set, then the batch file will replace all UTC indicators (ie the z character at the end of timestamp values) with the specified string (+11:00 in my case, since my zone is currently 11 hours ahead of UTC).

The rest should be pretty self-explanatory, but if not, just ask! :)

Here's a quick summary of relevant links:

Update

Just updated the script (1.0.0.264) to include support for the ALTITUDE variable just added to the AWK script.

Update 2

2012-02-25: Just updated the script (1.0.0.298) to not show errors if TCX files are present without matching GPX files.

Attachments