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

13

0

  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?

The Tech Guy

Posted 2016-07-02T18:50:20.940

Reputation: 139

Answers

16

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

DavidPostill

Posted 2016-07-02T18:50:20.940

Reputation: 118 938

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.207

2@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, \\?\ paths aren't UNC paths. The MS spec for UNC paths says that the host-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\\.

– Ben N – 2016-07-03T18:51:42.940

@BenN You are correct. I had only ever seen \\?\ in the context of UNC paths. – Kroltan – 2016-07-03T18:58:01.780

Escaping percents (for instance folder %OS%) in cmd prompt: dir ^%OS^% in batch file dir "%%OS%%" – JosefZ – 2016-07-03T19:09:46.120

@JosefZ, This does not work, please read my answer below. Try set "OS^=test" and then echo ^%OS^% - you will get test 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

11

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.

Jack White

Posted 2016-07-02T18:50:20.940

Reputation: 869

Why do you use keyboard tags for path/command names? It would be both syntactically more correct and more readable if you just used inline code formatting. It would also be easier to type: a backtick instead of <kbd>. – Cody Gray – 2016-07-03T06:55:36.533

done. I thought that <kbd>tags</kbd> had much clearer outline than code blocks. But if such a question arised, I guess they do cause confusion. – Jack White – 2016-07-03T08:33:40.223

2"It is an escape symbol of the command shell" Not correct. The escape character in cmd is ^. % is "a character that has a special meaning for command line parameters and FOR parameters. To treat a percent as a regular character, double it". – DavidPostill – 2016-07-03T13:07:22.810

Indeed, I had the terms mixed up. Now corrected the answer. Thanks. – Jack White – 2016-07-03T18:04:02.980

Also please note, as outlined in my answer, you only double % in for if that for is inside a batch file. Typing for %%i in (*) do echo %%i into a command propmt window produces an error citing inapproporate appearance of %%i. – Jack White – 2016-07-03T18:07:43.437