Long Filenames in DOS (and TRUE DOS)

These files are distributed to show how to use Long Filenames In DOS and True DOS
See below for information on how these filenames are stored on the disk.
See below for information on Long Filenames on an NT machine.

Get Microsofts FAT description from here and LFN description from here.
(The FAT description, version 1.03, from here)
They are in PDF format. You can get a PDF veiwer here.

Alexei A. Frounze has written a FAT library at http://alexfru.narod.ru/os/fat/fat.html. Here in the States, it takes some time to download the file from that page, so you can download the .zip file from here (updated: 03 Dec 2006). Some new updates and a few image utilities are added. A great resource of info if you are starting out your OS, or want to make sure your current FAT code works.

DOS Session (only)
Make sure to use service 71A0h to make sure that the current system will allow long file names.
-If the system does not support long filenames, then AX will be 7100h and the CARRY will be set.

NEVER use these services on a system that does not support long file names.

The AH register is 71h while the AL register contains the old DOS AH registers' setting. All other registers have the same use.

Use these services just like the older INT 21h services, but replace AH with 71h, and put in AL the old AH value.

   service # (AX)         Function name
      710Dh             Reset Drive
      7139h             Create Directory
      713Ah             Remove Directory
      713Bh             Set Current Directory
      7141h             Delete File
      7143h             Get/Set File Attrbs.
      7147h             Get Current Directory
      714Eh             Find First File
      714Fh             Find Next File
      7156h             Move or Rename File
      7160h             Get Truename (get shortname)
      716Ch             Extended Open/Create File
      71A0h             Get Volume Info
      71A1h             Find Close
      71A6h             Get File Info
      71A7h             Time Conversion
      71A8h             Generate Short Name
      71A9h             Server Create Open
      71AAh             Create/Terminate SUBST
      43FFh*            BP=5053h, CL=39h Create Directory (Win 98/DOS 7.20)
      43FFh*            BP=5053h, CL=56h Rename File (Win 98/DOS 7.20)

*these two functions are equivalent to DOS services 39h and 56h, but with a maximum path length of 128 characters instead of 67.  However, unlike the other functions above, these functions are available under bare DOS and not just in a Windows DOS box.

See Ralf Brown's Interrupt list for more info on long file names.

Get an example program in C from here (3k)
Get an example program in Pascal from here (3k)

How to get the short file name of a long file name:
; you must have a file named "thisisalongfilename.extension"
; for this demo to work

; Assemble with NBASM

.model tiny
            push cs      ; make sure es=ds=cs
            push cs
            pop  ds
            pop  es
            mov  ax,714Eh
            mov  dx,offset FileName
            mov  di,offset FDRec
            xor  cx,cx      ; <--- Use this for file names
            ;mov  cx,0010h  ; <--- Use this for directory names
            mov  si,01h
            int  21h

            mov  si,offset ShortName
PLoop1:     lodsb
            or   al,al
            jz   short PLoopD
            int  29h
            jmp  short PLoop1
PLoopD:     ret

FileName    db  'thisisalongfilename.extension',0
FDRec       dw  00h,00h          ; File attribs
FileCrt     dw  00h,00h,00h,00h  ; File Creation date
LastAcc     dw  00h,00h,00h,00h  ; Last Access
LastMod     dw  00h,00h,00h,00h  ; Last Modified
FileSizeH   dw  00h,00h          ; File Size Hi
FileSizeL   dw  00h,00h          ; File Size Lo
            dup 8,?              ; reserved
FullName    dup 260,?            ; Full Long name
ShortName   dup 14,?             ; Short name


True DOS
View long filenames without Windows 95. The included C source code, written by the people from PC Magazine, shows how to view long filenames in True DOS mode. Not a Windows 95 DOS session.
I found it on the ZD-NET web site, cleaned it up, ported it to MS Quick C 2.5 (Small model) and now am including it here (22k) for your benefit.
Also have a look here and get LFNDOS.ZIP. The source code is also included as a separate zip file.

Accessing Long File Names in True DOS directly from the disk. Any DOS version supporting a FAT file system.

First, let us discuss the regular DOS File Directory Entry Area: (FDE Area)
The Root Directory contains 32-byte FDE's. These 32 byte entries contain information about the file: File Name, attribute, time, date, etc.
Here is the format of a regular DOS File Directory Entry:
 Offset   Size   Description (As of DOS 5.0)
   00h     8       filename (blank-padded) 
                    (First byte E5h if file deleted or the FDE is free)
   08h     3       filename ext (blank-padded)
   0Bh     1       attributes
   0Ch     10      reserved
   16h     2       Time
   18h     2       Date
   1Ah     2       Starting cluster number
   1Ch     4       File Size
Just a note: In later versions with a FAT 32 system, offset 14h was used for the high order word of the Starting Cluster Number.

Now, if we change this format to add LFN support and write to the disk, what happens when we boot to DOS directly and access the disk? Ouch!!!!!!

Did you note that in offset 00h, if the first byte in the file name is E5h, the file has been deleted and the FDE is free for use? Also, what if we use an attribute of subdirectory and volume label for the same FDE? This could not happen, because the volume name can not be a subdirectory entry. So, Win9x has used the file attribute byte to denote that this FDE is part of a long filename entry.

Let us say we have a file with a LFN of Thisisalongfilename.andextension and we want to save it to disk. However, the name is longer than the 8.3 format that is allowed in the regular FDE. With Win9x LFN's, we use more than one FDE to store the name. These are called individual slots, with the short filename stored as the last slot.

Here is the format of a LFN File Directory Entry:
 Offset   Size   Description
   00h     1       LFN record sequence and flags byte
   01h    10       long file name (first part) (16 bit Unicode, see below) 
   0Bh     1       Attributes (0Fh)
   0Ch     1       reserved
   0Dh     1       Check sum for short file name
   0Eh    12       long file name (second part) (16 bit Unicode)
   1Ah     2       First cluster number (always 0000h for LFN records)
   1Ch     4       long file name (third part) (16 bit Unicode)
Let us look at the first byte. This is the LFN slot number in the wanted sequence of slots for this LFN. Bits 5-0 are the sequence number with the last sequence number OR'd with 40h. These LFN slots are directly behind the regular short file name File Directory Entry.

A note : Bit 6 of above denotes last LFN slot for file (i.e: OR'd with 40h above), and Bit 7 set if file deleted.

So, all we have to do is find enough empty FDE tables to hold the LFN and the regular entries. Each LFN FDE will hold 13 characters of the LFN. There are 26 bytes used for the LFN, but each character uses a 16 bit word. Usually, each word contains the ascii character followed by a null char (00h).

Since each LFN FDE will hold 13 chars of the LFN, and we need 32 bytes for the above LFN, we will need (32/13) or 3 LFN FDE plus 1 regular FDE. So let us find 4 consecutive empty FDE's. Once we have found them, let us place the regular FDE with the short filename in the 4th slot.

Please note that the LFN FDE's are in reverse order. Meaning that the first part of the LFN is in the FDE in slot 3, with the second part of the LFN in slot 2, and the last LFN FDE needed in slot 1 with its first byte OR'd with 40h. So the first byte in the FDE in slot 3 would be 01h, with the first byte in slot 2, being 02h, and the first byte in slot 1, being 43h.

With all of this in mind, the layout of the four FDE's would be:
other entries....
slot #1, id = 43h, characters = "ension"
slot #2, id = 02h, characters = "lename.andext"
slot #3, id = 01h, characters = "Thisisalongfi"
slot #4, regular short filename Directory entry
other entries....
That is about all there is to it. Not very difficult at all.

You must remember a few things including:
  You can not always assume that there is a LFN FDE for every regular FDE even though the FDE preceding it is empty.

Use caution when writing back to the disk. You can only read and write full sectors at a time, so make sure that you don't corrupt the unused part of your buffer that you read in.

Calculating the Check Sum (the byte at offset 0Dh).
The checksum (CRC) is calculated quite simple:
  unsigned char crc = 0;
  unsigned char   i = 0;

  //actual name is:  ashort_n.ame
  char shortname[11] = "ashort_name"; 

  crc = 0;  // make sure crc = 0 each time one is calculated
  for (i=0; i<11; i++)
    crc = ((crc<<7) | (crc>>1)) + shortname[i];
  //                ^ = the OR operator (shift backspace key)
  // crc now equals byte for directory entry

Long Filenames on an NT machine
Posted by Wojciech Galazka. (Thanks Wojciech Galazka)

"...This package provides Win 95 LFN API in an Windows NT 4.0 DOS box.
This version supercedes any previous version.
Users of previous versions should replace them with this newest file"

Last updated: 18 Oct 2000
Download the zip files: ntlfn08b.zip (25k) and ntlfn08s.zip (64k) and unzip using the -d parameter.

View the latest faq list and the readme file for any questions you might have before you download the driver above.

Download (lfnlib.zip) (61k) a simple library that gives access to file i/o functions that can handle long file names. It works under Windows 95 that provides LFN API natively and under Windows NT 4.0 once LFN API Services are loaded. It should work under Windows 2000 as well though it has not been tested. (The source needs to be compiled to generate lib files before actually using the library. The compiler used was Borland C 3.1.)

Most of the traffic to this file comes from people using DJGPP. You can also subscribe to the comp.os.msdos.djgpp news group dedicated to this compiler.

Long Filenames on a DOS machine
If you are looking for a driver to allow the use of LFN's in True DOS, get LFNDOS.ZIP from here. (direct link)
Unfortunately, the site now has lots of adverts and pop ups :(

Try another utility by David Spear from the following link: http://www.odi.ch/prog/lfn/index.php