Getting a proper %DATE% variable in batch files

Windows batch files are hideous things, if one is used to *nix shell scripting. Everything feels wrong! Of course, that’s mostly just because they have a different syntax… but sometimes it’s because they are wrong.

Date formatting, for instance.

There’s an environment variable, %DATE%, that holds the current date, but it’s formatted to the current locale’s ‘short date’ definition, and so does not produce a consistent output from system to system.

Something as simple as naming files becomes a hideous contortion of detecting formats, splitting strings apart, and hopefully re-joining them in the right order. Too annoying for me, today, so I’ve taken a leap out of cmd and into java:

Firstly, compile the following Java class.

import java.text.SimpleDateFormat;
import java.util.Date;

public class date {

    public static void main(String[] args) {
        Date date = new Date();
        String out = new SimpleDateFormat("yyyy-MM-dd").format(date);
$ javac

Then, call it from a batch file with something like this:

echo off
java date > thedate.txt
set /p THEDATE= < thedate.txt
del thedate.txt
echo The date is %THEDATE%

This is ridiculous, yes, but it does at least work.

Open Data Kit

Open Data Kit (ODK) is a free and open-source set of tools which help organizations author, field, and manage mobile data collection solutions. ODK provides an out-of-the-box solution for users to:

  1. Build a data collection form or survey;
  2. Collect the data on a mobile device and send it to a server; and
  3. Aggregate the collected data on a server and extract it in useful formats.

I’m investigating this for use at work, and so far I’m loving it! Just the sort of simplicity, and (it has to be said) correlation to the office-people’s ideas of forms-that-get-filled-in, that I’ve been looking for.