cmd.rst
author Oleksandr Gavenko <gavenkoa@gmail.com>
Mon, 22 Feb 2016 12:41:52 +0200
changeset 1903 901e7394849f
parent 1334 9bf0d5a1f0cf
child 1905 fba288d59662
permissions -rw-r--r--
Decrease intent to increase space usage on mobile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
950
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
.. -*- coding: utf-8; -*-
1334
9bf0d5a1f0cf Include common header with quick links.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1075
diff changeset
     2
.. include:: HEADER.rst
950
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
====================
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
 CMD Windows shell.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
====================
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
.. contents::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
Quoting.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
========
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
 * Arguments are delimited by white space, which is either a space or a tab.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
 * A string surrounded by double quotation marks is interpreted as a single
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
   argument.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
 * A double quotation mark preceded by a backslash, \", is interpreted as a
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
   literal double quotation mark.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
 * Backslashes are interpreted literally, unless they immediately precede a
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
   double quotation mark.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
 * If an even number of backslashes is followed by a double quotation mark,
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
   then one backslash (\) is placed in the argv array for every pair of
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
   backslashes (\\), and the double quotation mark (") is interpreted as a
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
   string delimiter.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
 * If an odd number of backslashes is followed by a double quotation mark,
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
   then one backslash (\) is placed in the argv array for every pair of
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
   backslashes (\\) and the double quotation mark is interpreted as an escape
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
   sequence by the remaining backslash, causing a literal double quotation
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
   mark (") to be placed in argv.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
 * In double quote mark need surround such chars::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
     & < > [ ] { } ^ = ; ! ' + , ` ~ %
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
   Also all this char can be escaped by ^ char.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
 * Long line can be truncated by ^ char, in this case trailing white spaces
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
   not allowed.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
 * To quote percent sign % before alpha char in batch file double it
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
   occurrences or plase in quotes::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
     prog '%'HOME'%' "%"HOME"%" %%HOME%
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
  http://msdn.microsoft.com/en-us/library/ms880421.aspx
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
                Parsing C Command-Line Arguments
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
Variables.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
==========
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
Variable name start with letter and underscore, next chars can be letter,
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
number and underscore. Variable name is case insensitive.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
List of variables.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
------------------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
  cmd> set
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
  ...
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
  VAR=VALUE
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
Getting.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
--------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
Write %VAR% in place where you want insert variable VAr value.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
Setting.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
--------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
  cmd> set /p VAR=VALUE
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
Deleting.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
---------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
  cmd> set VAR=
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
Input from user.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
----------------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
  cmd> set /p VAR=PROMPT
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
VAR is variable name, PROMPT is displayed prompt.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
Input from file.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
----------------
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
  cmd> set /p VAR=<FILE
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
VAR is variable name, FILE is file name. Sfter executing VAR contain first
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
line from FILE.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
CMD tricks.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
===========
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
  $ set /p TOOLOUTPUT= < temp.txt
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
  $ for /f "tokens=*" %%i in ('%~dp0sometool.exe') do set TOOLOUTPUT=%%i
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
  $ for /f "tokens=1 delims=" %%s in (users.txt) do (echo %%S & command "%%S") >> outputfile.txt
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
1075
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   100
Resize cmd window.
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   101
==================
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   102
::
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   103
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   104
  cmd# mode CON: COLS=120 LINES=40
9462290b5498 Resize cmd window.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 950
diff changeset
   105
950
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
Limits.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
=======
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
Variable value and one line command string after expansion can not exceed 8191
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
characters for Windows XP and later and 2047 for Windows NT, Windows 2000.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
  http://support.microsoft.com/default.aspx?scid=kb;en-us;830473
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
                Command prompt (Cmd. exe) command-line string limitation
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
How run cmd on 64-bit OS.
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
=========================
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
From 64-bit process::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
  %windir%\System32\cmd.exe (for 64-bit)
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
  %windir%\SysWOW64\cmd.exe (for 32-bit)
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   123
From 32-bit process::
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
  %windir%\System32\cmd.exe (for 32-bit)
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
  %windir%\Sysnative\cmd.exe (for 64-bit)
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
  http://msdn.microsoft.com/en-us/library/aa384187%28VS.85%29.aspx
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
                File System Redirector
06221010c81d Fix RST syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130