Windows – In Windows Explorer, why can we create a folder or file with the percent (%) symbol, if the percent symbol is used for existing variables

command lineenvironment-variablesms-doswindowswindows-explorer

  1. Go to Windows Explorer, and create a folder/directory named %systemdrive%, %windir%, or any other existing variable.
  2. Open a command prompt, and go to the folder you just created.

You can't, because there are percent (%) symbols in the folder name, confusing the command prompt, because the percent symbol is used for existing variables.

Escaping the percent (%) symbol with two percent symbols (%%) does not work, so DavidPostill's answer is wrong:

Command prompt window, with a bit of the Explorer desktop showing
So, in Windows Explorer, why can we create a folder or file with the percent (%) symbol, if the percent symbol is used for existing variables?

Best Answer

Why can we create a folder/file with the percent symbol?

The % character is not a reserved character in a file name.

The reserved characters for naming files, paths and namespaces are:

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

Source Naming Files, Paths, and Namespaces


The percent symbol is reserved for variables

Now, open a command prompt and try to go to the folder you created.

You can't, because there are percent symbols in the folder name, indicating that it is a variable.

The above is not true. Everything works just as you would expect.

Example using %test%:

F:\test>echo %test%
%test%

F:\test>md %test%

F:\test>cd %test%

F:\test\%test%>

Example using %systemdrive%:

F:\test>echo %systemdrive%
C:

F:\test>md %systemdrive%
A subdirectory or file C: already exists.

F:\test>cd %systemdrive%
C:\Users\DavidPostill

F:\test>c:

C:\Users\DavidPostill>f:

F:\test>

F:\test>dir %systemdrive%
 Volume in drive C has no label.
 Volume Serial Number is C8D0-DF1E

 Directory of C:\Users\DavidPostill

03/06/2016  16:16    <DIR>          .
03/06/2016  16:16    <DIR>          ..
18/07/2015  19:25    <DIR>          .atom
03/06/2016  16:16    <DIR>          .oracle_jre_usage
08/05/2015  20:29    <DIR>          Contacts
03/06/2016  16:14    <DIR>          Desktop
01/06/2016  09:04    <DIR>          Documents
02/05/2016  12:55    <DIR>          Downloads
09/01/2015  11:51    <DIR>          dwhelper
08/05/2015  20:29    <DIR>          Favorites
20/02/2016  22:00    <DIR>          Jaikoz
08/05/2015  20:29    <DIR>          Links
17/03/2015  06:19    <DIR>          Music
29/03/2016  19:01    <DIR>          Pictures
08/05/2015  20:29    <DIR>          Saved Games
23/06/2016  10:55    <DIR>          Searches
02/05/2016  12:36    <DIR>          SecurityScans
11/04/2016  12:14               994 Start Menu - Shortcut.lnk
31/05/2016  00:52    <DIR>          temp
17/03/2015  06:19    <DIR>          Videos
               1 File(s)            994 bytes
              19 Dir(s)  69,716,357,120 bytes free

Escaping Percents

The % character has a special meaning for command line parameters and FOR parameters.

To treat a percent as a regular character, double it:

%%

Source syntax


Further Reading