Long filenames in Windows

Long file names in Windows are a hacked-in kludge, and sometimes it shows. Here’s an example of how, if you’re not careful, you can lose data:

C:TEMP>mkdir robintemp

C:TEMP>cd robintemp

C:TEMProbintemp>dir
 Volume in drive C has no label.
 Volume Serial Number is C899-6ADC

 Directory of C:TEMProbintemp

01/28/2004  02:26p      <DIR>          .
01/28/2004  02:26p      <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  34,996,314,112 bytes free

C:TEMProbintemp>echo Hi > longfi~1

C:TEMProbintemp>dir
 Volume in drive C has no label.
 Volume Serial Number is C899-6ADC

 Directory of C:TEMProbintemp

01/28/2004  02:26p      <DIR>          .
01/28/2004  02:26p      <DIR>          ..
01/28/2004  02:26p                   5 longfi~1
               1 File(s)              5 bytes
               2 Dir(s)  34,996,314,112 bytes free

C:TEMProbintemp>dir /x
 Volume in drive C has no label.
 Volume Serial Number is C899-6ADC

 Directory of C:TEMProbintemp

01/28/2004  02:26p      <DIR>                          .
01/28/2004  02:26p      <DIR>                          ..
01/28/2004  02:26p                   5                 longfi~1
               1 File(s)              5 bytes
               2 Dir(s)  34,996,314,112 bytes free

C:TEMProbintemp>echo Hi > longfilename01

C:TEMProbintemp>dir /x
 Volume in drive C has no label.
 Volume Serial Number is C899-6ADC

 Directory of C:TEMProbintemp

01/28/2004  02:27p      <DIR>                          .
01/28/2004  02:27p      <DIR>                          ..
01/28/2004  02:27p                   5 LONGFI~2        longfilename01
01/28/2004  02:26p                   5                 longfi~1
               2 File(s)             10 bytes
               2 Dir(s)  34,996,314,112 bytes free

C:TEMProbintemp>mkdir ..foo

C:TEMProbintemp>copy *.* ..foo
longfilename01
longfi~1
Overwrite ..foolongfi~1? (Yes/No/All): n
        1 file(s) copied.

C:TEMProbintemp>

See what happened? The file longfilename01 was copied first, thus creating the short filename longfi~1 according to Microsoft’s filename-creation rules. But the next step was to copy the file longfi~1 from the source directory, and oops! It already exists in the destination!

What should have happened here is that the file longfilename01 should have kept its short file name longfi~2 when it got copied; then there wouldn’t have been a filename collision.

And lest you think this is a contrived example: I ran into exactly this problem about a year and a half ago when trying to do some backups. The solution I came up with was to hack together a quick script that would first copy all the 8.3 filenames, and second copy all the long file names. Then I could be sure that the shortnames chosen by Windows wouldn’t accidentally conflict with a file that was going to get copied over later.

I hope this spares someone some pain someday.

Comments are closed.