Deleting files with special characters in their names, in Windows

A couple of directories in Windows couldn’t be deleted by Windows Explorer, because they had unprintable characters (I’m assuming) in their names.

 Volume in drive D is Data
 Volume Serial Number is 8C47-34BD

 Directory of D:\tmp

28/09/2012  11:34 AM              .
28/09/2012  11:34 AM              ..
26/10/2010  01:51 PM              954321.
               0 File(s)              0 bytes
               3 Dir(s)  89,164,262,548 bytes free

On on hitting Delete it replied “Could not find this item. This is no longer located in D:\tmp”. I tried on the command line, a similar error:

D:\tmp>rd 954321.
The system cannot find the file specified.

The security properties of the folder looked weird, saying “The requested security information is either unavailable or can’t be displayed.”:

A screenshot of the top part of the properties dialog, showing the Security tab.

So I faffed around trying to change ownership, filenames, etc. all with no luck. Nothing seemed to see these files as existing except for Windows Explorer and ls -force.

In the end Superuser came to the rescue, as it often does, with the suggestion of referring to the file by its shortname, which can be got via dir /x.

D:\tmp>rd /s 954321~1
954321~1, Are you sure (Y/N)? y

Agh. Why are the simple things so hard to remember sometimes?…

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.