Windows Programming/Programming CMD

from http://en.wikibooks.org/wiki/Windows_Programming/Programming_CMD

In Windows NT (2000, XP, Vista, 7, etc.) one is able to write batch files that are interpreted by the Command Prompt (cmd.exe). They can be used to automate file-system tasks such as backups or basic installations and can be used with other command-line utilities as well. The batch files can be considered to be a simple scripting language with logic and jumps. The advantages of using batch files are the ease of writing them, the ability to edit the files without compiling anything, their cross-compatibility across Windows NT Operating Systems and their inherent ability to manipulate file systems due to their basis on MS-DOS. Batch file scripts are not case-sensitive, although strings and data is. The file is controlled by a list of commands separated into lines which are run like normal commands would be at the Command Prompt, although some functionality is different. Batch files can be run from Windows Explorer but the console used to display them closes automatically at the end of the batch file, so a command at the end that prevents instantaneous exit is needed for any remaining output to be read before the console window closes. Although the batch files are able to manipulate their environment, such as color settings and environment variables, the changes are all temporary, as in a standard Command Prompt session. Color settings, however, are retained on later editions of Windows NT. In order to try to learn about batch files, it is useful to understand Command Prompt commands. See: Guide to Windows commands.

[--106.78.244.102 (discuss) 08:14, 28 March 2014 (UTC)[[1][2]]]

Batch File[edit]

The script is kept inside a batch file, with the extension .bat or .cmd. Although .bat is more recognisable, as it was used in the MS-DOS environment that preceded the Command Prompt, the Command Prompt's interpretations of batch files is very different to the manner of interpreting DOS batch files, and .cmd files are only interpreted by the Command Prompt, so using the .cmd extension prevents mis-use in older environments.

Execution starts at the top of the file and ends at the end. When the end of the file is reached, the file exits to the Command Prompt if it was invoked from there, or the console window closes if it was invoked from Windows Explorer or the START command.

ECHO Command[edit]

Typically, batch files start with the 'echo off' command, which stops the input and prompt from being displayed during execution, so only the command output is displayed. The '@' symbol prevents a command from having input and its prompt displayed, so it is used on the 'echo off' command to prevent that first command from displaying the input and prompt:

@ECHO OFF

In order to print lines, the ECHO command is used again, but this time with a text parameter other than 'off':

ECHO.Hello World!

The period ('.') is used to prevent confusion with the attempt to output ON or OFF rather than turn input and prompt displaying on and off. The last line in a code should then be:

ECHO ON

'Echo on' will turn the input/prompt display back on, just in case the program exits to the Command Prompt where without the command 'echo on', there will be no visible prompt left for use.

Hello World Example[edit]

Using the code above, we can make a hello world program like so:

@ECHO OFF
ECHO.Hello World!
ECHO ON

Comments[edit]

In batch files there are two ways of writing comments. Firstly there is the form:

REM Comment here.

This form is included as it was in the MS-DOS batch file script. The other form is this:

::Comment here.

This form is generally favoured, for being faster to execute and write, and also for being easy to differentiate from normal commands. For this type of comment only two double-colons ('::') are needed and the comment ends at the end of the line. Batch files have no multi-line comment types.

You can also add a comment to the end of the command:

command &::Comment here.

Variables[edit]

We access variables with the SET command. SET lets us set the value for a variable, and delete a variable. The variables used are environment variables, which are set up to correspond to the system's environment variables, although they are not the actual environment variables of the system and changing them will not change system environment variables. For example:

SET name=John Smith

This command creates an environment variable called name, and sets its value to the string "John Smith". The first space is ignored as the value entered records from the first non-whitespace character encountered after the '=' sign.

'Set' also allows the storing of integers specifically, using the /A parameter:

SET /A number=38

This command creates an environment variable named number with the integer value 38, not the string value "38". This number can be involved in arithmetic and numerical logic, but without the /A parameter all that is stored are the characters '3' and '8', which can't be used as numbers.

Variables are accessed by surrounding the name with '%'s. This substitutes the value straight into a command, and does not point to the variable. For example:

@ECHO OFF

SET name=John Smith
ECHO %name%

ECHO ON

This file would return the text 'John Smith'. A string number can be converted to an integer number in a similar fashion:

@ECHO OFF

SET num=38
SET /A num=%num%

ECHO ON

This code creates a string called num, then overwrites it with an integer with the numerical value represented by the string contents of num, in this case 38. 'num' is now an integer number ready for use as a number.

Input[edit]

The set command can also be used for input:

SET /P var=Enter a value for var:

This command displays "Enter a value for var:" and when the user enters the data, var is given that value.

Be aware, if the user presses enter without entering anything then the value in var is unchanged, so for the sake of a prompt it is often best to give a default value, or clear the value for the variable first if it has been used before:

SET var=
SET /P var=Enter a value for var:

Below is an example:

@ECHO OFF
SET /P answer = Enter name of file to delete: 
DEL /P %answer%
ECHO ON

This batch file gets the name of a file to delete and then uses the DEL command with the prompt parameter '/P' to ask the user if they're sure they want to delete the file.

Flow Control[edit]

Conditionals[edit]

IF[edit]

The IF command can be used to create program logic in batch files. The IF command allows three basic checks, on the ERRORLEVEL, the equality of two strings, and the existence of a file or folder. The first check on the ERRORLEVEL will check to see if it is greater than or equal to a certain number:

IF ERRORLEVEL 5 ECHO.The ERRORLEVEL is at least 5.

For this style the first parameter is always ERRORLEVEL, and the second is the value it checks against. In this case, if the ERRORLEVEL is at least 5 then the command at the end of the line is executed, outputting the message "The ERRORLEVEL is at least 5.". The second form is a check between two strings:

IF "%str1%"=="Hello." ECHO.The strings are equal.

Here the first parameter is two strings either side of the double '=', symbolising a check to see if they are equal. If the variable str1 is exactly equal to "Hello.", a check which is case-sensitive, then "The strings are equal." is outputted. In the case that you wish to make the check case-insensitive you would rewrite it as following:

IF /I "%str1%"=="Hello." ECHO.The strings are equal.

Now, for example, str1 could contain "HELLO." but the check would still result in the command being executed at the end as the check is now case-insensitive. The final basic IF type is the existence check, to see if a file or folder exists.

IF EXIST myfile.txt TYPE myfile.txt

Here if the file "myfile.txt" exists in the current folder then the command TYPE myfile.txt is executed which displays the contents of "myfile.txt" in the console window.

All of the preceding examples have an optional NOT parameter that can be written after the IF which will execute the command at the end of the line if the condition is not true. For example:

IF NOT EXIST myfile.txt ECHO.File missing.

Which will output "File missing." if the file "myfile.txt" is not existent in the current folder. There are a few other IF types with command extensions, which can be seen with the IF /? command at the command prompt.

ELSE[edit]

The ELSE operator can be used with a combination of brackets to provide multi-line logical statements that provide an alternative set of commands if the condition is not true.

IF condition (
   commands to be executed if the condition is true 
) ELSE (
   commands to be executed if the condition is false
)

Unlike some languages, in batch files the scripting requires that the lines IF condition () ELSE ( and ) are written very specifically like that. It is possible, however, to re-write it to use single-line outcomes all on one line:

IF condition (command if true) ELSE command if false

Below is an example of the ELSE operator in use:

@ECHO OFF
::Prompt for input.
SET /P answer=Enter filename to delete: 
IF EXIST %answer% (
 DEL /P %answer%
) ELSE (
 ECHO.ERROR: %answer% can not be found in this folder!
)
ECHO ON

This batch file will delete a file, unless it doesn't exist in which case it will tell you with the message "ERROR: %answer% can not be found in this folder!".

Unlike in most computer languages, multiple multi-line IF...ELSE style statements can't be nested in batch files.

Jumps[edit]

You can control program flow using the GOTO statement. Batch files don't have all elements for structured programming scripting, however some elements of structured programming such as functions can be simulated. The simplest way of controlling program flow, however, is the GOTO statement which jumps to a specified label.

GOTO labelnam

This code will direct program flow to the label labelnam, which is found at the first occurance of this line:

:labelnam

It is important to remember that labels only store 8 characters, so if a label is longer than 8 characters only the first 8 will be seen. This means the labels labelname1 and labelname2 can't be distinguished from each other as their only difference occurs past the first 8 characters. Although not strictly incorrect, it is better to avoid using label names longer than 8 characters to avoid these distinguishing problems easily.

Here is the example from earlier redesigned to loop until asked to stop:

@ECHO OFF

:prompt
::Clear the value of answer ready for use.
SET answer=
SET /P answer=Enter filename to delete (q to quit): 

IF EXIST %answer% (
 DEL /P %answer%
 GOTO prompt
)
IF /I "%answer%"=="q" GOTO :EOF

::By this point an error must have occurred as all
::the correct entries have already been dealt with.
ECHO.ERROR: Incorrect entry!
GOTO prompt

ECHO ON

Take note of the command GOTO :EOF. This command will take the script to the end of the file and end the current batch script.

FOR Looping[edit]

Runs a specified command for each file in a set of files.

   FOR %variable IN (set) DO command [command-parameters]
 %variable  Specifies a single letter replaceable parameter.
 (set)      Specifies a set of one or more files.  Wildcards may be used.
 command    Specifies the command to carry out for each file.
 command-parameters
            Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I.

Batch File Example:

 for %%F IN (*.txt) DO @echo %%F

This command will list all the files ending in .txt in the current directory.

If Command Extensions are enabled, the following additional forms of the FOR command are supported:

   FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory names instead of file names.

   FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree.

   FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers from start to end, by step amount. So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would generate the sequence (5 4 3 2 1)

   FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
   FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
   FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

or, if usebackq option present:

   FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
   FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
   FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

filenameset is one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens. The body of the for loop is then called with the variable value(s) set to the found token string(s). By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. You can override the default parsing behavior by specifying the optional "options" parameter. This is a quoted string which contains one or more keywords to specify different parsing options. The keywords are:

   eol=c           - specifies an end of line comment character
                     (just one)
   skip=n          - specifies the number of lines to skip at the
                     beginning of the file.
   delims=xxx      - specifies a delimiter set.  This replaces the
                     default delimiter set of space and tab.
   tokens=x,y,m-n  - specifies which tokens from each line are to
                     be passed to the for body for each iteration.
                     This will cause additional variable names to
                     be allocated.  The m-n form is a range,
                     specifying the mth through the nth tokens.  If
                     the last character in the tokens= string is an
                     asterisk, then an additional variable is
                     allocated and receives the remaining text on
                     the line after the last token parsed.
   usebackq        - specifies that the new semantics are in force,
                     where a back quoted string is executed as a
                     command and a single quoted string is a
                     literal string command and allows the use of
                     double quotes to quote file names in
                     filenameset.

Some examples might help:

   FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.

%i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'. Remember, FOR variables are single-letter, case sensitive, global, and you can't have more than 52 total active at any one time.

You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string, using single quote characters. It will be treated as a single line of input from a file and parsed.

Finally, you can use the FOR /F command to parse the output of a command. You do this by making the filenameset between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file. So the following example:

   FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

would enumerate the environment variable names in the current environment.

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

   %~I         - expands %I removing any surrounding quotes (")
   %~fI        - expands %I to a fully qualified path name
   %~dI        - expands %I to a drive letter only
   %~pI        - expands %I to a path only
   %~nI        - expands %I to a file name only
   %~xI        - expands %I to a file extension only
   %~sI        - expanded path contains short names only
   %~aI        - expands %I to file attributes of file
   %~tI        - expands %I to date/time of file
   %~zI        - expands %I to size of file
   %~$PATH:I   - searches the directories listed in the PATH
                 environment variable and expands %I to the
                 fully qualified name of the first one found.
                 If the environment variable name is not
                 defined or the file is not found by the
                 search, then this modifier expands to the
                 empty string

The modifiers can be combined to get compound results:

   %~dpI       - expands %I to a drive letter and path only
   %~nxI       - expands %I to a file name and extension only
   %~fsI       - expands %I to a full path name with short names only
   %~dp$PATH:I - searches the directories listed in the PATH
                 environment variable for %I and expands to the
                 drive letter and path of the first one found.
   %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

Pipes[edit]

this is mainly used to redirect the output of one program to another program

a | b

means execute "a" and what all output "a" gives to the console - give that as "b" s input

dir | find ".htm"

will give a list of file which contain ".htm" in their names

The output of a command as well as possible errors could be redirected also to files (Note the 2 in front of the >> ):

ACommand >>TheOutputOfTheCommandLogFile.log 2>>TheErrorOutputOfTheCommandFile.log

Functions[edit]

Functions are denoted by prepending a colon to their name. Functions are defined after the main script at the end of the file. For the handling of parameters and return values use the following structure.

CALL :functionname %param1% %param2% ...
ECHO %result% 

:functionname
SETLOCAL
commands using parameters %1, %2, ... and setting %retval%
...
ENDLOCAL & SET result=%retval% 


Bat file with next content outputs "42" as result of execution

:: describes and calls function for multiplication with 2 arguments
@ECHO OFF
CALL :multiply 21 2 
ECHO %result% 

:multiply
SETLOCAL
set retval=0
set left=%1
set right=%2
:: use '/A' for arithmetic
set /A "retval=left*right" 
ENDLOCAL & SET result=%retval% 

Command-Line Interfacing[edit]

let's say we want to call a program "MyProgram" from the command prompt. we type the following into our prompt (the .exe file extension is unnecessary):

C:\>myprogram.exe

And this will run the myprogram executable. Now, let's say we want to pass a few arguments to this program:

C:\>myprogram arg1 arg2 arg3

Now, if we go into the standard main function, we will have our argc and argv values:

int main(int argc, char *argv[])

Where:

argc = 4
argv[0] = "myprogram" (the name of the program - deduct 1 from argc to get the number of arguments)
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"

This shouldn't come as a big surprise to people who have any familiarity with standard C programming. However, if we translate this to our WinMain function, we get a different value:

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int iCmdShow)

we will only have one value to accept the command line:

CmdLine = "myprogram arg1 arg2 arg3".

We can also use the function GetCommandLine to retrieve this string from any point in the application. If we want to parse this into a standard argv/argc pair, we can use the functionCommandLineToArgvW to perform the conversion. It is important to note that CommandLineToArgvW only works on unicode strings.

When we return a value from our C program, that value gets passed to the CMD shell, and stored in a variable called "ERRORLEVEL". ERRORLEVEL is the only global variable that is not a string, and it can contain a number from 0 to 255. By convention, a value of zero means "success", while a value other then zero signifies an error.

Let's say we wanted to write a C program that returns the number of arguments passed to it. This might sound like a simple task in C, but it is difficult to accomplish in batch script:

int main(int argc, char *argv[])
{
   return (argc - 1);
}

And we will name this program "CountArgs.exe". Now, we can put this into a batch script, to pass it a number of arguments, and to print out the number passed:

countargs.exe %*
ECHO %ERRORLEVEL%

We can, in turn, call this script "count.bat", and run that from a command prompt:

C:\>count.bat arg1 arg2 arg3

and running this will return the answer: 3.

NOTE: Actually, this can be accomplished with a batch file without resorting to a C program, by simply using CMD delayed variable expansion via the "/V:ON" parameter:

/V:ON   Enable delayed environment variable expansion using ! as the 
delimiter. For example, /V:ON would allow !var! to expand the variable 
var at execution time.  The var syntax expands variables at input time, 
which is quite a different thing when inside of a FOR loop.

Then use a simple batch file like the following to count parameters:

set COUNT=0
for %%x in (%*) do ( set /A COUNT=!COUNT!+1 )
echo %COUNT%

Or an easier way, without having to enable & use 'delayed environment expansion', would be to do the following:

set COUNT=0
for %%x in (%*) do set /A COUNT+=1
echo COUNT = %COUNT%


This returns the same answer as the C program example.

Console Control Handlers[edit]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值