Toolvito

Works on this device

Windows Command Line Escaper

Type each argument exactly as the program must receive it. The tool adds the quotes and backslashes that Windows programs expect, and the carets that cmd.exe needs. It writes text only and runs nothing.

Your arguments

Quotes each argument so that the program receives exactly what you typed.

Options

Arguments, one in each row

Type each argument exactly as the program must receive it, with no quotes around it. An empty row is an empty argument.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Enter the arguments, or choose Try an example.

How to use this tool

  1. Enter one argument in each row, with no quotes around it. An empty row is an empty argument.
  2. Choose where the command line is used: a program started directly, the command prompt, or a batch file.
  3. Copy the result and put it after the program name.
  4. Check the table What the program receives. It must show your arguments exactly as you typed them.
  5. To see how an existing command line is split, choose Read a command line into its arguments in the menu.

Two layers, two sets of rules

A Windows program gets its command line as one piece of text and splits it by itself. Most programs use the rule of the Microsoft C runtime. When you type the line at the command prompt or in a batch file, cmd.exe reads it first, by other rules. So there are two layers, and each one needs its own escaping.

What each layer does
Layer Rule Example
1. Quoting for the program An argument with a space, a tab or a double quote is put inside double quotes. A double quote is written as a backslash and a quote. Backslashes are doubled only before a double quote or at the end of a quoted argument. say "hi" becomes "say \"hi\""
2. Carets for cmd.exe A caret is put before each of ( ) % ! ^ " < > & |, also before the quotes of layer 1. a&b becomes a^&b
2. In a batch file The same, but each percent sign is written twice and gets no caret. 50% becomes 50%%

Layer 1 follows the ArgvQuote method that Microsoft published in 2011. Layer 2 puts a caret also before every double quote, so cmd.exe never sees a quoted part and treats the whole line in one way.

How a program reads the line

  • Spaces and tabs outside quotes separate the arguments.
  • A double quote starts or ends a quoted part. The quote itself is left out.
  • A backslash is a normal character, unless backslashes stand before a double quote. Then each pair of backslashes gives one backslash. If one backslash is left over, the quote is a normal character; if none is left over, the quote starts or ends a quoted part.
  • Inside a quoted part, two double quotes in a row give one double quote. Here the C runtime and the Windows function CommandLineToArgvW differ a little, so this tool never writes quotes in that way.
  • The program name at the start of the line has a simpler rule: backslashes are never special in it.

Worked example

The four arguments C:\My Files\report 1.txt, say "hi", folder with space\ and 50%&done are written for a program that is started directly as "C:\My Files\report 1.txt" "say \"hi\"" "folder with space\\" 50%&done. The backslash at the end of the third argument is doubled, because the closing quote follows it. The backslashes in the first argument stay single.

For the command prompt, the three arguments a&b, 50% and say "hi" become a^&b 50^% ^"say \^"hi\^"^". In a batch file the middle one is 50%%.

Limits you should know

  • A program can read its command line in its own way. Most programs use the C runtime rule, but some do not, for example some programs that came from Unix. Check the manual of such a program.
  • If the program you start is a batch file, cmd.exe reads the arguments again and this result is not enough.
  • If delayed expansion is on in cmd.exe, an exclamation mark has a special meaning and this result is not right. The tool tells you when an argument has one.
  • At the command prompt a percent sign cannot be fully protected. The caret form works unless a variable exists whose name fits between two of the percent signs. In a batch file, the doubled percent sign always works.
  • When a whole line is given to cmd /c and it starts with a double quote, cmd.exe may remove the first and the last quote. Use cmd /s /c and put the whole line inside one more pair of quotes.
  • cmd.exe takes at most 8,191 characters in one line. Windows takes at most 32,766 characters for the command line of a new program.
  • An argument cannot have a line break here. PowerShell has other rules; use the Powershell String Escaper.
  • This tool cannot test your command. Try the result with harmless values first, above all before a command that deletes or changes files.

Related tools