Someone asked me recently if FreeDOS supports spaces in filenames. FreeDOS is an open source DOS-compatible operating system that you can use to play classic DOS games, run legacy business software, or write new DOS programs. Any program that works on MS-DOS should also run on FreeDOS. And that means FreeDOS comes with the same limitations as classic DOS.
DOS uses 8.3 filenames, which means filenames can only have up to 8 characters for the base name and up to 3 characters for the extension. Filenames are stored in all-uppercase, but are actually case insensitive. That means if you save as file as MYFILE.TXT you can also access it as Myfile.txt or MyFile.Txt or any other mix of uppercase and lowercase.
You can use almost any character in a filename, but in practice this is usually letters, numbers, and certain punctuation characters like !, #, $, %, (, ), -, _, {, }, _, and a few other characters. You cannot use some other characters like quotes, question marks, slashes, square brackets, colon or semicolon, periods, commas, greater-than or less-than (plus a few more), which would get in the way of the shell.
But space is technically allowed in 8.3 filenames. Although in practice it’s not a good idea to use them, because some applications may not support spaces in filenames in the same way.
Let’s try it
Whenever you have a question about something, like can a filename have a space in it, I recommend writing a quick program to test it. So let’s write a test program that creates a new text file with a space in the filename, writes a short message, and saves it. This program does just that, saving the file as A A.TMP. Note that the space must be surrounded by other valid characters. For example, you can name a file as MY FILE.TXT but not MYFILE .TXT where the space is at the end of the base name.
#include <stdio.h>
int main()
{
FILE *f;
f = fopen("A A.TMP", "w");
if (f==NULL) {
puts("error, cannot open file");
return 1;
}
fputs("Hello world\n", f);
fclose(f);
puts("Ok");
return 0;
}
There’s not much going on inside this program, because it’s meant to test a specific thing. The fopen line opens a file called A A.TMP for writing. The if statement after that checks if the file could be opened; if it could not, the program prints an error and immediately quits. Otherwise, the program writes Hello world to the file, then closes it before printing an “Ok” message and returning to the operating system.
If you’ve written this program on FreeDOS, you can compile it with any C compiler. We include several C compilers on the FreeDOS distribution, including Open Watcom C, an IA-16 version of GCC, and BCC. I saved this program as aa.c and compiled it with Open Watcom C. (Open Watcom prints a lot of messages as it goes, but I’ve made it quiet with the -q option. I also prevented the DOS4G extender from printing a message by setting the DOS4G environment variable to quiet.)
D:\SRC>set DOS4G=quiet
D:\SRC>wcl -q aa.c
Just run the program, and you’ll see that it creates a new file called A A.TMP (there’s a space in between the two A‘s).
D:\SRC>dir /b *.tmp
File not found.
D:\SRC>aa.exe
Ok
D:\SRC>dir /b *.tmp
A A.TMP
You can access the file using quotes on the command line. But because the filename has TMP for the extension, you can also use a wildcard for other actions such as deleting the file:
D:\SRC>type "A A.TMP"
Hello world
D:\SRC>del *.tmp
one file removed.
A caution
This is a valid C program and it will compile using any standard C compiler. However, note that some C compilers may alter the behavior because of spaces in the filename. Notably, the runtime library in BCC (Bruce’s C Compiler) prevents spaces in filenames, replacing them with underscore characters. So if you compile this program with BCC and run it, you will get slightly different behavior.
D:\SRC>bcc -o aa.com aa.c
Now run the program and you will instead get a file called A_A.TMP (with an underscore instead of a space).
D:\SRC>aa.com
Ok
D:\SRC>dir /b *.tmp
A_A.TMP
D:\SRC>type A_A.TMP
Hello world