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.
1Note: not all of the reserved characters apply to the NTFS filesystem. Using Linux, for example, I can easily create a file named
<test>
. It simply can't be done using Windows' file management functions. – Nathan Osman – 2016-07-02T22:36:02.2072@NathanOsman You may be able to create files with those names from within Windows using raw absolute path notation (
\\?\C:\USERS\DEFAULT\<test>
). – zwol – 2016-07-02T22:41:40.517@zwol oh? I didn't know that. That's good to know. – Nathan Osman – 2016-07-02T22:44:14.970
@zwol Not from a
cmd
shell ...F:\test>md \\?\f:\test\<test>
The syntax of the command is incorrect. – DavidPostill – 2016-07-02T22:50:03.937@DavidPostill What about
md \\^?\f:\test\^<test^>
? – zwol – 2016-07-02T22:51:31.750@zwol "The filename, directory name, or volume label syntax is incorrect." – DavidPostill – 2016-07-02T22:52:18.687
@DavidPostill Harumph. Never mind. – zwol – 2016-07-02T23:01:05.280
@Kroltan Technically,
– Ben N – 2016-07-03T18:51:42.940\\?\
paths aren't UNC paths. The MS spec for UNC paths says that thehost-name
part has to be a valid DNS or NetBIOS hostname, and?
isn't. To make an absolute literal path refer to a UNC path, start it with\\?\UNC\\
.@BenN You are correct. I had only ever seen
\\?\
in the context of UNC paths. – Kroltan – 2016-07-03T18:58:01.780Escaping percents (for instance folder
%OS%
) incmd
prompt:dir ^%OS^%
in batch filedir "%%OS%%"
– JosefZ – 2016-07-03T19:09:46.120@JosefZ, This does not work, please read my answer below. Try
set "OS^=test"
and thenecho ^%OS^%
- you will gettest
printed because^
does not actually escape%
– Jack White – 2016-07-03T20:16:51.540@zwol You do not need to escape
?
character in\\?\
with^
. Also it only allows to override some restrictions, not all of them. If you're interested, I've posted another answer to this question with more detailed explaination. – Jack White – 2016-07-03T20:34:09.997