Cover V05, I02
Article
Listing 1
Sidebar 1

feb96.tar


Converting UNIX Filenames to MS-DOS

Ed Schaefer and Fred Brunet

As the boundary between UNIX and MS-DOS software development continues to blur, a system administrator may routinely need to move files between the two. The MS-DOS filename is a base name of up to eight characters with an extension of three characters or fewer. UNIX file naming conventions are far more liberal than MS-DOS, so we have developed a UNIX utility, dosnames, which renames every filename suppplied on the command line to an MS-DOS compatible filename. As each eligible file is renamed, the new MS-DOS name and the original UNIX filename are stored in a file called dosnames.lst. Running the utility a second time renames the MS-DOS files back to their UNIX original filenames.

The Conversion Rules

If the dosnames.lst file does not exist, every file listed in standard input which violates the basic MS-DOS convention is renamed using the following conversion rules:

1. Any base filename of more than eight characters is reduced to eight characters.

2. Any filename extension longer than three characters is reduced to three characters.

3. All filenames are downshifted, meaning any filename with mixed case is also downshifted.

4. Directories listed among the arguments and any files in those directories are skipped.

5. Any filename with an unprintable character is skipped.

6. Any filename with more than one period is modified to contain only one period. In UNIX, a hidden file is defined as a file beginning with a period, but the hidden file attribute is more than a naming convention in DOS, so all UNIX hidden files are ignored.

7. Any filename that converts to a name that already exists has a two-digit number inserted before the extension, allowing dosnames to process up to 99 UNIX files with the same first six characters.

If the file dosnames.lst exists, the files listed are restored to their original name using the UNIX mv command. At the end of the conversion, the dosnames.lst file is deleted.

Executing dosnames

dosnames processes filenames supplied as command-line arguments rather than reading every file in the directory. To execute dosnames for only individual files on the command line:

dosnames long_file1 long_file2

To execute dosnames for every file in the present working directory:

dosnames *

The Example

Given the following directory listing in the present working directory:

12345678.123  dosnames.c   long_name1.c long_name2.c two.dots.
BIGNAME       ok_dir       Uppercase    cat.file

you can run:

dosnames *

which provides the following listing to standard output:

9 files to process:
12345678.123 ok
Renaming BIGNAME to bigname
Renaming cat.file to cat.fil
Renaming dosnames.c ok
Renaming ok_dir skipping directory
Renaming long_name1.c to long_nam.c
Renaming long_name2.c to long_n00.c
Renaming two.dots. to two.dots
Renaming Uppercase to uppercas

and writes a record of how the names were mapped to dosnames.lst. This following listing shows the contents of dosnames.lst. The first column is the MS-DOS filename, and the second column is the original UNIX name.

bigname      BIGNAME
cat.fil      cat.file
two.dot      two.dots.
long_nam.c   long_name1.c
long_n00.c   long_name2.c
uppercas     Uppercase

Because the first eight characters of long_name1.c and long_name2.c are the same, the seventh and eighth characters of the DOS filename are changed to a sequence number. Up to 99 files (e.g., long_name99.c) can be converted.

Enhancements

One enhancement to this program would be to change the conversion rules. For example, the UNIX hidden file method could be changed. A version that reads filenames from standard input instead of from the command line would avoid operating system limitations, allowing usage such as:

find . -print|dosnames

About the Authors

Ed Schaefer is a frequent contributer to Sys Admin. He is an Informix Database Administrator and UNIX administrator for jeTECH Data Systems of Moorpark, CA, where he develops Time and Attendance Software. He can be reached at olded@ix.netcom.com.

Fred Brunet has a B.S. in computer science from California State University., Northridge and is currently a C and Informix programmer working on UNIX and MS-DOS platforms for jeTECH Data Systems in Moorpark, CA.