File names that include a space require special handling. Specifically, the name or path needs to be surrounded by quotation marks when they’re referred to.
For that reason I have an aversion to using space characters in my file names. Whenever I download a file, I find myself replacing them with underscore characters. It’s easy to do in Altap Salamander
1. Open the Rename tool with Ctrl-Shift-R, put a space in the Search box, an underscore in the Replace box, press Alt-Y to uncheck “Only Once” and finally Alt-R to perform the action.
Since I’m learning Python, I thought it would be fun to write a script to do this. And while I’m at it, I should create the script so it also replaces any illegal characters in a string.
Thus I went down the rabbit hole of searching for a list of illegal characters.
According to Microsoft
2, there are these:
< > : " / \ | ? *. But I realized that I didn’t want a list of illegal characters; I wanted a list of characters whose use is a bad idea – inconvenient characters. To me, they are
( ) + & , ^ ! %.
So I looked at a popular thread on Stackoverflow
3, where, to my chagrin, a Python script similar to mine
4 was waiting for me. I enjoyed reading the answers, the comments, the opinions. But not once did anyone mention an inconvenient character except for space. And so I revved up my “expertise engine” and looked for the post editor in which to write my response.
Alas, I did not have enough “reputation points” to answer the question. And anyway, I thought the whitelist idea was the best.
But in case you’re curious, here are explanation of why these
characters are inconvenient:
Parenthesis: They confuse the DOS FOR command. Create
my(file).txt. Then enter the following at the cmd prompt:
5
for /F %s in (my(file).txt) do echo %s
I’m using Windows 10 and verifying these examples with it. On older Windows (7, XP?) parenthesis caused a problem even if the file name is placed after the do, like this:
for %i in (2 4 6 8) do copy my(file).txt my(file)%i.txt
Plus sign: The DOS copy command can be used to concatenate two or more files into one. What’s the concatenation operator? You guessed it – it’s the plus sign. How would you concatenate
my+1.txt with
my+2.txt? This doesn’t work:
copy my+1.txt+my+2.txt ex2.bat
You can do this instead, which is just as bad as using spaces:
copy "my+1.txt"+"my+2.txt" ex2.bat
Ampersand: Two statements can be placed on one line when they’re separated by an ampersand. If you wanted to copy one file to another and then print the contents to the screen you could enter:
copy ch2.txt ch42.txt & type ch42.txt
But if instead of ch2.txt you had ch2&3.txt, it gets tricky. For example, enter:
copy ch2&3.txt ch42.txt & type ch42.txt
It gives up rather quickly, saying that ch2 and ch42.txt could not be found and that 3.txt is not a command. That’s because DOS interprets it as three separate commands, not two:
copy ch2
&3.txt ch42.txt
type ch42.txt
Comma: My only concern about commas is that I work with CSV data files frequently. If a file name needed to be included in such a file, the comma could be interpreted as a delimiter and screw up the layout.
Percent and Exclamation mark: These are used in batch files to reference variables or command line parameters. Consider what special handling you’ll need for a file named my%100.txt:
set x=2
copy my%100.txt my%100%x%.txt
The %1 is replaced by the first command line parameter, which is not what you want. When this is run with no parameters, the batch file tries to copy my00.txt to my002.txt.
When delayed environment variable expansion is enabled, the second statement could be written as shown below, which can be inconvenient when the file name includes an exclamation mark:
copy my%100.txt my%100!x!.txt
If the file is named my!100.txt, you can make this example work by escaping the ! that’s part of the file name. Use ^ as the escape character, but you’ll have to double it:
copy my^^%100.txt my^^%100%x%.txt
Unfortunately, there is no way (that I know of in DOS) to escape the percent character in %1.
And that brings us to caret. If you use it in a file name such as my^100.txt, you’ll need to escape it, as well.
copy my^^100.txt my^^100%x%.txt
Command prompt help lists even more characters that require quotes if they’re used in filenames. You can find this at the end of the output from cmd /h:
The completion code deals correctly with file names that contain spaces or other special characters by placing quotes around the matching path…. The special characters that require quotes are:
<space>
&()[]{}^=;!'+,`~