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

  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?


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

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • syntax - Escape Characters, Delimiters and Quotes.

DavidPostill's answer is great, but may confuse newcomers a little. I'll try to rephrase.

% is not a reserved character.

It is an character with special meaning to the command shell (aka cmd.exe and command.com)

The difference is:

  • you cannot use reserved characters at all (but see below)
  • you may be able to use special and escape characters - just not by typing them as-is

In other words, you can create a file or folder containing % in their name. You can do it directly using windows explorer because % has no special meaning there. There was no intention to prevent you from creating such files at command prompt either, but because % has a special meaning there, you need to use syntax outlined below to create such files.

BAT-files

If you are writing a batch file (*.bat, *.cmd), which get executed by command shell (aka the command prompt application), to use % literally (such as to create a file with % in the name, and not to substitute a variable or parameter) you need to type %% instead.

For example,

  • command echo %windir% produces output: c:\windows
  • however, command echo %%windir%% produces output: %windir%
  • and so on: command echo %%%%windir%%%% produces output: %%windir%%

So if you save following line as test.bat and run it, it will create a directory named %test% complete with percent signs:

md %%test%%

If there is no variable named test, this next command is equivalent to the last one - it also creates a directory %test%:

md %test%

...but please never do it like that since your command will not behave the way you wanted once someone creates a variable with that name

If you intend to use command for in a BAT-file, you also need to double % so that it would have special meaning to for command instead of for the command shell itself:

for %%i in (*.*) do echo %%i

This will produce a list of files in current directory (that is, %%i has special meaning), and I'm not sure how to make it produce literal %%i without resorting to %p% workaround described in the next section (%%%%i does not work here).

% is not the only character with special meaning at command prompt. Others include ^, <, >, |, (, ), &, !, ". Most of those (excluding ") can be escaped - that is, prefixed with an escape-character ^ so that a literal character is inserted, suppressing its special meaning. Those also lose their special function inside doublequoted string, and " is escaped with a backslash: \". Some carets (^) may need to be doubled inside doublequote-enclosed string and where delayed variable substitution takes place (! special meaning).

Command prompt

Unfortunately, when typing commands into command prompt window directly, doubling % character just produces %% i.e. the above approach does not work in that case.

There appears to be a workaround exploiting a quirk in command processing - you can get literal % by typing ^ after %. That approach is not foolproof though: if your filename is enclosed in double quotes, ^ is interpreted literally (and not removed like without quotes)

  • command echo %windir% produces output: c:\windows
  • command echo %^windir% produces output: %windir%
  • command echo "%^windir%" produces output: "%^windir%" (with extra ^ - not what we wanted)

I recommend another workaround instead:

  • create a variable like this: set "p=%"
  • use %p% whereever you need a literal percent sign: echo "%p%windir%p%" will now produce output: "%windir%"

The same workaround can be used to get literal percent sign in for command, however note that unlike BAT-files when keying the for command directly you do not double percent signs.

Filesystem reserved characters

You cannot normally create a file or directory containing following reserved characters in its name: / ? < > \ : * | ", NULL-symbol and characters with codes 1 to 31; also . and space cannot be last characters; and following names are illegal: com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9 con nul prn.

However, some of these restrictions can be bypassed prefixing file path with \\?\ like this: \\?\c:\test...\nul. At least files with reserved names and those ending with space and dot can be manipulated that way.

What's more, NTFS filesystem itself supports several naming subsystems: DOS, Windows and POSIX. It is the Windows subsystem that has all the restrictions above, while POSIX only forbids / and NULL-symbol.

GNU/Linux OS (a superset of POSIX) can thus create (and delete) file/directory names that most normal windows API functions (and therefore windows programs) cannot work with. On Windows XP working with them was possible by installing free "Services For Unix subsystem" (SFU); on Vista and above third-party tools are required for that. I'm not sure if new ubuntu-on-windows10 subsystem can do it.