Скрипты в командной строке windows

This book describes and shows how to use the Microsoft-supplied command interpreter cmd.exe and the associated commands, and how to write Windows batch scripts for the interpreter. cmd.exe is the default interpreter on all Windows NT-based operating systems, including Windows XP, Windows 7 and Windows 10.

Introduction[edit | edit source]

This book addresses 32-bit Windows commands applicable to modern versions of Windows based on the Windows NT environment. It does not address commands that are specific to DOS environments and to DOS-based operating systems, such as Windows 95, Windows 98, and Windows Me, whose Microsoft-supplied command interpreters are in fact DOS programs, not Win32 programs.

You can find out which version of Windows you are running using the VER command.

This book first describes using the Windows NT command interpreter, how it receives, parses, and processes commands from users. Then it describes various commands available.

To obtain an extensive list of Windows commands and their short summaries, open the command prompt on any Windows computer, and type help. To find out about a particular command, type the name of the command followed by «/?».

The subject of this book is also known as «batch programming», even though «batch» refers not only to batch files for MS DOS and Windows command interpreter. Other subject terms include «batch file programming», «batch file scripting», «Windows batch command», «Windows batch file», «Windows command line», «Windows command prompt», and «Windows shell scripting».

For Windows scripting, cmd.exe is a legacy technology; a modern equivalent is PowerShell, which is based on .NET and whose streams for pipelines are objects, not character (byte) streams. PowerShell capabilities vastly outstrip those of cmd.exe; PowerShell has the capabilities of a full programming language, including typed content of variables, floating-point arithmetic, big integers, GUI programming via .NET classes, etc. Nonetheless, cmd.exe is still useful for simple scripting and command-line interaction tasks, often offering shorter syntax and quicker startup time, and working with the character stream pipelining familiar from other operating systems. cmd.exe can be enhanced by commands known from other operating systems; see #Unix commands. Another alternative for scripting Windows is the popular Python with its pywin32 and wmi libraries; however, that requires installation.

Using the Windows command interpreter[edit | edit source]

How a command line is interpreted[edit | edit source]

The parsing of a command line into a sequence of commands is complex. There are four main components:

  1. Variable substitution: A command line is scanned for variable specifications, and any found are replaced with the contents of those variables.
  2. Quoting: Special characters can be quoted, to remove their special meanings.
  3. Syntax: Command lines are developed into a sequence of commands according to a syntax.
  4. Redirection: Redirection specifications are applied, and removed from the command line, before an individual command in a sequence is executed.

Variable substitution[edit | edit source]

Command lines can contain variable specifications. These comprise a % character followed by a name, followed by a second % character unless the name is a digit in 0 … 9 or an asterisk *.

Variable specifications are replaced with values as follows:

  • %varname%, such as %PATH% or %USERNAME%, is replaced with the value of the named environment variable. For example, %PATH% is replaced by the value of the PATH environment variable. The variable name match is case insensitive: %PATH% and %path% have the same effect.
  • %n for 0 <= n <= 9, such as %0 or %9, is replaced with the value of the n-th parameter passed to the batch file when it was invoked, subject to any subsequent modifications by the SHIFT command. For example: %2 is replaced by the value of the second batch file parameter. In interactive use (outside of a batch), no substitution takes place for this case.
  • %* is replaced with the values of all the command-line parameters except for %0, even those beyond index 9. SHIFT command has no impact on the result of %*. See also Command-line arguments. In interactive use (outside of a batch), no substitution takes place for this case.
Special variable names[edit | edit source]

Some variable names are not visible using SET command. Rather, they are made available for reading using the % notation. To find out about them, type «help set».

Special variable names and what they expand to:

Name Replacement Value Used
%CD% The current directory, not ending in a slash character if it is not the root directory of the current drive. See also #CD.
%TIME% The system time in HH:MM:SS.mm format unless regional settings dictate otherwise. See also #TIME.
%DATE% The system date in a format specific to localization. See also #DATE.
%RANDOM% A generated pseudorandom number between 0 and 32767. See also #Calculation.
%ERRORLEVEL% The error level returned by the last executed command, or by the last called batch script. See also #Error level.
%CMDEXTVERSION% The version number of the Command Processor Extensions currently used by cmd.exe.
%CMDCMDLINE% The content of the command line used when the current cmd.exe was started.

Links:

  • Windows Environment Variables at ss64.com
  • Command shell overview at TechNet / Microsoft Docs

Quoting and escaping[edit | edit source]

You can prevent the special characters that control command syntax from having their special meanings as follows, except for the percent sign (%):

  • You can surround a string containing a special character by quotation marks.
  • You can place caret (^), an escape character, immediately before the special characters. In a command located after a pipe (|), you need to use three carets (^^^) for this to work.

The special characters that need quoting or escaping are usually <, >, |, &, and ^. In some circumstances, ! and \ may need to be escaped. A newline can be escaped using caret as well.

When you surround the string using quotation marks, they become part of the argument passed to the command invoked. By contrast, when you use caret as an escape character, the caret does not become part of the argument passed.

The percent sign (%) is a special case. On the command line, it does not need quoting or escaping unless two of them are used to indicate a variable, such as %OS%. But in a batch file, you have to use a double percent sign (%%) to yield a single percent sign (%). Enclosing the percent sign in quotation marks or preceding it with caret does not work.

Examples

  • echo «Johnson & son»
    • Echoes the complete string rather than splitting the command line at the & character. Quotes are echoed as well
  • echo Johnson ^& son
    • As above, but using caret before the special character ampersand. No quotes are echoed.
  • echo Johnson & son
    • Does not use an escape character and therefore, «son» is interpreted as a separate command, usually leading to an error message that command son is not found.
  • echo A ^^ B
    • Echoes A ^ B. Caret needs escaping as well or else it is interpreted as escaping a space.
  • echo > NUL | echo A ^^^^ B
    • Echoes A ^ B. When after a pipe, a caret used for escaping needs to be tripled to work; the fourth caret is the one being escaped.
  • if 1 equ 1 ^
    echo Equal &^
    echo Indeed, equal
    • Echoes the two strings. The caret at the end of the line escapes the newlines, leading to the three lines being treated as if they were a single line. The space before the first caret is necessary or else 1 gets joined with the following echo to yield 1echo.
  • attrib File^ 1.txt
    • Does not show attributes of file named «File 1.txt» since escaping of space does not work. Using quotes, as in attrib «File 1.txt», works.
  • echo The ratio was 47%.
    • If run from a batch, the percent sign is ignored.
  • echo The ratio was 47%%.
    • If run from a batch, the percent sign is output once.
  • set /a modulo=14%%3
    • If run from a batch, sets modulo variable to 2, the remainder of dividing 14 by 3. Does not work with single %.
  • for %%i in (1,2,3) do echo %%i
    • If run from a batch, outputs 1, 2 and 3.
  • echo %temp%
    • Outputs the content of temp variable even if run from a batch file. Use of the percent sign in a batch to access environment variables and passed arguments needs no escaping.
  • echo ^%temp^%
    • Outputs literally %temp% when run from the command line.
  • echo %%temp%%
    • Outputs literally %temp% when run from a batch.
  • echo //comment line | findstr \//
    • Command FINDSTR uses backslash (\) for escaping. Unlike caret, this is internal to the command and unknown to the command shell.

Links:

  • Syntax : Escape Characters, Delimiters and Quotes at ss64
  • Command shell overview at Microsoft
  • set at Microsoft

Syntax[edit | edit source]

Command lines are developed into a sequence of commands according to a syntax. In that syntax, simple commands may be combined to form pipelines, which may in turn be combined to form compound commands, which finally may be turned into parenthesized commands.

A simple command is just a command name, a command tail, and some redirection specifications. An example of a simple command is dir *.txt > somefile.

A pipeline is several simple commands joined together with the «pipe» metacharacter—»|», also known as the «vertical bar». The standard output of the simple command preceding each vertical bar is connected to the standard input of the simple command following it, via a pipe. The command interpreter runs all of the simple commands in the pipeline in parallel. An example of a pipeline (comprising two simple commands) is dir *.txt | more.

A compound command is a set of pipelines separated by conjunctions. The pipelines are executed sequentially, one after the other, and the conjunction controls whether the command interpreter executes the next pipeline or not. An example of a compound command (comprising two pipelines, which themselves are just simple commands) is move file.txt file.bak && dir > file.txt.

The conjunctions:

  • & — An unconditional conjunction. The next pipeline is always executed after the current one has completed executing.
  • && — A positive conditional conjunction. The next pipeline is executed if the current one completes executing with a zero exit status.
  • || — A negative conditional conjunction. The next pipeline is executed if the current one completes executing with a non-zero exit status.

A parenthesized command is a compound command enclosed in parentheses (i.e. ( and )). From the point of view of syntax, this turns a compound command into a simple command, whose overall output can be redirected.

For example: The command line ( pushd temp & dir & popd ) > somefile causes the standard output of the entire compound command ( pushd temp & dir & popd ) to be redirected to somefile.

Links:

  • Conditional Execution at ss64.com
  • Using parenthesis/brackets to group expressions at ss64.com
  • Command shell overview at TechNet / Microsoft Docs

Redirection[edit | edit source]

Redirection specifications are applied, and removed from the command line, before an individual command in a sequence is executed. Redirection specifications control where the standard input, standard output, and standard error file handles for a simple command point. They override any effects to those file handles that may have resulted from pipelining. (See the preceding section on command syntax.) Redirection signs > and >> can be prefixed with 1 for the standard output (same as no prefix) or 2 for the standard error.

The redirection specifications are:

< filename
Redirect standard input to read from the named file.
> filename
Redirect standard output to write to the named file, overwriting its previous contents.
>> filename
Redirect standard output to write to the named file, appending to the end of its previous contents.
>&h
Redirect to handle h, where handle is any of 0—standard input, 1—standard output, 2—standard error, and more.
<&h
Redirect from handle h.

Examples:

  • dir *.txt >listing.log
    • Redirects the output of the dir command to listing.log file.
  • dir *.txt > listing.log
    • As above; the space before the file name makes no difference. However, if you type this into the command window, auto-completion with tab after typing «> l» actually works, while it does not work with «>listing.log».
  • dir *.txt 2>NUL
    • Redirects errors of the dir command to nowhere.
  • dir *.txt >>listing.log
    • Redirects the output of the dir command to listing.log file, appending to the file. Thereby, the content of the file before the redirected command was executed does not get lost.
  • dir *.txt >listing.log 2>&1
    • Redirects the output of the dir command to listing.log file, along with the error messages.
  • dir *.txt >listing.log 2>listing-errors.log
    • Redirects the output of the dir command to listing.log file, and the error messages to listing-errors.log file.
  • >myfile.txt echo Hello
    • The redirection can precede the command.
  • echo Hello & echo World >myfile.txt
    • Only the 2nd echo gets redirected.
  • (echo Hello & echo World) >myfile.txt
    • Output of both echos gets redirected.
  • type con >myfile.txt
    • Redirects console input (con) to the file. Thus, allows multi-line user input terminated by user pressing Control + Z. See also #User input.
  • (for %i in (1,2,3) do @echo %i) > myfile.txt
    • Redirects the entire output of the loop to the file.
  • for %i in (1,2,3) do @echo %i > myfile.txt
    • Starts redirection anew each time the body of the loop is entered, losing the output of all but the latest loop iteration.

Links:

  • Redirection at ss64.com
  • Using command redirection operators at Microsoft

How a command is executed[edit | edit source]

(…)

Batch reloading[edit | edit source]

The command interpreter reloads the content of a batch after each execution of a line or a bracketed group.

If you start the following batch and change «echo A» to «echo B» in the batch shortly after starting it, the output will be B.

@echo off
ping -n 6 127.0.0.1 >nul & REM wait
echo A

What is on a single line does matter; changing «echo A» in the following batch after running it has no impact:

@echo off
ping -n 6 127.0.0.1 >nul & echo A

Nor have after-start changes have any impact on commands bracketed with ( and ). Thus, changing «echo A» after starting the following batch has no impact:

@echo off
for /L %%i in (1,1,10) do (
  ping -n 2 127.0.0.1 >nul & REM wait
  echo A
)

Ditto for any other enclosing, including this one:

@echo off
(
ping -n 6 127.0.0.1 >nul & REM wait
echo A
)

Environment variables[edit | edit source]

The environment variables of the command interpreter process are inherited by the processes of any (external) commands that it executes. A few environment variables are used by the command interpreter itself. Changing them changes its operation.

Environment variables are affected by the SET, PATH, and PROMPT commands.

To unset a variable, set it to empty string, such as «set myvar=».

The command interpreter inherits its initial set of environment variables from the process that created it. In the case of command interpreters invoked from desktop shortcuts this will be Windows Explorer, for example.

Command interpreters generally have textual user interfaces, not graphical ones, and so do not recognize the Windows message that informs applications that the environment variable template in the Registry has been changed. Changing the environment variables in Control Panel will cause Windows Explorer to update its own environment variables from the template in the Registry, and thus change the environment variables that any subsequently invoked command interpreters will inherit. However, it will not cause command interpreters that are already running to update their environment variables from the template in the Registry.

See also #Variable substitution.

Links:

  • Windows Environment Variables at ss64.com
  • Command shell overview at TechNet / Microsoft Docs

COMSPEC[edit | edit source]

The COMSPEC environment variable contains the full pathname of the command interpreter program file. This is just inherited from the parent process, and is thus indirectly derived from the setting of COMSPEC in the environment variable template in the Registry.

PATH[edit | edit source]

The value of the PATH environment variable comprises a list of directory names, separated by semi-colon characters. This is the list of directories that are searched, in order, when locating the program file of an external command to execute.

PATHEXT[edit | edit source]

The value of the PATHEXT environment variable comprises a list of filename extensions, separated by semi-colon characters. This is the list of filename extensions that are applied, in order, when locating the program file of an external command to execute.

An example content of PATHEXT printed by «echo %PATHEXT%»:

  • .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

By adding «.PL» to the variable, you can ensure Perl programs get run from the command line even when typed without the «.pl» extension. Thus, instead of typing «mydiff.pl a.txt b.txt», you can type «mydiff a.txt b.txt».

Adding «.PL» to the variable in Windows Vista and later:

  • setx PATHEXT %PATHEXT%;.PL
    • If you use «set» available in Windows XP, the effect will be temporary and impacting only the current console or process.

Links:

  • Windows Environment Variables at ss64
  • Making Python scripts run on Windows without specifying “.py” extension at stackoverflow

PROMPT[edit | edit source]

The PROMPT environment variable controls the text emitted when the command interpreter displays the prompt. The command interpreter displays the prompt when prompting for a new command line in interactive mode, or when echoing a batch file line in batch file mode.

Various special character sequences in the value of the PROMPT environment variable cause various special effects when the prompt is displayed, as in the following table:

Characters Expansion Result
$$ $ character itself
$A & symbol AKA ampersand. A convenience, since it is difficult to place a literal & in the value of the PROMPT environment variable using the SET command.
$B Vertical bar ‘|’ (pipe symbol)
$C Left parenthesis ‘(‘
$D Current date
$E ESC (ASCII code 27)
$F Right parenthesis ‘)’
$G Greater-than symbol ‘>’
$H Backspace (deletes previous character)
$L Less-than symbol ‘<‘
$M Remote name linked to the current drive if it is a network drive; empty string otherwise.
$N Current drive letter
$P Current drive letter and full path
$Q ‘=’ (equals sign)
$S ‘ ‘ (space character)
$T Current system time
$V Windows version number
$_ <CR> (carriage return character, aka «enter»)
$+ As many plus signs (+) as there are items on the pushd directory stack

Links:

  • prompt at ss64
  • prompt at Microsoft

Switches[edit | edit source]

Most Windows commands provide switches AKA options to direct their behavior.

Observations:

  • Switches most often consist of a single-letter; some switches consist of a sequence of multiple letters.
  • Switches are preceded with a slash (/) rather than, as in some other operating systems, with a minus sign (-).
  • Switches are case-insensitive rather than, as in some other operating systems, case-sensitive.
  • If a command from another operating system is ported to Windows (such as grep), it usually retains the option conventions from the original operating system, including the use of minus sign and case-sensitivity.

Examples:

  • dir /?
    • Outputs the help. This option is provided by many commands.
  • dir /b /s
    • Lists all files and folders in the current folder recursively. Two switches are used: b and s.
  • dir /bs
    • Does not work; switches cannot be accumulated behind a single slash.
  • findstr /ric:»id: *[0-9]*» File.txt
    • Unlike many other commands, findstr allows the accumulation of switches behind a single slash. Indeed, r, i and c are single-letter switches.
  • dir/b/s
    • Works. In dir, removing whitespace between the command and the first switch or between the switches does not make a difference; thus, does the same as dir /b /s.
  • tree/f/a
    • Does not work, unlike tree /f /a. In tree, separation by whitespace is mandatory. Nor does find/i/v work.
  • dir /od
    • The switch letter o is further modified by a single letter specifying that ordering should be by date. The letter d is not a switch by itself. Similar cases include dir /ad and more /t4.
  • dir /B /S
    • The switches are case-insensitive, unlike in some other operating systems.
  • sort /r file.txt
    • Sorts the file in a reverse order.
  • sort /reverse file.txt
    • Sort allows the switch string to be longer than a single-letter.
  • sort /reve file.txt
    • Sort allows the specified switch string to be a substring of the complete long name of the switch. Thus, does the same as the above.
  • sort /reva file.txt
    • Does not work, since «reva» is not a substring of «reverse».
  • taskkill /im AcroRd32.exe
    • Taskkill requires a multiletter switch name for /im; shortening to /i does not work.
  • java -version
    • Java, which originated in the environment of another operating system family, uses the minus convention for its switches AKA options.
  • grep —help
    • If GNU grep is installed, it requires multi-letter switches to be preceded by two dashes.

Error level[edit | edit source]

Commands usually set error level at the end of their execution. In Windows NT and later, it is a 32-bit signed integer; in MS DOS, it used to be an integer from 0 to 255. Keywords: return code, exit code, exit status.

The conventional meaning of the error level:

  • 0 — success
  • not 0 — failure
  • The error levels being set are usually positive.
  • If the command does not distinguish various kinds of failure, the error level on failure is usually 1.

Uses of the error level:

  • It can be tested using && and ||; see also #Syntax.
  • It can be tested using IF.
  • The value can be accessed from ERRORLEVEL variable.

Examples:

  • dir >NUL && echo Success
    • The part after && is executed only if the error level is zero.
  • color 00 || echo Failure
    • The part after || is executed only if the error level is non-zero, whether positive or negative.
  • color 00 || (
      echo Failure
    )
    • Multiline bracketing works as well.
  • echo %ERRORLEVEL%
    • Outputs the error level without changing it.
  • if %errorlevel% equ 0 echo The error level is zero, meaning success.
  • if %errorlevel% neq 0 echo The error level is non-zero, meaning failure.
  • if errorlevel 1 echo The error level is >= 1, meaning failure via positive error level.
    • Does not cover failure via negative error level. Note the «>=» part: this is not the same as if %errorlevel% equ 1.
  • exit /b 1
    • Returns a batch file, setting the error level to 1.
  • cmd /c «exit /b 10»
    • In the middle of a batch file or on the command line, sets the error level to 10.
  • (cmd /c «exit /b 0» && Echo Success) & (cmd /c «exit /b -1» || Echo Failure)
    • As above, showing the error level is indeed affected.
  • (cmd /c «exit /b 0» & cmd /c «exit /b 1») || Echo Failure
    • The error level of a chain created by & is the error level of the last command of the chain.
  • cmd /c «exit /b -1» & if not errorlevel 1 echo Would-be success
    • The «if not errorlevel 1» test, which might appear to test for success, passes on negative numbers: it tests on «not error level >= 1», which is «error level <= 0».
  • set myerrorlevel=%errorlevel%
    • Remembers the error level for later.
  • set errorlevel=0
    • To be avoided: overshadows the built-in errorlevel variable. Ensures that subsequent accesses via %ERRORLEVEL% return 0 rather than the actual error level.
  • cmd /c «exit /b 0»
    if 1 equ 1 ( cmd /c «exit /b 1» & echo %errorlevel% )
    • Outputs 0, since %errorlevel% gets expanded before cmd /c «exit /b 1» gets executed.

Links:

  • Error level at ss64

String processing[edit | edit source]

Getting a substring of a non-empty variable:

set a=abcdefgh
echo %a:~0,1%   & rem from index 0, length 1; result: a
echo %a:~1,1%   & rem from index 1, length 1; result: b
echo %a:~0,2%   & rem from index 0, length 2; result: ab
echo %a:~1,2%   & rem from index 1, length 2; result: bc
echo %a:~1%     & rem from index 1 to the end; result: bcdefgh
echo %a:~-1%    & rem from index -1 (last char) to the end; result: h
echo %a:~-2%    & rem from index -2 (next-to-last) to the end; result: gh
echo %a:~0,-2%  & rem from index 0 to index -2, excl.; result: abcdef
echo %a:~0,-1%  & rem from index 0 to index -1, excl.; result: abcdefg
echo %a:~1,-1%  & rem from index 1 to index -1, excl.; result: bcdefg

Testing substring containment:

  • if not «%a:bc=%»==»%a%» echo yes
    • If variable a contains «bc» as a substring, echo «yes».
    • This test is a trick that uses string replacement, discussed below.
    • This test does not work if the variable contains a quotation mark.

Testing for «starts with»:

if %a:~0,1%==a echo yes   & rem If variable a starts with "a", echo "yes".
if %a:~0,2%==ab echo yes  & rem If variable a starts with "ab", echo "yes".

String replacement:

set a=abcd & echo %a:c=%   & rem replace c with nothing; result: abd
set a=abcd & echo %a:c=e%  & rem replace c with e; result: abed; 
set a=abcd & echo %a:*c=%  & rem replace all up to c with nothing; result: d
rem Above, the asterisk (*) only works at the beginning of the sought pattern.

See also the help for SET command: set /?.

Splitting a string by any of » «, «,» and «;»: [«space», «comma» and «semicolon»:]

set myvar=a b,c;d
for %%a in (%myvar%) do echo %%a

Splitting a string by semicolon, assuming the string contains no quotation marks:

@echo off
set myvar=a b;c;d
set strippedvar=%myvar%
:repeat
for /f "delims=;" %%a in ("%strippedvar%") do echo %%a
set prestrippedvar=%strippedvar%
set strippedvar=%strippedvar:*;=%
if not "%prestrippedvar:;=%"=="%prestrippedvar%" goto :repeat

Limitations:

  • The above string processing does not work with parameter variables (%1, %2, …).

Links:

  • Variables: extract part of a variable (substring) at ss64
  • Variable Edit/Replace at ss64

Command-line arguments[edit | edit source]

The command-line arguments AKA command-line parameters passed to a batch script are accessible as %1, %2, …, %9. There can be more than nine arguments; to access them, see how to loop over all of them below.

The syntax %0 does not refer to a command-line argument but rather to the name of the batch file.

Testing for whether the first command-line argument has been provided:

if not -%1-==-- echo Argument one provided
if -%1-==-- echo Argument one not provided & exit /b

A robust looping over all command-line arguments using SHIFT (for each command-line argument, …):

:argactionstart
if -%1-==-- goto argactionend
echo %1 & REM Or do any other thing with the argument
shift
goto argactionstart
:argactionend

A robust looping over all command-line arguments using SHIFT without modifying %1, %2, etc.:

call :argactionstart %*
echo Arg one: %1 & REM %1, %2, etc. are unmodified in this location
exit /b

:argactionstart
if -%1-==-- goto argactionend
echo %1 & REM Or do any other thing with the argument
shift
goto argactionstart
:argactionend
exit /b

Transferring command-line arguments to environment variables:

setlocal EnableDelayedExpansion
REM Prevent affecting possible callers of the batch
REM Without delayed expansion, !arg%argno%! used below won't work.
set argcount=0
:argactionstart
if -%1-==-- goto argactionend
set /a argcount+=1
set arg%argcount%=%1
shift
goto argactionstart
:argactionend

set argno=0
:loopstart
set /a argno+=1
if %argno% gtr %argcount% goto loopend
echo !arg%argno%! & REM Or do any other thing with the argument
goto loopstart
:loopend

Looping over all command-line arguments, albeit not a robust one:

for %%i in (%*) do (
  echo %%i
)

This looks elegant but is non-robust, maltreating arguments containing wildcards (*, ?). In particular, the above for command replaces arguments that contain wildcards (*, ?) with file names that match them, or drops them if no files match. Nonetheless, the above loop works as expected as long as the passed arguments do not contain wildcards.

Finding the number of command-line arguments, in a non-robust way:

set argcount=0
for %%i in (%*) do set /a argcount+=1

Again, this does not work with arguments containing wildcards.

The maximum possible number of arguments is greater than 4000, as empirically determined on a Windows Vista machine. The number can differ on Windows XP and Windows 7.

In passing arguments to a batch script, characters used for argument separation are the following ones:

  • space
  • comma
  • semicolon
  • equal sign
  • tab character

Thus, the following lines pass the same four arguments:

  • test.bat a b c d
  • test.bat a,b,c,d
  • test.bat a, b, c, d
  • test.bat a;b;c;d
  • test.bat a=b=c=d
  • test.bat a b,c;,;=d

Yes, even the line with «a b,c;,;=d» passes four arguments, since a sequence of separating characters is considered a single separator.

To have a space, comma or semicolon in the argument value, you can pass the value enclosed in quotation marks. However, the quotation marks become part of the argument value. To get rid of the enclosing quotation marks when referring to the argument in the script, you can use %~<number> described in #Percent tilde.

When passing arguments to an invoked command rather than a batch script, you usually need to separate the command from the first argument using a space. However, for internal commands, that separation is not necessary if the first character after the command name is one of a couple of symbols, including .\/, and more:

  • echo.
    • Outputs a newline.
  • tree.
    • Fails: «tree.» not found. tree is an external command.
  • dir..
    • Lists the content of the parent directory.
  • cd..
    • Changes the current directory to the parent one.
  • cd\
    • Changes the current directory to the root one.
  • start.
    • Opens Windows Explorer from the current directory.
  • dir/b/s
    • Lists directory content recursively, showing full paths.

Links:

  • Parameters / Arguments at ss64
  • Escape Characters, Delimiters and Quotes at ss64
  • Using batch parameters at Microsoft

Wildcards[edit | edit source]

Many commands accept file name wildcards—characters that do not stand for themselves and enable matching of a group of filenames.

Wildcards:

  • * (asterisk): any sequence of characters
  • ? (question mark): a single character other than a period («.») or, if part of a sequence of question marks at the end of a maximum period-free part of a file name, possibly zero number of characters; see examples for clarification

Examples:

  • dir *.txt
    • Matches Myfile.txt, Plan.txt and any other file with the .txt extension.
  • dir *txt
    • The period does not need to be included. However, this will also match files named without the period convention, such as myfiletxt.
  • ren *.cxx *.cpp
    • Renames all files with .cxx extension to have .cpp extension.
  • dir a?b.txt
    • Matches files aab.txt, abb.txt, a0b.txt, etc.
    • Does not match ab.txt, since a question mark followed by a character other than a question mark or period cannot match zero characters.
    • Does not match a.b.txt, since a question mark cannot match a period.
  • dir ???.txt
    • Matches .txt, a.txt, aa.txt, and aaa.txt, among others, since each question mark in the sequence followed by a period can match zero number of characters.
  • dir a???.b???.txt???
    • Matches a.b.txt, among others. While the last question mark sequence is not followed by a period, it is still a sequence at the end of a maximum period-free part of a file name.
  • dir ????????.txt & @REM eight question marks
    • Matches the same files as *.txt, since each file also has a short file name that has no more than 8 characters before .txt.

Quirk with short file names: the wildcard matching is performed both on long file names and the usually hidden short 8 chars + period + 3 chars file names. This can lead to bad surprises.

Unlike shells of some other operating systems, the cmd.exe shell does not perform wildcard expansion (replacement of the pattern containing wildcards with the list of file names matching the pattern) on its own. It is the responsibility of each program to treat wildcards as such. This enables such things as «ren *.txt *.bat», since the ren command actually sees the * wildcard rather than a list of files matching the wildcard. Thus, «echo *.txt» does not display files in the current folder matching the pattern but rather literally outputs «*.txt». Another consequence is that you can write «findstr a.*txt» without fearing that the «a.*txt» part gets replaced with the names of some files in the current folder. Furthermore, recursive «findstr /s pattern *.txt» is possible, while in some other operating systems, the «*.txt» part would get replaced with the file names found in the current folder, disregarding nested folders.

Commands accepting wildcards include ATTRIB, COPY, DIR, FINDSTR, FOR, REN, etc.

Links:

  • Wildcards at ss64
  • Using wildcard characters at Microsoft

User input[edit | edit source]

You can get input from the user using the following methods:

  • SET /P command
  • CHOICE command
  • Using «type con >myfile.txt», for which the multi-line user input is terminated by user pressing Control + Z.

Percent tilde[edit | edit source]

When a command-line argument contains a file name, special syntax can be used to get various information about the file.

The following syntaxes expand to various information about the file passed as %1:

Syntax Expansion Result Example
%~1 %1 with no enclosing quotation marks Not provided
%~f1 Full path with a drive letter C:\Windows\System32\notepad.exe
%~d1 Drive letter C:
%~p1 Drive-less path with the trailing backslash \Windows\System32\
%~n1 For a file, the file name without path and extension

For a folder, the folder name

notepad
%~x1 File name extension including the period .exe
%~s1 Modify of f, n and x to use short name C:\PROGRA~2\WINDOW~3\ACCESS~1\wordpad.exe
%~a1 File attributes —a——
%~t1 Date and time of last modification of the file (format depends on Windows «Regional format» setting) 02.11.2006 11:45 (example format as «English (United States)»)
%~z1 File size 151040
%~pn1 A combination of p and n \Windows\System32\notepad
%~dpnx1 A combination of several letters C:\Windows\System32\notepad.exe
%~$PATH:1 The full path of the first match found in the folders present in the PATH variable, or an empty string in no match.
%~n0 %~n applied to %0:

The extensionless name of the batch

tildetest
%~nx0 %~nx applied to %0:

The name of the batch

tildetest.bat
%~d0 %~d applied to %0:

The drive letter of the batch

C:
%~dp0 %~dp applied to %0:

The folder of the batch with trailing backslash

C:\Users\Joe Hoe\

The same syntax applies to single-letter variables created by FOR command, such as «%%i».

To learn about this subject from the command line, type «call /?» or «for /?».

Links:

  • Parameters / Arguments at ss64
  • Using batch parameters at Microsoft
  • for at Microsoft

Functions[edit | edit source]

Functions AKA subprograms can be emulated using CALL, labels, SETLOCAL and ENDLOCAL.

An example of a function that determines arithmetic power:

@echo off
call :power 2 4
echo %result%
rem Prints 16, determined as 2 * 2 * 2 * 2
goto :eof

rem __Function power______________________
rem Arguments: %1 and %2
:power
setlocal
set counter=%2
set interim_product=%1
:power_loop
if %counter% gtr 1 (
  set /a interim_product=interim_product * %1
  set /a counter=counter - 1
  goto :power_loop
)
endlocal & set result=%interim_product%
goto :eof

While the goto :eof at the end of the function is not really needed, it has to be there in the general case in which there is more than one function.

The variable into which the result should be stored can be specified on the calling line as follows:

@echo off
call :sayhello result=world
echo %result%
exit /b

:sayhello
set %1=Hello %2
REM Set %1 to set the returning value
exit /b

In the example above, exit /b is used instead of goto :eof to the same effect.

Also, remember that the equal sign is a way to separate parameters. Thus, the following items achieve the same:

  • call :sayhello result=world
  • call :sayhello result world
  • call :sayhello result,world
  • call :sayhello result;world

(See Command-line arguments as a reminder)

Links:

  • Functions at ss64

Calculation[edit | edit source]

Batch scripts can do simple 32-bit signed integer arithmetic and bitwise manipulation using SET /a command. The largest supported integer is 2147483647 = 2 ^ 31 — 1. The smallest supported integer is -2147483648 = — (2 ^ 31), assignable with the trick of set /a num=-2147483647-1. The syntax is reminiscent of the C language.

Arithmetic operators include *, /, % (modulo), +, -. In a batch, modulo has to be entered as «%%». There is no exponentiation operator.

Bitwise operators interpret the number as a sequence of 32 binary digits. These are ~ (complement), & (and), | (or), ^ (xor), << (left shift), >> (arithmetic AKA sign-preserving right shift). There is no logical AKA sign-erasing right shift and no bit rotation operators.

A logical operator of negation is !: it turns zero into one and non-zero into zero.

A combination operator is ,: it allows more calculations in one set command.

Combined assignment operators are modeled on «+=», which, in «a+=b», means «a=a+b». Thus, «a-=b» means «a=a-b». Similarly for *=, /=, %=, &=, ^=, |=, <<=, and >>=.

The precedence order of supported operators, is as follows:

  1. ( )
  2. * / % + —
  3. << >>
  4. &
  5. ^
  6. |
  7. = *= /= %= += -= &= ^= |= <<= >>=
  8. ,

Literals can be entered as decimal (1234), hexadecimal (0xffff, leading 0x), and octal (0777, leading 0).

The internal bit representation of negative integers is two’s complement. This provides for bit operations on negative integers, assignment and arithmetic results for hexadecimal literals larger than 0x7FFFFFFF and smaller or equal to 0xFFFFFFFF, and overflow and underflow behavior for arithmetic operations. For instance, -2147483648 is represented as 0x80000000, and therefore the binary complement operation «set /a num=~(-2147483647-1)» yields 2147483647, which equals 0x7FFFFFFF (type set /a num=0x7FFFFFFF to check). For another instance, «-2 & 0xF» yields 14 since -2 is 0xFFFFFFFE, which ANDed with 0xF yields 0xE, which is 14. While maximum decimal literal is 2147483647, maximum hexadecimal literal is 0xFFFFFFFF, which gets assigned as decadic -1. Similarly, 0xFFFFFFFE gets assigned as decadic -2, etc., and therefore, e.g. 0xFFFFFFFE — 2 yields -4. A hexadecimal literal larger than 0xFFFFFFFF is converted to -1, e.g. 0xFFFFFFFFFFFFFFFF, while a decimal literal larger than 2147483647 yields an error. The smallest representable 32-bit signed integer is -2147483648; «-2147483648» cannot be assigned directly since this is interpreted as -1 * 2147483648, and 2147483648 is larger than the maximum decimal literal 2147483647; however, -2147483647-1 does the trick, as does 0x80000000. «-2147483647-2» underflows to the largest positive integer, 2147483647; similary, «-2147483647-3» underflows to 2147483646. In the other direction, «2147483647+1» overflows to -2147483648, which corresponds to the addition being performed on the internal bit representation as if unsigned; «2147483647+2» overflows to -2147483647, etc.

As some of the operators have special meaning for the command interpreter, an expression using them needs to be enclosed in quotation marks, such as this:

  • set /a num=»255^127″
  • set /a «num=255^127»
    • Alternative placement of quotation marks.
  • set /a num=255^^127
    • Escape ^ using ^ instead of quotation marks.

Examples:

  • set /a n=(2+3)*5
    • Performs a simple calculation and stores the result in environment variable n. When called interactively (outside of a batch), outputs the result of calculation, otherwise outputs nothing.
  • set /a (2+3)*5
    • When called interactively (outside of a batch), outputs the result of calculation, otherwise outputs nothing. Changes no variable, being useful only for interactive use.
  • set n1=40 & set n2=25

    set /a n3=%n1%+%n2%

    • Uses the standard percent notation for variable expansion.
  • set n1=40 & set n2=25

    set /a n3=n1+n2

    • Avoids the percent notation around variable names as unneeded for /a.
  • set /a num=»255^127″
    • Encloses «^» in quotation marks to prevent its special meaning for the command interpreter.
  • set /a n1 = (10 + 5)/5
    • The spaces around = do not matter with /a. However, getting used to it lends itself to writing «set var = value» without /a, which sets the value of «var » rather than «var».
  • if 1==1 (set /a n1=(2+4)*5)
    • Does not work: the arithmetic brackets within grouping brackets cause trouble.
  • if 1==1 (set /a n1=^(2+4^)*5)
    • Escaping the arithmetic brackets with caret (^) works, as does enclosing «n1=2+(4*5)» in quotation marks.
  • set /a n1=2+3,n2=4*7
    • Performs two calculations.
  • set /a n1=n2=2
    • Has the same effect as n1=2,n2=2.
  • set n1=40 & set n2=25 & set /a n3=n1+n2
    • Works as expected.
  • set /a n1=2,n2=3,n3=n1+n2
    • Works as expected.
  • set n1=40 & set n2=25 & set /a n3=%n1%+%n2%
    • Does not work unless n1 and n2 were set previously. The variable specifications «%n1%» and «%n2″% get expanded before the first set command is executed. Dropping percent notation makes it work.
  • set /a n1=2,n2=3,n3=%n1%+%n2%
    • Does not work unless n1 and n2 were set previously, for the reason stated in the previous example.
  • set /a n1=0xffff
    • Sets n1 using hexadecimal notation, to «65535».
  • set /a n1=0777
    • Sets n1 using octal notation, to «511».
  • set /a n1=0xffffffff
    • Sets n1 to -1.
  • set /a n1=»~0″
    • Sets n1 to -1, in keeping with the underlying two’s complement representation of negative integers. Thus, bitwise complements (~) of non-negative integers yield negative integers.
  • set /a «n1=-1>>1»
    • Outputs -1 as a result of the «>>» operator being an arithmetic right shift, preserving the highest bit of the underlying bitwise representation of the signed 32-bit integer. Note that -1 is internally 0xFFFFFFFF. Logical right shift (highest-bit-dropping one) would result in 0x7FFFFFFF, the maximum positive 32-bit integer.
  • set /a «n1=(-1>>1) & 0x7FFFFFFF»
    • Emulates logical right shift (highest-bit-dropping one) by 1 by clearing the highest bit of the underlying bitwise representation of the 32-bit signed integer.
  • set /a «n=-1, shiftcount=4, n1=(n>>shiftcount) & (0x7FFFFFFF >> (shiftcount-1))»
    • Emulates logical right shift for shiftcount > 0, which when by more than 1 bit requires clearing multiple highest bits.
  • set /a «n1=1<<32»
    • Yields 0 for the right operand being 32 or higher, or when negative. This is unlike the behavior of the left shift instruction on modern x86 processors, which for 32-bit registers masks the right operand to constrain it to 5 bits. Thus, using the x86 instruction, the result would be 1 since 32 & 0x1F is 0. In the C language, the result is undefined and the actual result is platform dependent.
  • set /a n1=%random%
    • A pseudo-random number from 0 to 32767 = 2^15-1.
  • set /a n1=»%random%>>10″
    • A pseudo-random number from 0 to 31 = 2^5-1. The shift right operator drops 10 out of 15 bits, keeping 5 bits.
  • set /a n1=%random%%50
    • A pseudo-random number from 0 to 49. Uses the % modulo operator. In a batch, %% is needed for modulo: set /a n1=%random%%%50. Because of this particular use of the modulo, the result is not perfectly uniform; it is uniform if the 2nd modulo operand—above 50—equals to a power of 2, e.g. 256 = 2^8.
  • set /a n1=»(%random%<<15)+%random%»
    • A pseudo-random number from 0 to 1073741823 = 2^30 — 1. Combines the two 15-bit random numbers produced by %random% alone to produce a single 30-bit random number..
  • set /a n1=»((%random%<<15)+%random%)%1000000″
    • As above, but again using modulo, this time to achieve the range 0 to 999999.

An example calculation that prints prime numbers:

@echo off
setlocal
set n=1
:print_primes_loop
set /a n=n+1
set cand_divisor=1
:print_primes_loop2
set /a cand_divisor=cand_divisor+1
set /a cand_divisor_squared=cand_divisor*cand_divisor
if %cand_divisor_squared% gtr %n% echo Prime %n% & goto :print_primes_loop
set /a modulo=n%%cand_divisor
if %modulo% equ 0 goto :print_primes_loop & REM Not a prime
goto :print_primes_loop2

Links:

  • set at ss64.com
  • set at Microsoft
  • Random Numbers at ss64.com
  • Rules for how CMD.EXE parses numbers by dbenham, dostips.com
  • Bitwise operations # Batch File, rosettacode.org

Finding files[edit | edit source]

Files can be found using #DIR, #FOR, #FINDSTR, #FORFILES, and #WHERE.

Examples:

  • dir /b /s *base*.doc*
    • Outputs all files in the current folder and its subfolders such that the file name before the extension contains the word «base» and whose extension starts with «doc», which includes «doc» and «docx». The files are output with full paths, one file per line.
  • dir /b /s *.txt | findstr /i pers.*doc
    • Combines the result of outputting files including their complete paths with the findstr filtering command supporting limited regular expressions, yielding a versatile and powerful combination for finding files by names and the names of their directories.
  • for /r %i in (*) do @if %~zi geq 1000000 echo %~zi %i
    • For each file in the current folder and its subfolders that has the size greater than or equal to 1,000,000 bytes, outputs the file size in bytes and the full path of the file. For the syntax in %~zi, see #Percent tilde.
  • forfiles /s /d 06/10/2015 /c «cmd /c echo @fdate @path»
    • For each file in the current folder and its subfolders modified on 10 June 2015 or later, outputs the file modification date and full file path. The date format after /d is locale specific. Thus, allows to find most recently modified files.
  • (for /r %i in (*) do @echo %~ti :: %i) | findstr 2015.*::
    • Searching the current folder recursively, outputs files whose last modification date is in year 2015. Places the modification date and time, followed by a double colon, before the file name. Works as long as the used version of Windows and locale displays dates in a format that contains four-digit years. The double colon is used to make sure the findstr command is matching the date and not the file name.
  • for /r %i in (*) do @echo %~ti | findstr 2015 >NUL && echo %i
    • As above, outputs files changed in 2015. Unlike the above, only outputs the files, not the modification dates.
  • findstr /i /s /m cat.*mat *.txt
    • Finds files by their content. Performs a full text search for regular expression cat.*mat in files with names ending in .txt, and outputs the files names. The /m switch ensures only the file names are output.
  • where *.bat
    • Outputs all .bat files in the current directory and in the directories that are in PATH.

Keyboard shortcuts[edit | edit source]

When using Windows command line from the standard console that appears after typing cmd.exe after pressing Windows + R, you can use multiple keyboard shortcuts, including function keys:

  • Tab: Completes the relevant part of the typed string from file names or folder names in the current folder. The relevant part is usually the last space-free part, but use of quotation marks changes that. Generally considers both files and folders for completion, but cd command only considers folders.
  • Up and down arrow keys: Enters commands from the command history, one at a time.
  • Escape: Erases the current command line being typed.
  • F1: Types the characters from the single previously entered command from the command history, one character at a time. Each subsequent press of F1 enters one more character.
  • F2: Asks you to type a character, and enters the shortest prefix of the previous command from the command history that does not include the typed character. Thus, if the previous command was echo Hello world and you typed o, enters ech.
  • F3: Enters the single previous command from the command history. Repeated pressing has no further effect.
  • F4: Asks you to type a character, and erases the part of the currently typed string that starts at the current cursor location, continues to the right, and ends with the character you entered excluding that character. Thus, if you type echo Hello world, place the cursor at H using left arrow key, press F4 and then w, you get echo world. If you press F4 and then Enter, erases the text from the cursor to the end of the line.
  • F5: Enters previous commands from the command history, one at a time.
  • F6: Enters Control+Z character.
  • F7: Opens a character-based popup window with the command history, and lets you use arrow key and enter to select a command. After you press enter in the popup, the command is immediately executed.
  • F8: Given an already typed string, shows items from the command history that have that string as a prefix, one at a time.
  • F9: Lets you enter the number of the command from the command history, and then executes the command.
  • Alt + F7: Erases the command history.

The above are also known as command prompt keyboard shortcuts.

The availability of the above shortcuts does not seem to depend on running DOSKEY.

Links:

  • Windows Keyboard shortcuts at ss64.com
  • doskey at Microsoft

Paths[edit | edit source]

File and directory paths follow certain conventions. These include the possible use of a drive letter followed by a colon (:), the use of backslash (\) as the path separator, and the distinction between relative and absolute paths.

Forward slash (/) often works when used instead of (\) but not always; it is normally used to mark switches (options). Using forward slash can lead to various obscure behaviors, and is best avoided.

Special device names include NUL, CON, PRN, AUX, COM1, …, COM9, LPT1, …, LPT9; these can be redirected to.

Examples:

  • attrib C:\Windows\System32\notepad.exe
    • Succeeds if the file exists, as it should. This is an absolute path with a drive letter. It is also known as a fully qualified path.
  • attrib \Windows\System32\notepad.exe
    • Succeeds if the current drive is C:, and if the file exists, as it should. This is an absolute path without a drive letter.
  • cd /d C:\Windows & attrib System32\notepad.exe
    • Succeeds if the file exists. The path given to attrib is a relative path.
  • cd /d C:\Windows\System32 & attrib C:notepad.exe
    • Succeeds if the file exists. The path given to attrib is a relative one despite containing a drive letter: there would have to be C:\notepad.exe with a backslash for that to be an absolute path.
  • cd /d C:\Windows & attrib .\System32\notepad.exe
    • Succeeds if the file exists. A single period denotes the current folder.
  • attrib .
    • A single period denotes the current folder.
  • cd /d C:\Windows & attrib .\System32\\\notepad.exe
    • Succeeds if the file exists. Piling of backslashes has no impact beyond the first backslash.
  • cd /d C:\Windows & attrib .\System32
    • Succeeds if the folder exists.
  • cd /d C:\Windows & attrib .\System32\
    • Fails. Folders are usually denoted without the final backslash.
  • cd C:\Windows\System32\
    • Succeeds, whyever.
  • cd ..
    • A double period denotes the parent folder.
  • attrib C:\Windows\System32\..\..\Windows\System32
    • A double period can be used in the middle of the path to navigate to the parent folder, even multiple times.
  • attrib \\myserver\myvolume
    • A network UNC path starts with double backslash and no drive letter.
  • cd \\myserver\myvolume
    • Does not work; changing to a server folder in this direct manner does not work.
  • pushd \\myserver\folder
    • Automatically creates a drive for the folder and changes to it. After you use #POPD, the drive gets unassigned again.
  • attrib C:/Windows/System32/notepad.exe
    • Succeeds on multiple versions of cmd.exe. Uses forward slashes.

Links:

  • Long filenames, NTFS and legal filename characters at ss64.com
  • Naming Files, Paths, and Namespaces at Microsoft
  • W:Path (computing)#MS-DOS/Microsoft Windows style, wikipedia.org
  • Why does the cmd.exe shell on Windows fail with paths using a forward-slash (‘/) path separator?, stackoverflow.com

Arrays[edit | edit source]

Arrays can be emulated in the delayed expansion mode using the combination of % and ! to indicate variables. There, %i% is the value of variable i with the immediate expansion while !i! is the value of variable i in the delayed expansion.

@echo off
setlocal EnableDelayedExpansion
for /l %%i in (1, 1, 10) do (
  set array_%%i=!random!
)

for /l %%i in (1, 1, 10) do (
  echo !array_%%i!
)

:: For each item in the array, not knowing the length
set i=1
:startloop
if not defined array_%i% goto endloop
set array_%i%=!array_%i%!_dummy_suffix
echo A%i%: !array_%i%!
set /a i+=1
goto startloop
:endloop

Links:

  • Arrays, linked lists and other data structures in cmd.exe (batch) script, stackoverflow.com

Perl one-liners[edit | edit source]

Some tasks can be conveniently achieved with Perl one-liners. Perl is a scripting language originating in the environment of another operating system. Since many Windows computing environments have Perl installed, Perl one-liners are a natural and compact extension of Windows batch scripting.

Examples:

  • echo «abcbbc»| perl -pe «s/a.*?c/ac/»
    • Lets Perl act as sed, the utility that supports textual replacements specified using regular expressions.
  • echo a b| perl -lane «print $F[1]»
    • Lets Perl act as cut command, displaying the 2nd field or column of the line, in this case b. Use $F[2] to display 3rd field; indexing starts at zero. Native solution: FOR /f.
  • perl -ne «print if /\x22hello\x22/» file.txt
    • Acts as grep or FINDSTR, outputting the lines in file.txt that match the regular expression after if. Uses the powerful Perl regular expressions, more powerful than those of FINDSTR.
  • perl -ne «$. <= 10 and print» MyFile.txt
    • Lets Perl act as head -10 command, outputting the first 10 lines of the file.
  • perl -e «sleep 5»
    • Waits for 5 seconds.
  • for /f %i in (‘perl -MPOSIX -le «print strftime ‘%Y-%m-%d’, localtime»‘) do @set isodate=%i
    • Gets current date in the ISO format into isodate variable.
  • perl -MWin32::Clipboard -e «print Win32::Clipboard->Get()»
    • Outputs the text content of the clipboard. When stored to getclip.bat, yields a handy getclip command to complement CLIP command.
  • perl -MText::Diff -e «print diff ‘File1.txt’, ‘File2.txt'»
    • Outputs differences between two files in a format similar to diff command known from other operating systems, including context lines, lines starting with + and lines starting with -.
  • perl -MWin32::Sound -e «Win32::Sound::Play(‘C:\WINDOWS\Media\notify.wav’);»
    • Plays notification sound in notify.wav without showing any window.

On the web, Perl one-liners are often posted in the command-line conventions of another operating system, including the use of apostrophe (‘) to surround the arguments instead of Windows quotation marks. These need to be tweaked for Windows.

Links:

  • Perl One Liners at novosial.org
  • Why doesn’t my Perl one-liner work on Windows? at stackoverflow.com
  • W:One-liner program#Perl

Unix commands[edit | edit source]

Windows cmd.exe command interpreter can use commands from Unix-like operating systems, provided they are installed. Example commands include grep, sed, awk, wc, head and tail. The commands are available from GNU project, and their Windows ports exist. You can learn more about the commands in Guide to Unix Wikibook. Beware that batch programs that come to depend on these commands are not guaranteed to work on other Windows machines.

Freely licensed Windows versions of GNU commands can be obtained from the following projects:

  • GnuWin32, sourceforge.net
  • ezwinports, sourceforge.net: has some fresher ports than GnuWin32

An alternative way of running GNU commands for Windows 10 is Windows Subsystem for Linux.

Changing file timestamp[edit | edit source]

There is no touch command familiar from other operating systems. The touch command would modify the last-modification timestamp of a file without changing its content.

One workaround, with unclear reliability and applicability across various Windows versions, is this:

  • copy /b file.txt+,,

Links:

  • Windows recursive touch command at superuser.com
  • Windows version of the Unix touch command at stackoverflow.com

Getting last lines[edit | edit source]

There is no built-in command to get last lines of a file; no equivalent of tail command. However, a batch can be written to do the job and is available from the link below. Alternatively, one can install tail command for Windows, or run a PowerShell equivalent with no installation required.

Links:

  • CMD.EXE batch script to display last 10 lines from a txt file, stackoverflow.com

Hex dump[edit | edit source]

There is no preinstalled hex dump tool to view file content in hexadecimal. Nonetheless, there are the following options:

1) Use fsutil file createnew to create a file of all zero bytes and use fc to view the bytes via comparison:

  • fsutil file createnew allzeros.bin 1000
    fc /b C:\Windows\notepad.exe allzeros.bin
    • The notepad.exe bytes that are zero are not shown in the comparison, but other bytes are shown with their offset, one byte per line. You can determine how many initial bytes you want to see by choosing the length of the all-zero-byte file by changing the final 1000 above. To view complete file, create the all-zero-byte file of the length of the inspected file. Far from perfect, but can be used as a quick hack to view e.g. BOM marks or the kind of newlines used.

2) Use certutil -encodeHex, where the encodeHex feature is not officially documented:

  • certutil -encodeHex file.txt filehex.txt
    • Writes the file content of file.txt in hexadecimal to filehex.txt. Refuses to overwrite filehex.txt if it exists. You cannot limit the number of bytes to be converted.

3) Use PowerShell 5.0 Format-Hex:

  • powershell Format-Hex C:\Windows\notepad.exe
    • Option -Count is supported in latest PowerShell.

4) Install a command such as the feature-rich od or hexdump; see Unix commands. od can do octal dump as well.

5) If you are on old 32-bit version of Windows (not very likely), use debug; see DEBUG.

Links:

  • fc, docs.microsoft.com
  • certutil, ss64.com
  • certutil, docs.microsoft.com
  • Format-Hex, docs.microsoft.com
  • HEXDUMP.BAT version 2.1 using CERTUTIL by Dave Benham, dostips.com
  • New function — :hexDump by Dave Benham, dostips.com — pure batch; heavy batch magic and also slow

Running with elevated privileges[edit | edit source]

If your user has elevated administration privileges, you can use these if you start cmd.exe in a particular way. To start cmd.exe with these elevated privileges, you can do the following.

  • Select Start menu and type «cmd.exe» there.
  • Right-click on the cmd.exe icon and select Run as administrator.

To test whether you have elevated privileges:

  • Run «net session», which will fail if you do not have the privileges.

Limitations[edit | edit source]

Limitations include the following:

  • No while loop; can be emulated via labels and goto. There is a for loop and an if statement.
  • No break and continue statements for loop control known from the C language.
  • No proper custom functions; a custom function can be created using labels, call and %n parameter expansion.
  • Fragile processing of strings with special characters such as quotation marks («) or ampersands (&).
  • No arrays; can be emulated in a limited manner.
  • No associative arrays AKA dictionaries.
  • No floating-point arithmetic.
  • No ternary conditional operator from the C language.
  • No function to convert a character to its ascii value or to convert an ascii value to a character. No sane workarounds.
  • No arbitrarily large integer arithmetic.
  • No touch command to change file timestamp from other operating systems; no head and tail commands.
  • No GUI programming.
  • And more.

Built-in commands[edit | edit source]

These commands are all built in to the command interpreter itself, and cannot be changed. Sometimes this is because they require access to internal command interpreter data structures, or modify properties of the command interpreter process itself.

Overview[edit | edit source]

Command Description
ASSOC Associates an extension with a file type (FTYPE).
BREAK Sets or clears extended CTRL+C checking.
CALL Calls one batch program from another.
CD, CHDIR Outputs or sets the current directory.
CHCP Outputs or sets the active code page number.
CLS Clears the screen.
COLOR Sets the console foreground and background colors.
COPY Copies files.
DATE Outputs and sets the system date.
DEL, ERASE Deletes one or more files.
DIR Outputs a list of files and subdirectories in a directory.
ECHO Outputs messages, or turns command echoing on or off.
ELSE Performs conditional processing in batch programs when «IF» is not true.
ENDLOCAL Ends localization of environment changes in a batch file.
EXIT Quits the CMD.EXE program (command interpreter).
FOR Runs a specified command for each file in a set of files.
FTYPE Sets the file type command.
GOTO Goes to a label.
IF Performs conditional processing in batch programs.
MD, MKDIR Creates a directory.
MOVE Moves a file to a new location
PATH Sets or modifies the PATH environment
PAUSE Causes the command session to pause for user input.
POPD Changes to the drive and directory popped from the directory stack
PROMPT Sets or modifies the string displayed when waiting for input.
PUSHD Pushes the current directory onto the stack, and changes to the new directory.
RD / RMDIR Removes the directory.
REM A comment command. Unlike double-colon (::), the command can be executed.
REN / RENAME Renames a file or directory.
SET Sets or outputs shell environment variables.
SETLOCAL Creates a child-environment for the batch file.
SHIFT Moves the batch parameters forward.
START Starts a program with various options.
TIME Outputs or sets the system clock.
TITLE Changes the window title
TYPE Prints the content of a file to the console.
VER Shows the command processor, operating system versions.
VERIFY Verifies that file copy has been done correctly.
VOL Shows the label of the current volume.

ASSOC[edit | edit source]

Associates an extension with a file type (FTYPE), outputs existing associations, or deletes an association. See also FTYPE.

Examples:

  • assoc
    • Lists all associations, in the format «<file extension>=<file type>», as, for example, «.pl=Perl» or «.xls=Excel.Sheet.8».
  • assoc | find «.doc»
    • Lists all associations containing «.doc» substring.

Links:

  • assoc at ss64.com
  • assoc at Microsoft
  • Making Python scripts run on Windows without specifying “.py” extension at stackoverflow

BREAK[edit | edit source]

In Windows versions based on Windows NT, does nothing; kept for compatibility with MS DOS.

Examples:

  • break > empty.txt
    • Creates an empty file or to clears the content of an existing file, taking advantage of the fact that break does nothing and has no output. Shorter to type than «type nul > empty.txt».

Links:

  • break at Microsoft

CALL[edit | edit source]

Calls one batch program from another, calls a subprogram within a single batch program, or, as an undocumented behavior, starts a program. In particular, suspends the execution of the caller, starts executing the callee, and resumes the execution of the caller if and when the callee finishes execution.

For calling a subprogram, see Functions section.

Beware that calling a batch program from a batch without using the call keyword results in the execution never returning to the caller once the callee finishes.

The callee inherits environment variables of the caller, and unless the callee prevents that via SETLOCAL, changes made by the callee to environment variables become visible to the caller once it resumes execution.

Examples:

  • mybatch.bat
    • If used in a batch, transfers control to mybatch.bat and never resumes the execution of the caller.
  • call mybatch.bat
  • call mybatch
  • call mybatch.bat arg1 «arg 2»
  • call :mylabel
  • call :mylabel arg1 «arg 2»
  • cmd /c mybatch.bat
    • Similar to call, but resumes execution even when there are errors. Furthermore, any changes the callee makes to environment variables are not propagated to the caller.
  • call notepad.exe
    • Launches Notepad, or in general, any other executable. This is apparently not the intended usage of call, and is not officially documented.

See also Functions, CMD amd START.

Links:

  • call at ss64.com
  • call at Microsoft
  • CALL command vs. START with /WAIT option, stackoverflow.com

CD[edit | edit source]

Changes to a different directory, or outputs the current directory. However, if a different drive letter is used, it does not switch to that different drive or volume.

Examples:

  • cd
    • Outputs the current directory, e.g. C:\Windows\System32.
  • cd C:\Program Files
    • No surrounding quotes are needed around paths with spaces.
  • cd \Program Files
  • cd Documents
  • cd %USERPROFILE%
  • cd /d C:\Program Files
    • Changes to the directory of the C: drive even if C: is not the current drive.
  • C: & cd C:\Program Files.
    • Changes to the directory of the C: drive even if C: is not the current drive.
  • cd ..
    • Changes to the parent directory. Does nothing if already in the root directory.
  • cd ..\..
    • Changes to the parent directory two levels up.
  • C: & cd C:\Windows\System32 & cd ..\..\Program Files
    • Uses «..» to navigate through the directory tree up and down
  • cd \\myserver\folder
    • Does not work. Changing the directory directly to a network Universal Naming Convention (UNC) folder does not work. Keywords: UNC path.
  • subst A: \\myserver\folder && cd /d A:
    • Changes the directory to a server folder with the use of #SUBST command, assuming drive letter A: is free.
  • pushd \\myserver\folder
    • Automatically creates a drive for the folder and changes to it. After you use #POPD, the drive gets unassigned again.
  • cd C:\W*
    • Changes to C:\Windows, in a typical Windows setup. Thus, wildcards work. Useful for manual typing from the command line.
  • cd C:\W*\*32
    • Changes to C:\Windows\System32, in a typical Windows setup.

Links:

  • cd at ss64.com
  • cd at Microsoft

CHDIR[edit | edit source]

A synonym of CD.

CLS[edit | edit source]

Clears the screen.

COLOR[edit | edit source]

Sets the console foreground and background colors.

Examples:

  • color f9
    • Use white background and blue foreground.
  • color
    • Restore the original color setting.

Links:

  • color at ss64.com
  • color at Microsoft

COPY[edit | edit source]

Copies files. See also MOVE, XCOPY and ROBOCOPY.

Examples:

  • copy F:\File.txt
    • Copies the file into the current directory, assuming the current directory is not F:\.
  • copy «F:\My File.txt»
    • As above; quotation marks are needed to surround a file with spaces.
  • copy F:\*.txt
    • Copies the files located at F:\ and ending in dot txt into the current directory, assuming the current directory is not F:\.
  • copy F:\*.txt .
    • Does the same as the above command.
  • copy File.txt
    • Issues an error message, as File.txt cannot be copied over itself.
  • copy File1.txt File2.txt
    • Copies File1.txt to File2.txt, overwriting File2.txt if confirmed by the user or if run from a batch script.
  • copy File.txt «My Directory»
    • Copies File.txt into «My Directory» directory, assuming «My Directory» exists.
  • copy Dir1 Dir2
    • Copies all files directly located in directory Dir1 into Dir2, assuming Dir1 and Dir2 are directories. Does not copy files located in nested directories of Dir1.
  • copy *.txt *.bak
    • For each *.txt file in the current folder, makes a copy ending with «bak» rather than «txt».

Links:

  • copy at ss64.com
  • copy at Microsoft

DEL[edit | edit source]

Deletes files. Use with caution, especially in combination with wildcards. Only deletes files, not directories, for which see RD. For more, type «del /?».

Examples:

  • del File.txt
  • del /s *.txt
    • Deletes the files recursively including nested directories, but keeps the directories; mercilessly deletes all matching files without asking for confirmation.
  • del /p /s *.txt
    • As above, but asks for confirmation before every single file.
  • del /q *.txt
    • Deletes without asking for confirmation.

Links:

  • del at ss64.com
  • del at Microsoft

DIR[edit | edit source]

Lists the contents of a directory. Offers a range of options. Type «dir /?» for more help.

Examples:

  • dir
    • Lists the files and folders in the current folder, excluding hidden files and system files; uses a different manner of listing if DIRCMD variable is non-empty and contains switches for dir.
  • dir D:
  • dir /b C:\Users
  • dir /s
    • Lists the contents of the directory and all subdirectories recursively.
  • dir /s /b
    • Lists the contents of the directory and all subdirectories recursively, one file per line, displaying complete path for each listed file or directory.
  • dir *.txt
    • Lists all files with .txt extension.
  • dir /a
    • Includes hidden files and system files in the listing.
  • dir /ah
    • Lists hidden files only.
  • dir /ad
    • Lists directories only. Other letters after /A include S, I, R, A and L.
  • dir /ahd
    • Lists hidden directories only.
  • dir /a-d
    • Lists files only, omitting directories.
  • dir /a-d-h
    • Lists non-hidden files only, omitting directories.
  • dir /od
    • Orders the files and folders by the date of last modification. Other letters after /O include N (by name), E (by extension), S (by size), and G (folders first)
  • dir /o-s
    • Orders the files by the size descending; the impact on folder order is unclear.
  • dir /-c /o-s /a-d
    • Lists files ordered by size descending, omitting the thousands separator via /-C, excluding folders.
  • dir /s /b /od
    • Lists the contents of the directory and all subdirectories recursively, ordering the files in each directory by the date of last modification. The ordering only happens per directory; the complete set of files so found is not ordered as a whole.
  • dir /a /s
    • Lists files recursively including hidden files and system files. Can be used to find out the disk usage (directory size), by considering the final lines of the output.

Links:

  • dir at ss64.com
  • dir at Microsoft

DATE[edit | edit source]

Outputs or sets the date. The way the date is output depends on country settings. Date can also be output using «echo %DATE%».

Getting date in the iso format, like «2000-01-28»: That is nowhere easy, as the date format depends on country settings.

  • If you can assume the format of «Mon 01/28/2000», the following will do:
    • set isodate=%date:~10,4%-%date:~4,2%-%date:~7,2%
  • If you have WMIC, the following is locale independent:
    • for /f %i in (‘wmic os get LocalDateTime’) do @if %i lss a if %i gtr 0 set localdt=%i
      set isodate=%localdt:~0,4%-%localdt:~4,2%-%localdt:~6,2%
    • To use the above in a batch, turn %i into %%i and remove @ from before if.
  • If you have Perl installed:
    • for /f %i in (‘perl -MPOSIX -le «print strftime ‘%Y-%m-%d’, localtime»‘) do @set isodate=%i

Links:

  • date at ss64.com
  • date at Microsoft
  • How to get current datetime on Windows command line, in a suitable format for using in a filename? at stackoverflow

ECHO[edit | edit source]

Outputs messages, or turns command echoing on or off.

Examples:

  • echo on
  • @echo off
  • echo Hello
  • echo «hello»
    • Outputs the quotes too.
  • echo %PATH%
    • Outputs the contents of PATH variable.
  • echo Owner ^& son
    • Uses caret (^) to escape ampersand (&), thereby enabling echoing ampersands.
  • echo 1&echo 2&echo 3
    • Outputs three strings, each followed by a newline.
  • echo.
    • Outputs a newline while the period is not being output. Without the period, outputs «echo off» or «echo on». Adding a space before the period leads to the period being output. Other characters having the same effect as period include :;,/\(=+[].
  • echo %random%>>MyRandomNumbers.txt
    • While it seems to output random numbers to MyRandomNumbers.txt, it actually does not do so for numbers 0-9, since these, when placed before >>, indicate which channel is to be redirected. See also #Redirection.
  • echo 2>>MyRandomNumbers.txt
    • Instead of echoing 2, redirects standard error to the file.
  • (echo 2)>>MyRandomNumbers.txt
    • Echoes even a small number (in this case 2) and redirects the result.
  • >>MyRandomNumbers.txt echo 2
    • Another way to echo even a small number and redirect the result.

Displaying a string without a newline requires a trick:

  • set <NUL /p=Output of a command:
    • Outputs «Output of a command:». The output of the next command will be displayed immediately after «:».
  • set <NUL /p=Current time: & time /t
    • Outputs «Current time: » followed by the output of «time /t».
  • (set <NUL /p=Current time: & time /t) >tmp.txt
    • Like before, with redirecting the output of both commands to a file.

Links:

  • echo at ss64.com
  • echo at Microsoft

ELSE[edit | edit source]

An example:

if exist file.txt (
  echo The file exists.
) else (
  echo The file does not exist.
)

See also IF.

ENDLOCAL[edit | edit source]

Ends local set of environment variables started using SETLOCAL. Can be used to create subprograms: see Functions.

Links:

  • endlocal at ss64.com
  • endlocal at Microsoft

ERASE[edit | edit source]

A synonym of DEL.

EXIT[edit | edit source]

Exits the DOS console or, with /b, only the currently running batch or the currently executed subroutine. If used without /b in a batch file, causes the DOS console calling the batch to close.

Examples:

  • exit
  • exit /b

Links:

  • exit at ss64.com
  • exit at Microsoft

FOR[edit | edit source]

Iterates over a series of values, executing a command. Keywords: loop.

In the following examples, %i is to be used from the command line while %%i is to be used from a batch.
The index (e.g., %i) must be a single character variable name.

Examples without switches and with /r and /d switches:

  • for %%i in (1,2,3) do echo %%i
    • In a batch, echoes 1, 2, and 3. In a batch, the command must use a double percent sign.
    • The remaining examples are intended to be directly pasted into a command line, so they use a single percent sign and include «@» to prevent repetitive display.
  • for %i in (1,2,3) do @echo %i
    • From a command line, echoes 1, 2, and 3.
    • The for command tries to interpret the items as file names and as patterns of file names containing wildcards.
    • It does not complain if the items do not match existing file names, though.
  • for %i in (1,2,a*d*c*e*t) do @echo %i
    • Unless you happen to have a file matching the third pattern, echoes 1 and 2, discarding the third item.
  • for %i in (1 2,3;4) do @echo %i
    • Echoes 1, 2, 3, and 4. Yes, a mixture of item separators is used.
  • for %i in (*.txt) do @echo %i
    • Echoes file names of files located in the current folder and having the .txt extension.
  • for %i in («C:\Windows\system32\*.exe») do @echo %i
    • Echoes file names matching the pattern.
  • for /r %i in (*.txt) do @echo %i
    • Echoes file names with full paths, of files having the extension .txt located anywhere in the current folder including nested folders.
  • for /d %i in (*) do @echo %i
    • Echoes the names of all folders in the current folder.
  • for /r /d %i in (*) do @echo %i
    • Echoes the names including full paths of all folders in the current folder, including nested folders.
  • for /r %i in (*) do @if %~zi geq 1000000 echo %~zi %i
    • For each file in the current folder and its subfolders that has the size greater than or equal to 1,000,000 bytes, outputs the file size in bytes and the full path of the file. For the syntax in %~zi, see #Percent tilde.

Examples of /l switch:

  • for /l %i in (1,2,11) do @echo %i
    • Echoes the numbers from 1 to 11 with step 2. Thus, the format is (start, step, end). 32-bit signed integers are supported.
  • for /l %i in (10,-1,1) do @echo %i
    • Echoes the numbers from 10 to 1 descending.
  • for /l %i in (1,0,1) do @echo %i
    • Keeps echoing 1; an infinite loop.
  • for /l %i in (0) do @echo %i
    • Keeps echoing 0; an infinite loop.
  • for /l %i in () do @echo %i
    • Keeps echoing 0; an infinite loop.
  • for /l %i in (-10,1) do @echo %i
    • Echoes the numbers from -10 to 0; the unstated end limit integer is taken to be zero.
  • for /l %i in (0xF, 1, 020) do @echo %i
    • Echoes the numbers from 15 to 16; thus, hexadecimal and octal literals are supported.
  • for /l %i in (2147483646,1,2147483647) do @echo %i
    • Echoes 2147483646, then 2147483647, then -2147483648, then -2147483647, and so on. This is probably caused by an increment of 2147483647 overflowing to -2147483648.
  • for /l %i in (-2147483648,1,-2147483647) do @echo %i
    • Echoes -2147483648 and then -2147483647. Thus, directly supports -2147483648 literal, unlike set /a.

Examples with /f switch:

  • for /f «tokens=*» %i in (list.txt) do @echo %i
    • For each line in a file, echoes the line.
  • for /f «tokens=*» %i in (list1.txt list2.txt) do @echo %i
    • For each line in the files, echoes the line.
  • for /f «tokens=*» %i in (*.txt) do @echo %i
    • Does nothing. Does not accept wildcards to match file names.
  • for /f «tokens=1-3 delims=:» %a in («First:Second::Third») do @echo %c-%b-%a
    • Parses a string into tokens delimited by «:».
    • The quotation marks indicate the string is not a file name.
    • The second and third tokens are stored in %b and %c even though %b and %c are not expressly mentioned in the part of the command before «do».
    • The two consecutive colons are treated as one separator; %c is not «» but rather «Third».
    • Does some of the job of the cut command from other operating systems.
  • for /f «tokens=1-3* delims=:» %a in («First:Second::Third:Fourth:Fifth») do @echo %c-%b-%a: %d
    • As above, just that the 4th and 5th items get captured in %d as «Fourth:Fifth», including the separator.
  • for /f «tokens=1-3* delims=:,» %a in («First,Second,:Third:Fourth:Fifth») do @echo %c-%b-%a: %d
    • Multiple delimiters are possible.
  • for /f «tokens=1-3» %a in («First Second Third,item») do @echo %c-%b-%a
    • The default delimiters are space and tab. Thus, they differ from the separators used to separate arguments passed to a batch.
  • for /f «tokens=*» %i in (‘cd’) do @echo %i
    • For each line of the result of a command, echoes the line.
  • for /f «tokens=*» %i in (‘dir /b /a-d-h’) do @echo %~nxai
    • For each non-hidden file in the current folder, outputs the file attributes followed by the file name. In the string «%~nxai», uses the syntax described at #Percent tilde.
  • for /f «usebackq tokens=*» %i in (`dir /b /a-d-h`) do @echo %~nxai
    • As above, but using the backquote character (`) around the command to be executed.
  • for /f «tokens=*» %i in (‘tasklist ^| sort ^& echo End’) do @echo %i
    • Pipes and ampersands in the command to be executed must be escaped using caret (^).

Examples of redirection:

  • (for %i in (1,2,3) do @echo %i) > anyoldtemp.txt
    • To redirect the entire result of a for loop, place the entire loop inside brackets before redirecting. Otherwise, the redirection will tie to the body of the loop, so each new iteration of the body of the loop will override the results of the previous iterations.
  • for %i in (1,2,3) do @echo %i > anyoldtemp.txt
    • An example related to the one above. It shows the consequence of failing to put the loop inside brackets.

Continue: To jump to the next iteration of the loop and thus emulate the continue statement known from many languages, you can use goto provided you put the loop body in a subroutine, as shown in the following:

for %%i in (a b c) do call :for_body %%i
exit /b

:for_body
    echo 1 %1
    goto :cont
    echo 2 %1
  :cont
exit /b

If you use goto directly inside the for loop, the use of goto breaks the loop bookkeeping. The following fails:

for %%i in (a b c) do (
    echo 1 %%i
    goto :cont
    echo 2 %%i
  :cont
    echo 3 %%i
)

Links:

  • for at ss64.com
  • for at Microsoft
  • Rules for how CMD.EXE parses numbers by dbenham, dostips.com

FTYPE[edit | edit source]

Outputs or sets the command to be executed for a file type. See also ASSOC.

Examples:

  • ftype
    • Lists all associations of commands to be executed with file types, as, for example, ‘Perl=»C:\Perl\bin\perl.exe» «%1» %*’
  • ftype | find «Excel.Sheet»
    • Lists only associations whose display line contains «Excel.Sheet»

Links:

  • ftype at ss64.com
  • ftype at Microsoft
  • Making Python scripts run on Windows without specifying “.py” extension at stackoverflow

GOTO[edit | edit source]

Goes to a label.

An example:

goto :mylabel
echo Hello 1
REM Hello 1 never gets printed.

:mylabel
echo Hello 2
goto :eof

echo Hello 3
REM Hello 3 never gets printed. Eof is a virtual label standing for the end of file.

Goto within the body of a for loop makes cmd forget about the loop, even if the label is within the same loop body.

Links:

  • goto at ss64.com
  • goto at Microsoft

IF[edit | edit source]

Conditionally executes a command. Documentation is available by entering IF /? to CMD prompt.

Available elementary tests:

  • exist <filename>
  • <string>==<string>
  • <expression1> equ <expression2> — equals
  • <expression1> neq <expression2> — not equal
  • <expression1> lss <expression2> — less than
  • <expression1> leq <expression2> — less than or equal
  • <expression1> gtr <expression2> — greater than
  • <expression1> geq <expression2> — greater than or equal
  • defined <variable>
  • errorlevel <number>
  • cmdextversion <number>

To each elementary test, «not» can be applied. Apparently there are no operators like AND, OR, etc. to combine elementary tests.

The /I switch makes the == and equ comparisons ignore case.

An example:

if not exist %targetpath% (
  echo Target path not found.
  exit /b
)

Examples:

  • if not 1 equ 0 echo Not equal
  • if 1 equ 0 echo A & echo B
    • Does nothing; both echo commands are subject to the condition.
  • if not 1 equ 0 goto :mylabel
  • if not a geq b echo Not greater
  • if b geq a echo Greater
  • if b geq A echo Greater in a case-insensitive comparison
  • if B geq a echo Greater in a case-insensitive comparison
  • if 0 equ 00 echo Numerical equality
  • if not 0==00 echo String inequality
  • if 01 geq 1 echo Numerical comparison
  • if not «01» geq «1» echo String comparison
  • if 1 equ 0 (echo Equal) else echo Unequal
    • Notice the brackets around the positive then-part to make it work.
  • if not a==A echo Case-sensitive inequality
  • if /i a==A echo Case-insensitive equality
  • if /i==/i echo This does not work
  • if «/i»==»/i» echo Equal, using quotation marks to prevent the literal meaning of /i

Links:

  • if at ss64.com
  • if at Microsoft

MD[edit | edit source]

Creates a new directory or directories. Has a synonym MKDIR; see also its antonym RD.

Examples:

  • md Dir
    • Creates one directory in the current directory.
  • md Dir1 Dir2
    • Creates two directories in the current directory.
  • md «My Dir With Spaces»
    • Creates a directory with a name containing spaces in the current directory.

Links:

  • md at ss64.com
  • md at Microsoft

MKDIR[edit | edit source]

A synonym for MD.

MKLINK[edit | edit source]

Makes a symbolic link or other type of link. Available since Windows Vista.

Links:

  • mklink at ss64.com
  • mklink at Microsoft

MOVE[edit | edit source]

Moves files or directories between directories, or renames them. See also REN.

Examples:

  • move File1.txt File2.txt
    • Renames File1.txt to File2.txt, overwriting File2.txt if confirmed by the user or if run from a batch script.
  • move File.txt Dir
    • Moves File.txt file into Dir directory, assuming File.txt is a file and Dir is a directory; overwrites target file Dir\a.txt if conditions for overwriting are met.
  • move Dir1 Dir2
    • Renames directory Dir1 to Dir2, assuming Dir1 is a directory and Dir2 does not exist.
  • move Dir1 Dir2
    • Moves directory Dir1 into Dir2, resulting in existence of Dir2\Dir1, assuming both Dir1 and Dir2 are existing directories.
  • move F:\File.txt
    • Moves the file to the current directory.
  • move F:\*.txt
    • Moves the files located at F:\ and ending in dot txt into the current directory, assuming the current directory is not F:\.

Links:

  • move at ss64.com
  • move at Microsoft

PATH[edit | edit source]

Outputs or sets the value of the PATH environment variable. When outputting, includes «PATH=» at the beginning of the output.

Examples:

  • path
    • Outputs the PATH. An example output:
      • PATH=C:\Windows\system32;C:\Windows;C:\Program Files\Python27
  • path C:\Users\Joe Hoe\Scripts;%path%
    • Extends the path with C:\Users\Joe Hoe\Scripts, applying only to the process of the cmd.exe.
  • path ;
    • Empties the path.
  • echo %path% | perl -pe «s/;/\n/g» | sort
    • Shows the folders in the path sorted if you have perl installed.

Links:

  • path at ss64.com
  • path at Microsoft

PAUSE[edit | edit source]

Prompts the user and waits for a line of input to be entered.

Links:

  • pause at SS64.com
  • pause at Microsoft

POPD[edit | edit source]

Changes to the drive and directory popped from the directory stack. The directory stack is filled using the PUSHD command.

Links:

  • popd at ss64.com
  • popd at Microsoft

PROMPT[edit | edit source]

Can be used to change or reset the cmd.exe prompt. It sets the value of the PROMPT environment variable.

C:\>PROMPT MyPrompt$G

MyPrompt>CD
C:\

MyPrompt>PROMPT

C:\>

The PROMPT command is used to set the prompt to «MyPrompt>». The CD shows that the current directory path is «C:\». Using PROMPT without any parameters sets the prompt back to the directory path.

Links:

  • prompt at ss64.com
  • prompt at Microsoft

PUSHD[edit | edit source]

Pushes the current directory onto the directory stack, making it available for the POPD command to retrieve, and, if executed with an argument, changes to the directory stated as the argument.

Links:

  • pushd at ss64.com
  • pushd at Microsoft

RD[edit | edit source]

Removes directories. See also its synonym RMDIR and antonym MD. Per default, only empty directories can be removed. Also type «rd /?».

Examples:

  • rd Dir1
  • rd Dir1 Dir2
  • rd «My Dir With Spaces»
  • rd /s Dir1
    • Removes the directory Dir1 including all the files and subdirectories in it, asking for confirmation once before proceeding with the removal. To delete files recursively in nested directories with a confirmation per file, use DEL with /s switch.
  • rd /q /s Dir1
    • Like above, but without asking for confirmation.

Links:

  • rd at ss64.com
  • rd at Microsoft

REN[edit | edit source]

Renames files and directories.

Examples:

  • ren filewithtpyo.txt filewithtypo.txt
  • ren *.cxx *.cpp

Links:

  • ren at ss64.com
  • ren at Microsoft
  • How does the Windows RENAME command interpret wildcards?, superuser.com

RENAME[edit | edit source]

This is a synonym of REN command.

REM[edit | edit source]

Used for remarks in batch files, preventing the content of the remark from being executed.

An example:

REM A remark that does not get executed
echo Hello REM This remark gets displayed by echo
echo Hello & REM This remark gets ignored as wished
:: This sentence has been marked as a remark using double colon.

REM is typically placed at the beginning of a line. If placed behind a command, it does not work, unless preceded by an ampersand, as shown in the example above.

Double colon is an alternative to REM. It can cause trouble when used in the middle of sequences in parentheses, like those used in FOR loops. The double colon seems to be just a trick, a label that starts with a colon.

Links:

  • rem at ss64.com
  • rem at Microsoft
  • Which comment style should I use in batch files?, stackoverflow.com

RMDIR[edit | edit source]

This is a synonym of RD.

SET[edit | edit source]

Outputs or sets environment variables. With /P switch, it asks the user for input, storing the result in the variable. With /A switch, it performs simple arithmetic calculations, storing the result in the variable. With string assignments, spaces before and after the equality sign are usually avoided since they lead to usually unintended consequences: «set name = Peter» assigns to variable «name «, while «set name=Peter» assigns to variable «name». See also #Environment variables and #Calculation.

Examples:

  • set
    • Outputs a list of environment variables with their values, in the format of VAR=VALUE, a variable per line.
  • set home
    • Outputs a list of environment variables with their values for the variables whose names start with «home», case-insensitive, in the format of VAR=VALUE, a variable per line.
  • set HOME
    • As above; the match between the variable name prefix and the variable name is case insensitive.
  • set myname=Joe Hoe
    • Sets the variable to a new value.
  • set mynumber=56
    • Sets the variable to the string value of «56».
  • set mynumber=
    • Unsets the variable, removing it from variables. The equal sign (=) must be the final character; with any spaces after the equal sign, they become the new value of the variable.
  • set home=%home%;C:\Program Files\My Bin Folder
  • set /P user_input=Enter an integer:
  • set /P firstline=< File.txt
    • Sets the variable to the first line of the file. An equivalent of head -1 command from other operating systems.
  • set /A result = 4 * ( 6 / 3 )
    • Sets the result variable with the result of a calculation. See also #Calculation.
  • set name = Peter
    echo *%name %*
    • Sets the value of variable «name «, with ending space, to the value of » Peter», with leading space. The intent was probably to use «set name=Peter», with no separating spaces.

Links:

  • set at ss64.com
  • set at Microsoft

SETLOCAL[edit | edit source]

When used in a batch file, makes all further changes to environment variables local to the current batch file. When used outside of a batch file, does nothing. Can be ended using ENDLOCAL. Exiting a batch file automatically calls «end local». Can be used to create subprograms: see Functions.

Furthermore, can be used to enable delayed expansion like this: «setlocal EnableDelayedExpansion». Delayed expansion consists in the names of variables enclosed in exclamation marks being replaced with their values only after the execution reaches the location of their use rather than at an earlier point.

The following is an example of using delayed expansion in a script that prints the specified number of first lines of a file, providing some of the function of the command «head» known from other operating systems:

@echo off

call :myhead 2 File.txt
exit /b

:: Function myhead
:: ===============
:: %1 - lines count, %2 - file name
:myhead
setlocal EnableDelayedExpansion
set counter=1
for /f "tokens=*" %%i in (%2) do ( 
  echo %%i
  set /a counter=!counter!+1
  if !counter! gtr %1 exit /b
)
exit /b

Links:

  • setlocal at ss64.com
  • EnableDelayedExpansion at ss64.com
  • setlocal at Microsoft

SHIFT[edit | edit source]

Shifts the batch file arguments along, but does not affect %*. Thus, if %1=Hello 1, %2=Hello 2, and %3=Hello 3, then, after SHIFT, %1=Hello 2, and %2=Hello 3, but %* is «Hello 1» «Hello 2» «Hello 3».

Links:

  • shift at ss64.com
  • shift at Microsoft

START[edit | edit source]

Starts a program in new window, or opens a document. Uses an unclear algorithm to determine whether the first passed argument is a window title or a program to be executed; hypothesis: it uses the presence of quotes around the first argument as a hint that it is a window title.

Examples:

  • start notepad.exe & echo «Done.»
    • Starts notepad.exe, proceeding to the next command without waiting for finishing the started one. Keywords: asynchronous.
  • start «notepad.exe»
    • Launches a new console window with notepad.exe being its title, apparently an undesired outcome.
  • start «» «C:\Program Files\Internet Explorer\iexplore.exe»
    • Starts Internet Explorer. The empty «» passed as the first argument is the window title of a console that actually does not get opened, or at least not visibly so.
  • start «C:\Program Files\Internet Explorer\iexplore.exe»
    • Launches a new console window with «C:\Program Files\Internet Explorer\iexplore.exe» being its title, apparently an undesired outcome.
  • start /wait notepad.exe & echo «Done.»
    • Starts notepad.exe, waiting for it to end before proceeding.
  • start /low notepad.exe & echo «Done.»
    • As above, but starting the program with a low priority.
  • start «» MyFile.xls
    • Opens the document in the program assigned to open it.
  • start
    • Starts a new console (command-line window) in the same current folder.
  • start .
    • Opens the current folder in Windows Explorer.
  • start ..
    • Opens the parent folder in Windows Explorer.
  • start «» «mailto:»
    • Starts the application for writing a new email.
  • start «» «mailto:joe.hoe@hoemail.com?subject=Notification&body=Hello Joe, I’d like to…»
    • Starts the application for writing a new email, specifying the to, subject and body of the new email.
  • start «» «mailto:joe.hoe@hoemail.com?subject=Notification&body=Hello Joe,%0a%0aI’d like to…»
    • As above, with newlines entered as %0a.
  • start /b TODO:example-application-where-this-is-useful
    • Starts the application without opening a new console window, redirecting the output to the console from which the start command was called.

Links:

  • start at ss64.com
  • start at Microsoft
  • How to use command line switches to create a pre-addressed e-mail message in Outlook, support.microsoft.com

TIME[edit | edit source]

Ouputs or sets the system time. See also #DATE and TIME variable in #Special variable names.

Examples

  • time /t
    • Outputs the system time in HH:MM format, with no seconds and milliseconds. An example output: 09:19.
  • time
    • Outputs the system time in a locale-specific format possibly featuring seconds and hundredths of seconds and asks for a new time to set; the time is preceded by a locale-specific message, usually a translation of «Current time:». Thus, the output format differs from the one of «time /t».
  • echo %time%
    • Outputs the current time using the special variable TIME, in a locale-dependent format featuring hours, minutes, seconds and possibly hundredths of seconds. An example output for one locale: 9:19:31.55.
  • echo %time% & timeout 1 >nul & echo,|time
    • Outputs time before and after the command in the middle, here timeout 1. Can be used to measure execution time of the sequence in the middle; keywords: how long does it take.

Links:

  • time at ss64.com
  • time at Microsoft

TITLE[edit | edit source]

Sets the title displayed in the console window.

Links:

  • title at ss64.com
  • title at Microsoft

TYPE[edit | edit source]

Prints the content of a file or files to the output.

Examples:

  • type filename.txt
  • type a.txt b.txt
  • type *.txt
  • type NUL > tmp.txt
    • Create an empty file (blank file).

Links:

  • type at ss64.com
  • type at Microsoft

VER[edit | edit source]

Shows the command processor or operating system version.

C:\>VER

Microsoft Windows XP [Version 5.1.2600]

C:\>

Some version strings:

  • Microsoft Windows [Version 5.1.2600]
    • For Windows XP
  • Microsoft Windows [Version 6.0.6000]
    • For Windows Vista

The word «version» appears localized.

Links:

  • ver at ss64.com
  • ver at Microsoft
  • Operating System Version at Microsoft
  • List of Microsoft Windows versions, wikipedia.org
  • Windows Build Numbers, eddiejackson.net
  • mxpv/windows_build_numbers.txt, gist.github.com

VERIFY[edit | edit source]

Sets or clears the setting to verify whether COPY files etc. are written correctly.

Links:

  • verify at ss64.com
  • verify at Microsoft

VOL[edit | edit source]

Outputs volume labels.

Links:

  • vol at ss64.com
  • vol at Microsoft

External commands[edit | edit source]

External commands available to Windows command interpreter are separate executable program files, supplied with the operating system by Microsoft, or bundled as standard with the third-party command interpreters. By replacing the program files, the meanings and functions of these commands can be changed.

Many, but not all, external commands support the «/?» convention, causing them to write on-line usage information to their standard output and then to exit with a status code of 0.

ARP[edit | edit source]

Outputs or changes items in the address resolution protocol cache, which maps IP addresses to physical addresses.

Links:

  • arp at ss64.com
  • at arp Microsoft

AT[edit | edit source]

Schedules a program to be run at a certain time. See also SCHTASKS.

Links:

  • at at ss64.com
  • at at Microsoft

ATTRIB[edit | edit source]

Outputs or sets file attributes. With no arguments, it outputs the attributes of all files in the current directory. With no attribute modification instructions, it outputs the attributes of the files and directories that match the given search wildcard specifications. Similar to chmod of other operating systems.

Modification instructions:

  • To add an attribute, attach a ‘+’ in front of its letter.
  • To remove an attribute, attach a ‘-‘ in front of its letter
  • Attributes:
    • A — Archived
    • H — Hidden
    • S — System
    • R — Read-only
    • …and possibly others.

Examples:

  • attrib
    • Outputs the attributes of all files in the current directory.
  • attrib File.txt
    • Outputs the attributes of the file.
  • attrib +r File.txt
    • Adds the «Read-only» attribute to the file.
  • attrib -a File.txt
    • Removes the «Archived» attribute from the file.
  • attrib -a +r File.txt
    • Removes the «Archived» attribute and adds the «Read-only» attribute to the file.
  • attrib +r *.txt
    • Acts on a set of files.
  • attrib /S +r *.txt
    • Acts recursively in subdirectories.

For more, type «attrib /?».

Links:

  • attrib at ss64.com
  • attrib at Microsoft

BCDEDIT[edit | edit source]

(Not in XP). Edits Boot Configuration Data (BCD) files. For more, type «bcdedit /?».

Links:

  • bcdedit at ss64.com
  • at Microsoft

CACLS[edit | edit source]

Outputs or changes discretionary access control lists (DACLs). See also ICACLS. For more, type «cacls /?».

Links:

  • cacls at ss64.com
  • cacls at Microsoft

CHCP[edit | edit source]

Outputs or sets the active code page number. For more, type «chcp /?».

Links:

  • chcp at ss64.com
  • chcp at Microsoft

CHKDSK[edit | edit source]

Checks disks for disk problems, listing them and repairing them if wished. For more, type «chkdsk /?».

Links:

  • chkdsk at ss64.com
  • chkdsk at Microsoft

CHKNTFS[edit | edit source]

Shows or sets whether system checking should be run when the computer is started. The system checking is done using Autochk.exe. The «NTFS» part of the command name is misleading, since the command works not only with NTFS file system but also with FAT and FAT32 file systems. For more, type «chkntfs /?».

Links:

  • chkntfs at ss64.com
  • chkntfs at Microsoft

CHOICE[edit | edit source]

Lets the user choose one of multiple options by pressing a single key, and sets the error level as per the chosen option. Absent in Windows 2000 and Windows XP, it was reintroduced in Windows Vista, and has remained in Windows 7 and 8.

Examples:

  • choice /m «Do you agree»
    • Presents the user with a yes/no question, setting the error level to 1 for yes and to 2 for no. If the user presses Control + C, the error level is 0.
  • choice /c rgb /m «Which color do you prefer»
    • Presents the user with a question, and indicates the letters for the user. Responds to user pressing r, g or b, setting the error level to 1, 2 or 3.

An alternative is «set /p»; see SET.

Links:

  • choice at ss64.com
  • choice at Microsoft

CIPHER[edit | edit source]

Shows the encryption state, encrypts or decrypts folders on a NTFS volume.

Links:

  • cipher at ss64.com
  • cipher at Microsoft

CLIP[edit | edit source]

(Not in XP, or make a copy from Server 2003) Places the piped input to the clipboard.

Examples:

  • set | clip
    • Places the listing of environment variables to the clipboard.
  • clip < File1.txt
    • Places the content of File1.txt to the clipboard.

Links:

  • clip at ss64.com
  • clip at Microsoft

CMD[edit | edit source]

Invokes another instance of Microsoft’s CMD.

Links:

  • cmd at ss64.com
  • cmd at Microsoft

COMP[edit | edit source]

Compares files. See also FC.

Links:

  • comp at ss64.com
  • comp at Microsoft

COMPACT[edit | edit source]

Shows or changes the compression of files or folders on NTFS partitions.

Links:

  • compact at Microsoft

CONVERT[edit | edit source]

Converts a volume from FAT16 or FAT32 file system to NTFS file system.

Links:

  • convert at ss64.com
  • convert at Microsoft

DEBUG[edit | edit source]

Allows to interactively examine file and memory contents in assembly language, hexadecimal or ASCII. Available in 32-bit Windows including Windows 7; the availability in 64-bit Windows is unclear. In modern Windows, useful as a quick hack to view hex content of a file. Keywords: hex dump, hexdump, hexadecimal dump, view hex, view hexadecimal, disassembler.

Debug offers its own command line. Once on its command like, type «?» to find about debug commands.

To view hex of a file, invoke debug.exe with the file name as a parameter, and then repeatedly type «d» followed by enter on the debug command line.

Limitations:

  • Being a DOS program, debug chokes on long file names. Use dir /x to find the 8.3 file name, and apply debug on that one.
  • Debug cannot view larger files.

Links:

  • Debug for Windows XP at TechNet / Microsoft Docs
  • Debug for MS-DOS at TechNet / Microsoft Docs
  • W:Debug (command)

DISKCOMP[edit | edit source]

Compares the content of two floppies.

Links:

  • diskcomp at ss64.com
  • diskcomp at Microsoft

DISKCOPY[edit | edit source]

Copies the content of one floppy to another.

Links:

  • diskcopy at ss64.com
  • diskcopy at Microsoft

DISKPART[edit | edit source]

Shows and configures the properties of disk partitions.

Links:

  • diskpart at ss64.com
  • diskpart at Microsoft, for XP
  • diskpart at Microsoft

DOSKEY[edit | edit source]

Above all, creates macros known from other operating systems as aliases. Moreover, provides functions related to command history, and enhanced command-line editing. Macros are an alternative to very short batch scripts.

Macro-related examples:

  • doskey da=dir /s /b
    • Creates a single macro called «da»
  • doskey np=notepad $1
    • Creates a single macro that passes its first argument to notepad.
  • doskey /macrofile=doskeymacros.txt
    • Loads macro definitions from a file.
  • doskey /macros
    • Lists all defined macros with their definitions.
  • doskey /macros | find «da»
    • Lists all macro definitions that contain «da» as a substring; see also FIND.

Command history-related examples:

  • doskey /history
    • Lists the complete command history.
  • doskey /history | find «dir»
    • Lists each line of command history that contains «dir» as a substring
  • doskey /listsize=100
    • Sets the size of command history to 100.

To get help on doskey from command line, type «doskey /?».

Links:

  • doskey at ss64.com
  • doskey at Microsoft

DRIVERQUERY[edit | edit source]

Shows all installed device drivers and their properties.

Links:

  • driverquery at ss64.com
  • driverquery at Microsoft

EXPAND[edit | edit source]

Extracts files from compressed .cab cabinet files. See also #MAKECAB.

Links:

  • expand at ss64.com
  • expand at Microsoft

FC[edit | edit source]

Compares files, displaying the differences in their content in a peculiar way.

Examples:

  • fc File1.txt File2.txt >NUL && Echo Same || echo Different or error
    • Detects difference using the error level of fc. The error level of zero means the files are the same; non-zero can mean the files differ but also that one of the files does not exist.

Links:

  • fc at ss64.com
  • fc at Microsoft

FIND[edit | edit source]

Searches for a string in files or input, outputting matching lines. Unlike FINDSTR, it cannot search folders recursively, cannot search for a regular expression, requires quotation marks around the sought string, and treats space literally rather than as a logical or.

Examples:

  • find «(object» *.txt
  • dir /S /B | find «receipt»
  • dir /S /B | find /I /V «receipt»
    • Prints all non-matching lines in the output of the dir command, ignoring letter case.
  • find /C «inlined» *.h
    • Instead of outputting the matching lines, outputs their count. If more than one file is searched, outputs one count number per file preceded with a series of dashes followed by the file name; does not output the total number of matching lines in all files.
  • find /C /V «» < file.txt
    • Outputs the number of lines AKA line count in «file.txt». Does the job of «wc -l» of other operating systems. Works by treating «» as a string not found on the lines. The use of redirection prevents the file name from being output before the number of lines.
  • type file.txt | find /C /V «»
    • Like the above, with a different syntax.
  • type *.txt 2>NUL | find /C /V «»
    • Outputs the sum of line counts of the files ending in «.txt» in the current folder. The «2>NUL» is a redirection of standard error that removes the names of files followed by empty lines from the output.
  • find «Schönheit» *.txt
    • If run from a batch file saved in unicode UTF-8 encoding, searches for the search term «Schönheit» in UTF-8 encoded *.txt files. For this to work, the batch file must not contain the byte order mark written by Notepad when saving in UTF-8. Notepad++ is an example of a program that lets you write UTF-8 encoded plain text files without byte order mark. While this works with find command, it does not work with #FINDSTR.
  • find «Copyright» C:\Windows\system32\a*.exe
    • Works with binary files no less than text files.

Links:

  • find at ss64.com
  • find at Microsoft

FINDSTR[edit | edit source]

Searches for regular expressions or text strings in files. Does some of the job of «grep» command known from other operating systems, but is much more limited in the regular expressions it supports.

Treats space in a regular expression as a disjunction AKA logical or unless prevented with /c option.

Examples:

  • findstr /s «[0-9][0-9].*[0-9][0-9]» *.h *.cpp
    • Searches recursively all files whose name ends with dot h or dot cpp, printing only lines that contain two consecutive decimal digits followed by anything followed by two consecutive decimal digits.
  • findstr «a.*b a.*c» File.txt
    • Outputs all lines in File.txt that match any of the two regular expressions separated by the space. Thus, the effect is one of logical or on regular expressions.
  • echo world | findstr «hello wo.ld»
    • Does not match. Since the 1st item before the space does not look like a regex, findstr treats the whole search term as a plain search term.
  • echo world | findstr /r «hello wo.ld»
    • Matches. The use of /r forces regex treatment.
  • findstr /r /c:»ID: *[0-9]*» File.txt
    • Outputs all lines in File.txt that match the single regular expression containing a space. The use of /c prevents the space from being treated as a logical or. The use of /r switches the regular expression treatment on, which was disabled by default by the use of /c. To test this, try the following:
      • echo ID: 12|findstr /r /c:»ID: *[0-9]*$»
        • Matches.
      • echo ID: 12|findstr /c:»ID: *[0-9]*$»
        • Does not match, as the search string is not interpreted as a regular expression.
      • echo ID: abc|findstr «ID: *[0-9]*$»
        • Matches despite the output of echo failing to match the complete regular expression: the search is interpreted as one for lines matching «ID:» or «*[0-9]*$».
  • findstr /ric:»id: *[0-9]*» File.txt
    • Does the same as the previous example, but in a case-insensitive manner.
    • While findstr enables this sort of accumulation of switches behind a single «/», this is not possible with any command. For instance, «dir /bs» does not work, while «dir /b /s» does.
    • To test this, try the following:
      • echo ID: 12|findstr /ric:»id: *[0-9]*$»
      • echo ID: ab|findstr /ric:»id: *[0-9]*$»
  • findstr /msric:»id: *[0-9]*» *.txt
    • Like above, but recursively for all files per /s, displaying only matching files rather than matching lines per /m.
  • echo hel lo | findstr /c:»hel lo» /c:world
    • /c switch can be used multiple times to create logical or.
  • echo \hello\ | findstr «\hello\»
    • Does not match. Backslash before quotation marks and multiple other characters acts as an escape; thus, \» matches «.
  • echo \hello\ | findstr «\\hello\\»
    • Matches. Double backslash passed to findstr stands for a single backslash.
  • echo \hello\ | findstr \hello\
    • Matches. None of the single backslashes passed to findstr is followed by a character on which the backslash acts as an escape.
  • echo ^»hey | findstr \^»hey | more
    • To search for a quote (quotation mark), you need to escape it two times: once for the shell using caret (^), and once for findstr using backslash (\).
  • echo ^»hey | findstr ^»\^»hey there^» | more
    • To search for a quote and have the search term enclosed in quotes as well, the enclosing quotes need to be escaped for the shell using caret (^).
  • echo //comment line | findstr \//
    • If forward slash (/) is the 1st character in the search term, it needs to be escaped with a backslash (\). The escaping is needed even if the search term is enclosed in quotes.
  • findstr /f:FileList.txt def.*():
    • Search in the files stated in FileList.txt, one file per line. File names in FileList.txt can contain spaces and do not need to be surrounded with quotation marks for this to work.
  • findstr /g:SearchTermsFile.txt *.txt
    • Search for the search terms found in SearchTermsFile.txt, one search term per line. A space does not serve to separate two search terms; rather, each line is a complete search term. A line is matched if at least one of the search terms matches. If the first search term looks like a regex, the search will be a regex one, but if it looks like a plain search term, the whole search will be a plain one even if 2nd or later search terms look like regex.
  • findstr /xlg:File1.txt File2.txt
    • Outputs set intersection: lines present in both files.
  • findstr /xlvg:File2.txt File1.txt
    • Outputs set difference: File1.txt — File2.txt.
  • findstr /m Microsoft C:\Windows\system32\*.com
    • Works with binary files no less than text files.

Limitations of the regular expressions of «findstr», as compared to «grep»:

  • No support of groups — «\(«, «\)».
  • No support of greedy iterators — «*?».
  • No support of «zero or one of the previous» — «?».
  • And more.

Other limitations: There is a variety of limitations and strange behaviors as documented at
What are the undocumented features and limitations of the Windows FINDSTR command?.

Bugs:

  • echo bb|findstr «bb baaaa»
    • Does not find anything in multiple Windows versions, but it should.

Also consider typing «findstr /?».

Links:

  • findstr at ss64.com
  • findstr at Microsoft
  • What are the undocumented features and limitations of the Windows FINDSTR command? at StackOverflow

FORFILES[edit | edit source]

Finds files by their modification date and file name pattern, and executes a command for each found file. Is very limited, especially compared to the find command of other operating systems. Available since Windows Vista. For more, type «forfiles /?».

Examples:

  • forfiles /s /d 06/10/2015 /c «cmd /c echo @fdate @path»
    • For each file in the current folder and its subfolders modified on 10 June 2015 or later, outputs the file modification date and full file path. The date format after /d is locale specific. Thus, allows to find most recently modified files. Keywords: most recently changed files.
  • forfiles /m *.txt /s /d 06/10/2015 /c «cmd /c echo @fdate @path»
    • As above, but only for files ending in .txt.

Links:

  • forfiles at ss64.com
  • forfiles at Microsoft

forfiles /?

FORMAT[edit | edit source]

Formats a disk to use Windows-supported file system such as FAT, FAT32 or NTFS, thereby overwriting the previous content of the disk. To be used with great caution.

Links:

  • format at ss64.com
  • format at Microsoft

FSUTIL[edit | edit source]

A powerful tool performing actions related to FAT and NTFS file systems, to be ideally only used by powerusers with an extensive knowledge of the operating systems.

Links:

  • fsutil at ss64.com
  • fsutil at Microsoft
    • Fsutil: behavior
    • Fsutil: dirty
    • Fsutil: file
    • Fsutil: fsinfo
    • Fsutil: hardlink
    • Fsutil: objectid
    • Fsutil: quota
    • Fsutil: reparsepoint
    • Fsutil: sparse
    • Fsutil: usn
    • Fsutil: volume

GPRESULT[edit | edit source]

Outputs group policy settings and more for a user or a computer.

Links:

  • gpresult at ss64.com
  • gpresult at Microsoft
  • Wikipedia:Group Policy

GRAFTABL[edit | edit source]

Enables the display of an extended character set in graphics mode. For more, type «graftabl /?».

Links:

  • graftabl at Microsoft

HELP[edit | edit source]

Shows command help.

Examples:

  • help
    • Shows the list of Windows-supplied commands.
  • help copy
    • Shows the help for COPY command, also available by typing «copy /?».

Links:

  • help at ss64.com
  • help at Microsoft

ICACLS[edit | edit source]

(Not in XP) Shows or changes discretionary access control lists (DACLs) of files or folders. See also CACLS. Fore more, type «icacls /?».

Links:

  • icacls at ss64.com
  • icacls at Microsoft

IPCONFIG[edit | edit source]

Outputs Windows IP Configuration. Shows configuration by connection and the name of that connection (i.e. Ethernet adapter Local Area Connection)
Below that the specific info pertaining to that connection is displayed such as DNS suffix and ip address and subnet mask.

Links:

  • ipconfig at ss64.com
  • ipconfig at Microsoft

LABEL[edit | edit source]

Adds, sets or removes a disk label.

Links:

  • label at ss64.com
  • label at Microsoft

MAKECAB[edit | edit source]

Places files into compressed .cab cabinet file. See also #EXPAND.

Links:

  • makecab at ss64.com
  • makecab at Microsoft

MODE[edit | edit source]

A multi-purpose command to display device status, configure ports and devices, and more.

Examples:

  • mode
    • Outputs status and configuration of all devices, such as com3 and con.
  • mode con
    • Outputs status and configuration of con device, the console in which the command interpreter is running.
  • mode con cols=120 lines=20
    • Sets the number of columns and lines for the current console, resulting in window resizing, and clears the screen. The setting does not affect new console instances. Keywords: wide screen, wide window, screen size, window size, resize screen, resize window.
  • mode 120, 20
    • As above: Sets the number of columns (120) and lines (20), resulting in window resizing, and clears the screen.
  • mode con cols=120
    • Sets the number of columns for the current console, resulting in window resizing, and clears the screen. It seems to change the number of visible lines as well, but the total lines count of the console buffer seems unchanged.
  • mode 120
    • As above: Sets the number of columns.
  • mode con cp
    • Outputs the current code page of the console.
  • mode con cp select=850
    • Sets the current code page of the console. For a list of code pages, see the linked Microsoft documentation below.
  • mode con rate=31 delay=1
    • Sets the rate and delay for repeated entry of a character while a key is held pressed, of the console. The lower the rate, the fewer repetitions per second.

Links:

  • mode at ss64.com
  • mode at Microsoft

MORE[edit | edit source]

Outputs the contents of a file or files, one screen at a time. When redirected to a file, performs some conversions, also depending on the used switches.

Examples:

  • more Test.txt
  • more *.txt
  • grep -i sought.*string Source.txt | more /p >Out.txt
    • Taking the output of a non-Windows grep command that produces line breaks consisting solely of LF character without CR character, converts LF line breaks to CR-LF line breaks. CR-LF newlines are also known as DOS line breaks, Windows line breaks, DOS newlines, Windows newlines, and CR/LF line endings,as opposed to LF line breaks used by some other operating systems.
    • In some setups, seems to output gibberish if the input contains LF line breaks and tab characters at the same time.
    • In some setups, for the conversion, /p may be unneeded. Thus, «more» would convert the line breaks even without /p.
  • more /t4 Source.txt >Target.txt
    • Converts tab characters to 4 spaces.
    • In some setups, tab conversion takes place automatically, even without the /t switch. If so, it is per default to 8 spaces.

Switch /e:

  • The online documentation for «more» in Windows XP and Windows Vista does not mention the switch.
  • The switch /e is mentioned in «more /?» at least in Windows XP and Windows Vista.
  • Per «more /?», the switch is supposed to enable extended features listed at the end of «more /?» help such as showing the current row on pressing «=». However, in Windows XP and Windows Vista, that seems to be enabled by default even without /e.
  • Hypothesis: In Windows XP and Windows Vista, /e does not do anything; it is present for compatibility reasons.

Links:

  • more at ss64.com
  • more at Microsoft, Windows XP
  • more at Microsoft, Windows Server 2008, Windows Vista

NET[edit | edit source]

Provides various network services, depending on the command used. Available variants per command:

  • net accounts
  • net computer
  • net config
  • net continue
  • net file
  • net group
  • net help
  • net helpmsg
  • net localgroup
  • net name
  • net pause
  • net print
  • net send
  • net session
  • net share
  • net start
  • net statistics
  • net stop
  • net time
  • net use
  • net user
  • net view

Links:

  • net at ss64.com
  • net services overview at Microsoft, Windows XP
    • net computer at Microsoft
    • net group at Microsoft
    • net localgroup at Microsoft
    • net print at Microsoft
    • net session at Microsoft
    • net share at Microsoft
    • net use at Microsoft
    • net user at Microsoft
    • net view at Microsoft

OPENFILES[edit | edit source]

Performs actions pertaining to open files, especially those opened by other users over the network. The actions involve querying, displaying, and disconnecting. For more, type «openfiles /?».

Links:

  • openfiles at ss64.com
  • openfiles at Microsoft

PING[edit | edit source]

Syntax:

  • PING /?
  • PING address
  • PING hostname

Send ICMP/IP «echo» packets over the network to the designated address (or the first IP address that the designated hostname maps to via name lookup) and print all responses received.

Examples:

  • ping en.wikibooks.org
  • ping 91.198.174.192
  • ping http://en.wikibooks.org/
    • Does not work.

Links:

  • ping at ss64.com
  • ping at Microsoft

RECOVER[edit | edit source]

Recovers as much information as it can from damaged files on a defective disk.

Links:

  • recover at ss64.com
  • recover at Microsoft

REG[edit | edit source]

Queries or modifies Windows registry.

The first argument is one of the following commands: query, add, delete, copy, save, load, unload, restore, compare, export, import, and flags. To learn more about a command, follow it by /?, like reg query /?.

Links:

  • reg at ss64.com
  • reg at Microsoft

REPLACE[edit | edit source]

Replaces files in the destination folder with same-named files in the source folder.

Links:

  • replace at ss64.com
  • replace at Microsoft

ROBOCOPY[edit | edit source]

(Not in XP) Copies files and folders. See also XCOPY and COPY.

Examples:

  • robocopy /s C:\Windows\system C:\Windows-2\system *.dll
    • Copies all files ending in .dll from one directory to another, replicating the nested directory structure.

Links:

  • robocopy at ss64.com
  • robocopy at Microsoft

RUNDLL32[edit | edit source]

Runs a function available from a DLL. The available DLLs and their functions differ among Windows versions.

Examples:

  • rundll32 sysdm.cpl,EditEnvironmentVariables
    • In some Windows versions, opens the dialog for editing environment variables.

Links:

  • rundll32 at ss64.com
  • at Microsoft
  • rundll at robvanderwoude.com
  • dx21.com — lists rundll32 examples

SC[edit | edit source]

Controls Windows services, supporting starting, stopping, querying and more. Windows services are process-like things. A Windows service is either hosted in its own process or it is hosted in an instance of svchost.exe process, often with multiple services in the same instance. Processor time use of a particular service can be found using freely downloadable Process Explorer from Sysinternals, by going to properties of a service and then Threads tab. Another command capable of controlling services is NET. TASKLIST can list hosted services using /svc switch.

Examples:

  • sc start wuauserv
    • Starts wuauserv service.
  • sc stop wuauserv
  • sc query wuauserv
  • sc query
    • Outputs information about all services.
  • sc config SysMain start= disabled
    • Make sure SysMain service is disabled after start. SysMain is the SuperFetch service, causing repeated harddrive activity by trying to guess which programs to load into RAM in case they will be used, and loading them. Notice the mandatory lack of space before = and the mandatory space after =.

Links:

  • sc at ss64.com
  • Windows 7 Services at ss64.com
  • sc at Microsoft

SCHTASKS[edit | edit source]

Schedules a program to be run at a certain time, more powerful than AT.

Links:

  • schtasks at ss64.com
  • schtasks at Microsoft

SETX[edit | edit source]

Like SET, but affecting the whole machine rather than the current console or process. Not available in Windows XP; available in Windows Vista and later.

Links:

  • setx at ss64.com
  • setx at Microsoft, Windows Server 2008, Windows Vista

SHUTDOWN[edit | edit source]

Shuts down a computer, or logs off the current user.

Examples:

  • shutdown /s
    • Shuts down the computer.
  • shutdown /s /t 0
    • Shuts down the computer with zero delay.
  • shutdown /l
    • Logs off the current user.

Links:

  • shutdown at ss64.com
  • shutdown at Microsoft

SORT[edit | edit source]

Sorts alphabetically, from A to Z or Z to A, case insensitive. Cannot sort numerically: if the input contains one integer per line, «12» comes before «9».

Examples:

  • sort File.txt
    • Outputs the sorted content of File.txt.
  • sort /r File.txt
    • Sorts in reverse order, Z to A.
  • dir /b | sort

Links:

  • sort at ss64.com
  • sort at Microsoft

SUBST[edit | edit source]

Assigns a drive letter to a local folder, outputs current assignments, or removes an assignment.

Examples:

  • subst p: .
    • Assigns p: to the current folder.
  • subst
    • Outputs all assignments previously made using subst.
  • subst /d p:
    • Removes p: assignment.

Links:

  • subst at ss64.com
  • subst at Microsoft

SYSTEMINFO[edit | edit source]

Shows configuration of a computer and its operating system.

Links:

  • systeminfo at ss64.com
  • systeminfo at Microsoft

TASKKILL[edit | edit source]

Ends one or more tasks.

Examples:

  • taskkill /im AcroRd32.exe
    • Ends all process with the name «AcroRd32.exe»; thus, ends all open instances of Acrobat Reader. The name can be found using tasklist.
  • taskkill /f /im AcroRd32.exe
    • As above, but forced. Succeeds in ending some processes that do not get ended without /f.
  • tasklist | find «notepad»

    taskkill /PID 5792

    • Ends the process AKA task with process ID (PID) of 5792; the assumption is you have found the PID using tasklist.

Links:

  • taskkill at ss64.com
  • taskkill at Microsoft

TASKLIST[edit | edit source]

Lists tasks, including task name and process id (PID).

Examples:

  • tasklist | sort
  • tasklist | find «AcroRd»
  • tasklist | find /C «chrome.exe»
    • Outputs the number of tasks named «chrome.exe», belonging to Google Chrome browser.
  • tasklist /svc | findstr svchost
    • Outputs Windows services hosted in svchost.exe processes alongside the usual information abot the process.

Links:

  • tasklist at ss64.com
  • tasklist at Microsoft

TIMEOUT[edit | edit source]

Waits a specified number of seconds, displaying the number of remaining seconds as time passes, allowing the user to interrupt the waiting by pressing a key. Also known as delay or sleep. Available in Windows Vista and later.

Examples:

  • timeout /t 5
    • Waits for five seconds, allowing the user to cancel the waiting by pressing a key.
  • timeout /t 5 /nobreak
    • Waits for five seconds, ignoring user input other than Control + C.
  • timeout /t 5 /nobreak >nul
    • As above, but with no output.

Workaround in Windows XP:

  • ping -n 6 127.0.0.1 >nul
    • Waits for five seconds; the number after -n is the number of seconds to wait plus 1.

Perl-based workaround in Windows XP, requiring Perl installed:

  • perl -e «sleep 5»
    • Waits for 5 seconds.

Links:

  • timeout at ss64.com
  • timeout at Microsoft
  • How to wait in a batch script? at stackoverflow.com
  • Sleeping in a batch file at stackoverflow.com

TREE[edit | edit source]

Outputs a tree of all subdirectories of the current directory to any level of recursion or depth. If used with /F switch, outputs not only subdirectories but also files.

Examples:

  • tree
  • tree /f
    • Includes files in the listing, in addition to directories.
  • tree /f /a
    • As above, but uses 7-bit ASCII characters including «+», «-» and \» to draw the tree.

A snippet of a tree using 8-bit ASCII characters:

├───winevt
│   ├───Logs
│   └───TraceFormat
├───winrm

A snippet of a tree using 7-bit ASCII characters:

+---winevt
|   +---Logs
|   \---TraceFormat
+---winrm

Links:

  • tree at Microsoft

WHERE[edit | edit source]

Outputs one or more locations of a file or a file name pattern, where the file or pattern does not need to state the extension if it listed in PATHEXT, such as .exe. Searches in the current directory and in the PATH by default. Does some of the job of «which» command of some other operating systems, but is more flexible.

Available on Windows 2003, Windows Vista, Windows 7, and later; not available on Windows XP. An alternative to be used with Windows XP is in the examples below.

Does not find internal commands, as there are no dot exe files for them to match.

Examples:

  • where find
    • Outputs the location of the find command, possibly «C:\Windows\System32\find.exe». The .exe extension does not need to be specified as long as it is listed in PATHEXT, which it is by default.
    • If there are more find commands in the path, outputs paths to both. In some situations, it can output the following:

      C:\Windows\System32\find.exe
      C:\Program Files\GnuWin32\bin\find.exe

  • for %i in (find.exe) do @echo %~$PATH:i
    • Outputs the location of «find.exe» on Windows XP. The name has to include «.exe», unlike with the where command.
  • where /r . Tasks*
    • Searches for files whose name matches «Task*» recursively from the current folder. Similar to «dir /b /s Tasks*». The /r switch disables search in the folders in PATH.
  • where *.bat
    • Outputs all .bat files in the current directory and in the directories that are in PATH. Thus, outputs all .bat files that you can run without entering their full path.
  • where ls*.bat
    • As above, constraining also the beginning of the name of the .bat files.
  • where ls*
    • As above, but with no constraint on the extension. Finds lsdisks.bat, lsmice.pl, and lsmnts.py if in the current directory or in the path.
  • where *.exe *.com | more
    • Outputs countless .exe and .com files in the path and in the current folder, including those in C:\Windows\System32.
  • where $path:*.bat
    • Outputs .bat files in the path but not those in the current folder unless the current folder is in PATH. Instead of path, another environment variable containing a list of directories can be used.
  • where $windir:*.exe
    • Outputs .exe files found in the folder stated in WINDIR environment variable.
  • where $path:*.bat $windir:*.exe
    • A combination is possible. Outputs all files matching either of the two queries.
  • where /q *.bat && echo Found
    • Suppresses both standard and error output, but sets the error level, enabling testing on it. The error level is set either way, with or without /q.

Links:

  • where at ss64.com
  • where at Microsoft
  • Is there an equivalent of ‘which’ on windows?

WMIC[edit | edit source]

Starts Windows Management Instrumentation Command-line (WMIC), or with arguments given, passes the arguments as commands to WMIC. Not in Windows XP Home. For more, type «wmic /?».

Examples:

  • wmic logicaldisk get caption,description
    • Lists drives (disks) accessible under a drive letter, whether local hard drives, CD-ROM drives, removable flash drives, network drives or drives created using #SUBST.
  • wmic logicaldisk get /format:list
  • wmic logicaldisk get /format:csv
  • wmic
    Control + C
    • Enters wmic and then interrupts it. A side effect is that the console buffer becomes very wide, and the screen becomes horizontally resizable with the mouse as a consequence. This is the result of wmic setting a high number of columns of the console, which you can verify using mode con. You can achieve a similar result by typing mode 1500. See also #MODE.
  • wmic datafile where name=»C:\\Windows\\System32\\cmd.exe» get Version /value
    • Outputs the version of the cmd.exe, which should be close to the Windows version.

Links:

  • wmic at ss64.com
  • wmic at Microsoft

XCOPY[edit | edit source]

Copies files and directories in a more advanced way than COPY, deprecated in Windows Vista and later in favor of ROBOCOPY. Type xcopy /? to learn more, including countless options.

Examples:

  • xcopy C:\Windows\system
    • Copies all files, but not files in nested folders, from the source folder («C:\Windows\system») to the current folder.
  • xcopy /s /i C:\Windows\system C:\Windows-2\system
    • Copies all files and folders to any nesting depth (via «/s») from the source folder («C:\Windows\system») to «C:\Windows-2\system», creating «Windows-2\system» if it does not exist (via «/i»).
  • xcopy /s /i /d:09-01-2014 C:\Windows\system C:\Windows-2\system
    • As above, but copies only files changed on 1 September 2014 or later. Notice the use of the month-first convention even if you are on a non-US locale of Windows.
  • xcopy /L /s /i /d:09-01-2014 C:\Windows\system C:\Windows-2\system
    • As above, but in a test mode via /L (list-only, output-only, display-only). Thus, does not do any actual copying, merely lists what would be copied.
  • xcopy /s /i C:\Windows\system\*.dll C:\Windows-2\system
    • As one of the examples above, but copies only files ending in .dll, including those in nested folders.

Links:

  • xcopy at ss64.com
  • xcopy at Microsoft

[edit | edit source]

  • PowerShell — a modern technology for scripting Windows operating systems
  • VBScript Programming — a legacy Windows scripting technology whose syntax resembles Visual Basic

External links[edit | edit source]

  • Windows CMD Commands at ss64.com — licensed under Creative Commons Attribution-Non-Commercial-Share Alike 2.0 UK: England & Wales[1], and thus incompatible with CC-BY-SA used by Wikibooks
  • Windows XP — Command-line reference A-Z at microsoft.com
  • Windows Server 2008R2 — Command-Line Reference at microsoft.com
  • Windows Server 2012R2 — Command-Line Reference at microsoft.com
  • Windows Server 2016 — Windows Commands at microsoft.com
  • The FreeDOS HTML Help at fdos.org — a hypertext help system for FreeDOS commands, written in 2003/2004, available under the GNU Free Documentation License
  • Category:Batch File, rosettacode.org
  • Cmd.exe, wikipedia.org
  • Batch file, wikipedia.org

Человек на черном фоне
Всем нам, приходилось сталкиваться с рутинными задачами изо дня в день на работе, и не только.

Например:

  1. Необходимо раз в месяц удалять все файлы из папки обмен.
  2. «Добросовестные» пользователи не выключают компьютеры, и уходят домой, а вам потом по голове дают за то, что компьютер работал, и жрал электроэнергию.
  3. У вас 20 человек в кабинете, принтер один и всем нужно выводить на него печать. Можно написать батник закинуть его в обмен зайти в кабинет и всем пользователям сказать зайдите туда-туда, нажмите то-то, и можете печатать, а если у Вас есть active directory, то можно распространить с помощью неё.

Можно еще привести множество примеров обыкновенных задач, на которые лучше не тратить свое время, а автоматизировать процесс. Сегодня хочу рассказать, как пишутся элементарные bat скрипты.

Давайте разберем первый пример:

Необходимо раз в месяц удалять все файлы из папки обмен.

  1. Открываем WordPad, блокнот не подойдет, он не умеет сохранять в кодировке 866.
  2. Пишем:

del /q “c:\обмен\”
pause
Команда del- удаляет файлы, ключ q говорит, удалять файлы без подтверждения пользователя, дальше идет путь до папки обмен, команда pause – это для вашего удобства, что бы окно не закрылось автоматически после выполнения работы скрипта, можно её не писать.

  1. Дальше выбираем Файл => Сохранить как => в строке Имя файла пишем допустим, del_obmen.bat, жмем Ок, запускаем и наслаждаемся.

Второй пример:

«Добросовестные» пользователи не выключают компьютеры, и уходят домой, а вам потом по голове дают за то, что компьютер работал, и жрал электроэнергию.

  1. Открываем WordPad.
  2. Пишем:

SHUTDOWN /s
Пояснения я думаю ни к чему.
3. Дальше выбираем Файл => Сохранить как => в строке Имя файла пишем допустим, shutdown.bat, жмем Ок, запускаем и наслаждаемся.
4. Дальше открываем панель управления => планировщик заданий, создаем задание в 20 00, думаю понятно для чего.

Третий пример:

У вас 20 человек в кабинете, принтер один и всем нужно выводить на него печать. Можно написать батник закинуть его в обмен зайти в кабинет и всем пользователям сказать зайдите туда-туда, нажмите то-то, и можете печатать, а если у Вас есть active directory, то можно распространить с помощью неё.

  1. Открываем WordPad.
  2. Пишем:

start \\192.168.0.37\SamsungU
Start – запуск, \\192.168.0.37 – ip адрес, \SamsungU – имя принтера.

Если у вас ip адреса раздаются по DHCP, то лучше ввести не ip адрес, а имя компьютера. 

3. Дальше выбираем Файл => Сохранить как => в строке Имя файла пишем допустим, print.bat, жмем Ок, запускаем и наслаждаемся.

 

Основные команды, которые используются для написания батников:

ASSOC — Отображает или модифицирует связи расширений файлов
AT — Планирует команды и программы для выполнения на компьютере.
ATTRIB — Отображает или изменяет атрибуты файла.
BREAK — Устанавливает или отменяет проверку комбинации [Ctrl+C].
CACLS — Отображает или модифицирует списки управления доступом (ACLs) для файлов.
CALL — Вызывает один *.BAT-файл из другого.
CD — Отображает имя или изменяет имя текущей директории.
CHCP — Отображает или устанавливает номер активной кодовой страницы.
CHDIR — Отображает имя или изменяет имя текущей директории.
CHKDSK — Проверяет диск и отображает отчет о состоянии.
CLS — Очищает экран.
CMD — Стартует новый экземпляр интерпретатора команд Windows NT.
COLOR — Устанавливает цвета по умолчанию для переднего и заднего плана консоли.
COMMAND — Стартует новую копию интерпретатора команд Windows.
COMP — Сравнивает содержимое двух файлов или установки файлов.
COMPACT — Отображает или видоизменяет сжатие файлов на патрициях Windows NT(NTFS).
CONVERT — Конвертирует FAT томов к формату файловой системы Windows NT(NTFS). Вы не можете конвертировать текущий диск.
COPY — Копирует один или больше файлов на другое место.
CTTY — Изменяет терминальное устройство, используемое для управления вашей системой.
DATE — Отображает или устанавливает дату.
DEL — Удаляет один или более файлов.
DEBUG — Выполняет отладку, тестирование программ и редактирование инструментальных средств.
DIR — Отображает список файлов и поддиректорий в директории.
DISKCOMP — Сравнивает содержимое двух дискет.
DISKCOPY — Копирует содержимое одной дискеты на другую.
DOSKEY — Редактирует командные строки, восстанавливает команды Windows и создает макрос.
ECHO — Отображает сообщения, или включает/выключает вывод команд.
EMM386 — Включает/выключает поддержку расширенной памяти EMM386.
ENDLOCAL — Заканчивает локализацию изменений окружающей среды в *.BAT-файле.
ERASE — Удаляет один или более файлов.
EXIT — Прекращает выполнение программы «CMD.EXE» (интерпретатор команд).
EXTRACT — Средство извлечения информации из CAB — файлов.
FC — Сравнивает два файла или установки файлов, и отображает различие между ними.
FIND — Ищет текстовую строку в файле или файлах.
FINDSTR — Поиск строк в файлах.
FOR — Выполняет указанную команду для каждого файла в наборе файлов.
FORMAT — Форматирует диск для использования с Windows.
FTYPE — Отображает или модифицирует типы файлов, используемых в связях расширений.
GOTO — Направляет интерпретатор команд Windows NT к помеченной строке в *.BAT-файле.
GRAFTABL — Способность Windows отображать символы псевдографики, вставленные в графическом режиме.
HELP — Обеспечивает информацию Help для команд Windows.
IF — Выполняет обработку условия в *.BAT-файле.
KEYB — Конфигурирует клавиатуру для заданного языка.
LABEL — Создает, изменяет, или удаляет метку тома на диске.
LOADHIGH(LH) — Загружает программу в верхние адреса памяти.
MD — Создает директорию.
MEM — Отображает величину используемой и свободной памяти в вашей системе.
MKDIR — Создает директорию.
MODE — Конфигурирует системное устройство.
MORE — Отображает вывод одного экрана за раз.
MOVE — Перемещает один или более файлов из одной директории в другую на том же диске.
NETSTAT — Отображает статистики протоколов и текущих сетевых соединений TCP/IP.
NLSFUNC — Загружает информацию, специфическую для страны.
PATH — Отображает или устанавливает путь поиска для выполняемых файлов.
PAUSE — Приостанавливает обработку *.BAT-файла и отображает сообщение.
POPD — Восстанавливает предыдущее значение текущей директории, сохраненной по PUSHD.
PRINT — Печатает текстовый файл.
PROMPT — Изменяет подсказку к командам Windows.
PUSHD — Сохраняет текущую директорию, потом изменяет.
RD — Удаляет директорию.
RECOVER — Восстанавливает читаемую информацию с плохого или дефектного диска.
REM — Записывает комментарии (примечания) в *.BAT-файлы или CONFIG.SYS.
REN — Переименует файл или файлы.
RENAME — Переименует файл или файлы.
REPLACE — Заменяет файлы.
RESTORE — Восстанавливает файлы, которые были архивированы с использованием команды BACKUP.
RMDIR — Удаляет директорию.
SET — Отображает, устанавливает или удаляет переменные среды Windows.
SETLOCAL — Начинает локализацию изменений среды в *.BAT-файле.
SETVER — Устанавливает номер версии MS-DOS, который Windows сообщает программе.
SHIFT — Сдвигает позицию замещаемых параметров в *.BAT-файле.
SMARTDRV — Инсталлирует и конфигурирует утилиту кэширования диска SMART — драйва.
SORT — Сортирует входной поток.
START — Стартует отдельное окно для выполнения указанной программы или команды.
SUBST — Связывает путь с литерой диска.
SYS — Копирует файлы системы MS-DOS и интерпретатор команд на указанный вами диск.
TIME — Отображает или устанавливает системное время.
TITLE — Устанавливает заголовок окна для сеанса «CMD.EXE» .
TREE — Графически отображает структуру директория в драйве или путь.
TYPE — Отображает содержимое текстового файла.
VER — Отображает версию Windows.
VERIFY — Сообщает Windows, проверять ли правильность записи файлов на диск.
VOL — Отображает метку дискового тома и серийный номер.
XCOPY — Копирует файлы и деревья директории.
Также есть очень хороший форум, где куча готовых скриптов.
Командная строка, батники\сценарии (bat, cmd) 

Предыдущая

Windowsntvdm.exe загрузка процессора на 100%

Следующая

WindowsОбзор Windows 8

Командная строка Windows предоставляет мощный инструмент для автоматизации задач и выполнения различных операций на вашем компьютере. С помощью скриптов, вы можете написать последовательность команд, которые будут выполняться автоматически и эффективно. Это особенно полезно, когда вам нужно выполнить одну и ту же операцию множество раз, или когда вы хотите создать сложный сценарий выполнения команд.

В этом руководстве мы рассмотрим основные концепции написания скриптов для командной строки Windows, и покажем вам, как создать свои первые скрипты. Мы начнем с простых примеров, постепенно углубляясь в более сложные сценарии. Вы узнаете о различных командах и операторах, и научитесь использовать их для автоматизации различных задач.

В ходе чтения этого руководства вы познакомитесь с такими концепциями как переменные, условные операторы, циклы, функции и многое другое. Вы научитесь использовать эти концепции совместно с командами командной строки Windows, что позволит вам записывать сложные скрипты для автоматизации повседневных задач.

Не волнуйтесь, если вы не имеете опыта программирования. Это руководство предназначено для начинающих и облегчит вам путь к созданию собственных скриптов для командной строки Windows.

Содержание

  1. Что такое скрипты для командной строки и как они работают
  2. Основы написания скриптов для командной строки
  3. Выбор языка программирования и настройка среды

Что такое скрипты для командной строки и как они работают

Скрипты для командной строки в Windows используются для автоматизации и упрощения повседневных задач, таких как копирование файлов, переименование папок, запуск программ и многое другое. С помощью скриптов можно написать набор команд, которые будут выполняться последовательно, что позволяет значительно сэкономить время и усилия пользователя.

Скрипты для командной строки в Windows работают на основе командного интерпретатора, такого как Command Prompt (cmd.exe) или PowerShell. Командный интерпретатор выполняет каждую команду в скрипте по очереди, следуя заданной последовательности. Это позволяет программистам и системным администраторам создавать сложные скрипты с условными операторами, циклами и другими контрольными конструкциями для автоматизации сложных задач.

Скрипты для командной строки в Windows могут быть написаны с использованием различных языков программирования, таких как Batch-файлы (.bat), VBScript (.vbs) или PowerShell-скрипты (.ps1). Каждый из этих языков имеет свои особенности и возможности, и выбор конкретного языка зависит от требований и опыта пользователя.

С помощью скриптов для командной строки в Windows вы можете автоматизировать широкий спектр задач, повышая свою продуктивность и сокращая время, затрачиваемое на выполнение рутинных операций. Изучение и использование скриптов для командной строки является важным навыком для программистов, системных администраторов и всех, кто работает с операционной системой Windows.

Основы написания скриптов для командной строки

Скрипты для командной строки Windows позволяют автоматизировать выполнение различных задач, что существенно упрощает работу со множеством файлов и папок, а также позволяет сэкономить время и усилия.

Для начала написания скриптов вам понадобится текстовый редактор, такой как Notepad++ или Sublime Text. Далее следует ознакомиться с основными командами операционной системы Windows, чтобы понять, какие команды можно использовать в скриптах.

Одной из самых часто используемых команд является команда cd (от англ. Change Directory). Данная команда позволяет перейти в другую директорию. Например, чтобы перейти в папку Documents, напишите следующий код:

cd Documents

Для создания новой папки используется команда mkdir (от англ. Make Directory). Например, чтобы создать папку с названием «NewFolder», напишите следующий код:

mkdir NewFolder

Для копирования файла или папки используется команда copy. Например, чтобы скопировать файл «file.txt» в папку «NewFolder», напишите следующий код:

copy file.txt NewFolder

Кроме того, вы можете использовать различные операторы, такие как операторы перенаправления вывода (> и >>), операторы циклов (for и while) и условные операторы (if и else), чтобы реализовать более сложные функции в своих скриптах.

После написания скрипта сохраните его с расширением .bat (например, script.bat) и запустите его в командной строке Windows. Вы увидите, как скрипт будет выполняться, выводя результаты в командную строку.

Запомните, что написание скриптов для командной строки может быть сложным на начальных этапах, поэтому рекомендуется практиковаться и искать дополнительную информацию для лучшего понимания и работы с командной строкой Windows.

Выбор языка программирования и настройка среды

Язык программирования Описание
Batch-скриптинг Простой язык программирования, который предоставляет базовые команды для автоматизации задач в Windows. Несложен в изучении и может быть полезен, если вам нужно выполнить простые задачи.
PowerShell Мощный и гибкий язык программирования, который предоставляет широкие возможности для работы с командной строкой. Он поддерживает множество команд и расширений, что делает его идеальным выбором для сложных задач.
VBScript Язык сценариев, основанный на Visual Basic, идеально подходит для автоматизации задач в Windows. Он поддерживает различные функции, объекты и методы, делая его легко расширяемым и гибким.
Python Универсальный язык программирования, широко используемый для различных задач, в том числе и для написания скриптов в Windows. Он имеет простой и понятный синтаксис, великолепную документацию и множество библиотек, что делает его привлекательным выбором для новичков и профессионалов.

После выбора языка программирования вы должны настроить среду разработки, чтобы быть готовыми к написанию и выполнению скриптов. В зависимости от выбранного языка, вы можете использовать различные инструменты и редакторы кода. Некоторые из них включают:

  • Command Prompt — стандартная командная строка Windows, которая может использоваться для написания и выполнения скриптов на Batch-скриптинге и PowerShell.
  • Windows PowerShell ISE — интегрированная среда разработки PowerShell, предоставляющая более удобный интерфейс для написания и отладки скриптов.
  • Notepad++ — легкий текстовый редактор, который поддерживает множество языков программирования, включая Batch-скриптинг, PowerShell и VBScript.
  • Visual Studio Code — мощный и расширяемый редактор кода, который поддерживает различные языки программирования, включая PowerShell и Python.

Выбор среды разработки зависит от ваших предпочтений и опыта в программировании. Вы можете выбрать любую из предложенных опций или использовать другую среду разработки, которая лучше подходит для ваших потребностей.

Несмотря на простоту использования графического интерфейса пользователя Windows (GUI), интерфейс командной строки остается полезным способом выполнить многие задачи обслуживания, конфигурации и диагностики. Многие наиболее важные инструменты диагностики, такие как ping, tracert и nslookup доступны только из командной строки, если конечно Вы не купили сторонние графические оболочки для выполнения тех же самых функций. И хотя термин «bat-файл» может вернуть воспоминания о не комфортной старой рабочей среде MS-DOS, bat-файлы и программные скрипты остаются мощными инструментами, предоставляющий полезный способ инкапсулировать в себе общие функции управления. Утилиты командной строки, bat-файлы и скрипты, основанные на WSH (Windows Script Host) совместно предоставляют полный набор строительных блоков, из которых Вы можете создать высокоуровневые утилиты для выполнения сложных задач.

Примечание: это перевод части книжки «Microsoft® Windows 7 In Depth» [1], авторы Brian Knittel, Robert Cowart, касающейся настройки и конфигурирования рабочей среды с командной строкой. Более полное руководство с примерами можно получить в книге «Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools», автор Brian Knittel.

[Командная строка Windows 7]

Открыть окно приглашения командной строки (Command Prompt), где Вы сможете вводить команды и получать от них сообщения о результатах работы, кликните на кнопку Start (Пуск) -> All Programs (Все программы) -> Accessories (Стандартные) -> Command Prompt (Командная строка). Есть альтернативный способ — кликните Start и в поле Search (поиск) введите cmd. Посте того, как будет найден файл cmd.exe, нажмите клавишу Enter.

Примечание: похожим образом запускается интерпретатор команд и на Windows XP, только cmd нужно вводить в приглашении «Выполнить…», после чего нажать на OK.

После этой операции откроется окно Command Prompt, как это показано на рис. 29.1.

command line and automation tools fig29 1

Рис. 29.1. Окно командной строки — ворота в мир инструментов мощной системы управления Windows.

Главное отличие стандартного приложения Windows с графическим интерфейсом и программой командной строки (по терминологии Windows последняя называется консольной программой) — программа командной строки не использует графический вывод информации или выпадающие меню. Вместо этого Вы вводите команды в окне консоли Command Prompt, чтобы указать системе Windows что-нибудь сделать, и программы в ответ возвращают Вам некую информацию в текстовом, удобочитаемом для человека виде. Каждая командная строка начинается с имени программы, которую Вы хотите запустить, за которой идет дополнительная информация, которая называется аргументами. Аргументы говорят программе, что конкретно нужно сделать (уточняют действие и дают некие входные данные).

Совет: если Вы планируете регулярно использовать окно Command Prompt, то для быстрого доступа к нему прикрепите его к панели задач. Для этого сделайте правый клик на иконку Command Prompt в панели задач, и выберите «Pin This Program to Taskbar» (прикрепить эту программу к Панели Задач). Также можно перетащить ярлычок окна Command Prompt на Рабочий Стол Windows (Desktop).

Также можно открыть окно Command Prompt из Проводника (Windows Explorer). Для этого в Проводнике удерживайте клавишу Shift, и выполните правый клик на имени нужной папки, после чего выберите в контекстном меню «Open Command Window Here». Откроется окно командной строки (Command Prompt) и выбранная папка будет для этого окна команд папкой по умолчанию.

Расширения имен для запускаемых файлов. У большинства файлов Windows есть так называемое расширение имени файла — суффикс, следующий после точки, который обычно состоит из трех английских букв. Изначально это расширение предназначено для определения типа файла. Некоторые из расширений обозначают запускаемые файлы программ разного вида, это такие расширения файлов, как .exe, .bat, and .vbs (большинство общих расширений файлов программ показано в таблице 29.1). Для выполняемой команды не обязательно вводить имя программы полностью, достаточно ввести имя без расширения. Например, для утилиты Ping можно вести команду ping (с опциями, указывающий адрес для пинга) вместо ping.exe. В случае ввода команды без расширения операционная система Windows предполагает, что это программа, и сама добавляет расширение файла, когда ищет программу по своему внутреннему списку поиска, и если такая программа найдена, то запускает её. После этого запущенная программа получает список аргументов, указанный в строке команды после имени программы, и программа должна соответствующим образом обработать эти аргументы.

Таблица 29.1. Типовые расширения имени для выполняемых файлов.

Расширение Тип программы
.bat, .cmd bat-файл, скрипт из набора команд.
.com Архаичная программа MS-DOS.
.exe GUI-программа Windows, консольная программа, или программа MS-DOS (Windows определяет тип программы по содержимому файла).
.js Скрипт, написанный на языке JavaScript.
.msc Консоль управления Windows (Microsoft Management Console).
.vbs Скрипт, написанный на языке VBScript.

Пути поиска программ, переменная окружения PATH. Есть очень важный момент, касающийся запуска программ в системе Windows. Когда Вы вводите командную строку, то при её запуске система Windows ищет указанную программу по своему списку каталогов (папок). Этот список называется «путь поиска» (search path), и все перечисленные каталоги поиска находятся в переменной окружения PATH, и полный список расширений, которые Windows просматривает, находится в переменной окружения PATHEXT. По умолчанию путь поиска Windows 7 включает следующие папки:

C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0

Это означает, что любой файл программы, bat-файл или скрипт, если он находится в одной из этих перечисленных папок, может быть запущен из любого текущего каталога простым вводом имени (даже без расширения имени файла). Таким способом можно запустить как встроенные программы Windows (ping, calc, explorer и т. д.), так и любые программы командной строки. Например, просто введите команду notepad для запуска программы текстового редактора Блокнот (Notepad).

Если Вы создаете свои собственные bat-файлы, скрипты или программы, то хорошей идеей будет создать для них отдельную папку, и поместить путь до этой папки в пути поиска (переменную окружения PATH). Об этом будет рассказано в разделе «Установка значений переменных окружения».

[Запуск команд с повышением привилегий]

Некоторые утилиты командной строки (а иногда программы с графическим интерфейсом) требуют для своей корректной работы повышенных административных привилегий (права для пользователей и групп задаются через инструмент User Account Control). Чтобы запустить программу командной строки с повышенными привилегиями, Вы должны запустить её из командной строки (окна Command Prompt), которая сама имеет повышенные права.

Чтобы открыть окно Command Prompt с повышенными административными привилегиями, кликните Start (Пуск) -> All Programs (Все программы) -> Accessories (Стандартные). Затем сделайте правый клик на Command Prompt, и выберите Run As Administrator (запуск от имени Администратора). Или, если у Вас иконка Command Prompt прикреплена к Вашей панели задач (taskbar), есть 2 быстрых способа открыть приглашение командной строки с повышенными привилегиями:

• Сделайте правый клик на иконку, правый клик на метку Command Prompt в выпадающем меню Jump List, и выберите Run As Administrator.
• Удерживайте комбинацию клавиш Shift+Ctrl и кликните на иконку.

Предупреждение: будьте очень осторожны, когда используете командную строку с административными привилегиями. Любая команда, которая будет запущена оттуда, будет сразу работать с повышенными правами, и система UAC больше не будет предупреждать о том, что программа запускается с административными правами. Неосторожные действия администратора могут привести к повреждению системы Windows. Это также касается и запуска с повышенными привилегиями графических (GUI) программ Windows — например, если Вы введете в Windows 7 команду optionalfeatures, то откроется диалог настроек опций Windows Features, и Вы не получите окошка предупреждения перед запуском этого диалога.

Для безопасной работы не используйте для обычной повседневной работы окно Command Prompt с повышенными привилегиями. Используйте это только для специальных задач, требующих административных привилегий, и после завершения выполнения этих задач закройте окно Command Prompt с повышенными привилегиями.

Если Вы хотите то можете настроить ярлычок Command Prompt, прикрепленный к Панели Задач (taskbar), чтобы он по умолчанию запускался с повышенными привилегиями. Сделайте правый клик на иконку и выберите Properties (Свойства). На закладке Shortcut (Ярлык) кликните на кнопку Advanced (Дополнительно), и кликните Run As Administrator (запуск от имени администратора). Переименуйте ярлычок, чтобы он ясно обозначал запуск приглашения командной строки с повышенными привилегиями.

[Как научиться работать с программами командной строки]

Как узнать, какие программы имеются и как их использовать? Для этого обратитесь к документации по рабочему окружению командной строки (command-line environment). По какой-то причине Microsoft больше не предоставляет эту информацию в разделе справки «Help and Support», но сегодня можно найти нужную информацию онлайн, в сети Интернет, и многие программы могут сами предоставить о себе необходимую информацию по использованию. Также Вы можете обратиться к книге «Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools». Для общего поиска в Интернете запустите поисковую систему Google, и найдите список команд по первым буквам A–Z для Windows Server 2008 или Windows Server 2003. Большинство из перечисленных там команд имеются в Windows 7. Пример такой строки для поиска [2]:

site:microsoft.com command line a-z windows server

Чтобы получить дополнительную информацию по интересующей команде, попробуйте следующие методы в том порядке, в каком они перечислены ниже — на примере получения информации по команде rasdial. Та же техника может быть применена и к любой другой интересующей команде Windows.

• Большинство утилит командной строки выводит подсказку по своему использованию при добавлении опции /? к своей командной строке. Например, чтобы получить справку по команде rasdial, введите команду rasdial /? и нажмите клавишу Enter. Некоторые утилиты для вывода справки используют опцию -?, —? —h или -help.

• Если выведенный текст подсказки слишком большой, и не умещается в одном экране консоли, то для его чтения по частям можно использовать следующие методы:

— с помощью вертикальной линейки прокрутки окна Command Prompt можно прокручивать выведенный текст вверх или вниз.
— нажмите кнопку F3, чтобы вспомнить введенную команду, и добавьте в конец командной строки | more, после чего нажмите Enter. Это действие перенаправит вывод программы в команду more, которая отображает переданный на вход текст по одному экрану. Для перехода к следующему экрану нажимайте Enter.

• Введите команду help rasdial. Если выведенного текста будет очень много, используйте для чтения по частям технику, показанную выше.

• Откройте браузер Internet Explorer и введите rasdial в окне поиска (Search window). Также можно воспользоваться поисковой системой Google, если ввести в строке поиска:

site:microsoft.com rasdial

Не каждый из перечисленных способов получения подсказки может хорошо сработать для любой команды, но как минимум один из них даст объяснение, что команда делает и с какими опциями командной строки её нужно использовать, а также примеры такого использования. Опции командной строки для операционных систем Windows 7, Windows Vista, Windows XP, Windows Server 2003 и Windows Server 2008 почти всегда одни и те же, так что если Вы ищете информацию для Windows 7, то скорее всего подойдет информация, относящаяся и к другим версиям Windows.

[Копирование и вставка в окне командной строки]

Хотя можно использовать оператор перенаправления (> или >>) для вывода текста утилитами командной строки в текстовые файлы, также Вы можете использовать функцию копирования и вставки буфера обмена (cut/paste, copy/paste) для ввода текста как в окно командной строки, так и для того, чтобы брать оттуда текст.

Чтобы вставить (paste) текст в окно командной строки в позицию курсора, сделайте клик на системном меню окна командной строки (верхний левый угол окна) и выберите Edit (Редактировать) -> Paste (Вставить). Это можно проще всего сделать без использования мыши: нажмите клавиши Alt+Spacebar (откроется системное меню) и введите E P (для английской версии системы Windows) или И а (для русской версии). Для навигации по пунктам меню можно использовать клавиши со стрелками, для выбора пункта клавишу Enter.

Чтобы скопировать текст (copy) из окна командной строки в Буфер Обмена (Clipboard), кликните на системное меню и выберите Edit (Редактировать) -> Mark (Пометить). Альтернативно нажмите Alt+Spacebar E M (для английской версии) или Alt+Spacebar И П (для русской версии). После этого мышью выделите в окне командной строки прямоугольную область с нужным текстом и нажмите Enter. Это действие скопирует выделенный текст в Буфер Обмена.

По умолчанию мышь не будет выбирать текст, пока не будет выбрана функция Mark (Пометить). Это упрощает работу с программами MS-DOS, которые не рассчитаны на использование мыши. Это можно изменить, если войти в системное меню (кликом мыши или нажатием Alt+Spacebar), выбрать Defaults (Умолчания), поставить галочку Quick Edit (Выделение мышью). Когда эта опция разрешена, Вы можете использовать мышь для моментального копирования текста из консоли в Буфер Обмена, без специальной активации функции Mark (Пометить) через системное меню.

[Установка значений переменных окружения]

Использование системных переменных (Environment Variables) — один из способов, которым Windows управляет некоторыми настройками, и обменивается информацией с пользователем. В частности, эти переменные показывают, где находятся системные папки Windows, какие пути используются при поиске запускаемых программ, где находится папка для временных файлов. Также в переменных окружения хранятся и другие настройки, которые влияют на работу программ и производительность системы. Дополнительно переменные окружения могут использоваться в bat-файлах или скриптах для хранения информации о выполняемой работе.

В Windows 7 начальное значение переменных окружения, которые будут определены при запуске каждого окна Command Prompt, когда оно открыто первый раз, настраивается с помощью диалога GUI, показанного на рис. 29.2.

command line and automation tools fig29 2

Рис. 29.2. Просмотр переменных окружения текущего пользователя (верхняя часть окна диалога) и всех пользователей системы (нижняя часть диалога). Список переменных окружения, который может быть определен для каждого пользователя отдельно (в верхней части окна диалога) может добавлять или переназначать список переменных окружения уровня системы (нижняя часть диалога).

Обратите внимание, что этот диалог поделен на 2 секции, System Variables (системные переменные) и User Variables (переменные пользователя). Нижняя секция System Variables определяет глобальные переменные окружения, которые установлены для каждой учетной записи пользователя. Верхняя секция User Variables определяет дополнительные переменные окружения по умолчанию для текущего аккаунта пользователя, который сейчас работает в систем. Они могут добавлять какие-либо переменные к System Variables, либо переназначать эти переменные.

Чтобы открыть указанное окно диалога для просмотра и редактирования переменных окружения, если Вы пользователь компьютера с правами администратора, используйте одни из двух следующих методов:

• Если у Вас открыто окно командной строки Command Prompt, то введите команду start sysdm.cpl и нажмите Enter. Вам может понадобиться подтвердить запрос подтверждения системы UAC. Затем выберите закладку Advanced (Дополнительно), и кликните на кнопку Environment Variables (Переменные среды).
• Альтернативно кликните на кнопку Start (Пуск), сделайте правый клик на Computer (Мой компьютер) и выберите Properties (Свойства). Выберите Advanced System Settings (Дополнительные настройки системы). Вам может понадобиться подтвердить запрос подтверждения системы UAC. Затем кликните на кнопку Environment Variables (Переменные среды).

Теперь Вы можете редактировать списки как верхней пользовательской части переменных (персональные настройки), так и нижней (настройки переменных уровня системы).

Если Вы не администратор компьютера, то запуск этого диалога немного сложнее. Используйте одну из двух следующих процедур:

• Для редактирования системных переменных Вы можете использовать один из вышеперечисленных методов, однако Вам предстоит ввести пароль администратора. Не меняйте верхнюю (персональную) часть окна диалога — Вы поменяете настройки другой учетной записи, не Вашей.
• Чтобы редактировать персональные настройки переменных окружения, Вы должны использовать следующий метод: кликните Start (Пуск) -> Control Panel (Панель Управления) -> User Accounts and Family Safety -> User Accounts (учетные записи пользователей). В списке задач левой стороны окна кликните Change My Environment Variables (Изменить мои переменные окружения). Вы сможете редактировать только свои собственные (персональные) настройки переменных из верхней части диалога.

После того, как открылся диалог Environment Variables (Переменные среды), Вы можете создавать новые переменные, удалять переменные или выбрать и редактировать существующие переменные с использованием соответствующих кнопок.

Если Вам нужно изменить переменную, то необходимо понимать, что произойдет, если будет конфликт между переменными окружения пользователя и системными переменными окружения. Как правило, Windows проверяет несколько мест для определения в определенном порядке, и последнее определение выигрывает. Windows устанавливает переменные из следующих источников в указанном ниже порядке:

1. Список переменных уровня системы.
2. Список переменных пользователя. На этом шаге переменная PATH обрабатывается специальным способом (ниже это будет рассмотрено более подробно).
3. Набор команд в autoexec.nt. Это относится только для приложений MS-DOS или Windows 3.x. Для получения дополнительной информации см. раздел «Рабочее окружение MS-DOS».
4. Последующие определения, которые выдаются командами set, введенными в командной строке или которые встречаются в работающем bat-файле. Эти изменения прикладываются только к определенному окну, и исчезнут, как только это окно будет закрыто.

[Настройка переменной окружения PATH]

Если Вы пишете bat-файлы или скрипты, то полезно сохранять их в одной папке, и добавить её полное имя к переменной PATH, чтобы Вы могли запускать Ваши bat-файлы и скрипты из любого каталога простым вводом имения файла скрипта (без ввода его полного имени вместе с каталогом размещения).

Из-за того, что ошибка в редактировании переменной PATH может привести к тому, что Windows не сможет найти некоторые приложения, которые нужно запустить, предоставляется специальная обработка «персонального» определения PATH:

• Для переменной PATH определение пользовательских переменных (в разделе User Variables) добавляется в конец (приклеивается) определения системной переменной PATH (из раздела System Variables).
• Для всех других переменных окружения определение пользовательских переменных (в разделе User Variables) переназначит значение определения системных переменных (из раздела System Variables), если имена переменных из этих разделов совпадает.

Другими словами, Вы можете ввести свою собственную персональную папку (или папки) в пользовательское определение переменной PATH, не беспокоясь о том, что это повредит системным определениям путей поиска приложений.

Чтобы создать папку для своих собственных скриптов и bat-файлов, используя одну из следующих двух процедур:

• Если Вы хотите использовать скрипты и bat-файлы для своего собственного использования, то создайте папку, и поместите полный путь до неё в «персональную» переменную PATH. Например, создайте папку c:\scripts, затем добавьте переменную PATH в верхнюю часть диалога Environment Variables (переменные среды, см. рис. 29.2) со значением c:\scripts. Если нужно добавить больше одного пути в персональную переменную PATH, то вставляйте точку с запятой (;) между отдельными полными путями папок.
• Если Вы ходите создавать скрипты и bat-файлы, которые могут использовать любой пользователь этого компьютера (не только под Вашей учетной записью), то создайте папку и убедитесь, что настроены разрешения NTFS на запуск и чтение из неё (и/или другие необходимые права на определенные действия), так чтобы пользователи могли использовать скрипты в этой папке (если папка находится не на диске NTFS, а на диске FAT, то это не требуется).

Например, Вы создали папку c:\scripts на томе NTFS. Сделайте правый клик на неё, выберите Properties (Свойства), и выберите закладку Security (Безопасность). Если пользователи (Users) не видны в разделе групп (Group) или имен пользователей (User Names), то кликните Edit, затем Add, и добавьте пользователей в список. Убедитесь, что стоит галочка разрешения на праве чтения и выполнения (Read & Execute).

Примечание: чтобы узнать больше по поводу редактирования разрешений на файлы и папки, см. [3].

Затем с осторожностью отредактируйте переменную PATH в нижней части диалога Environment Variables (Переменные среды), где изменяются системные переменные, см. рис. 29.2. Добавьте точку с запятой (;) в конец существующего текста значения переменной, и затем добавьте туда имя папки c:\scripts.

Теперь Ваша папка будет частью PATH, когда Вы откроете новое окно приглашения командной строки (cmd.exe, Command Prompt window).

[Рабочее окружение MS-DOS]

Если Вы все еще используете программы MS-DOS, то должны иметь в виду, что 32-битные версии Windows 7 могут поддерживать программы MS-DOS.

Windows 7 и все другие версии Windows, основанные на Windows NT, запускают приложения MS-DOS из программы, которая называется ntvdm, она представляет для Windows NT виртуальную машину DOS (Virtual DOS Machine). Ntvdm также используется системой поддержки окружения (support environment) Windows 3.x. Она симулирует окружение, которое ожидают программы DOS, и которое позволяет им корректно работать под управлением Windows.

Ntvdm запускается автоматически, когда Вы делаете попытку запустить программу MSDOS или 16-битную программу Windows. Вам не нужно предпринимать специальные шаги для активации этой функции, но можно настроить работу таких старых приложений несколькими способами:

• Сконфигурируйте переменные пользователя в диалоге Environment Variables, как это ранее обсуждалось в разделе «Установка значений переменных окружения».
• Сделайте выбор настроек в меню управления окном DOS.
• Сделайте настройки в диалоге Properties (Свойства) ярлыка для приложения DOS.
• Настройте пользовательские конфигурационные файлы autoexec.nt и config.nt, чтобы они могли адресовать специальную память, драйвер или могли удовлетворять требованиям к переменным окружения, которые могут присутствовать у программы DOS.
• Введите команды, изменяющие окружение, в приглашении командной строки.

Эти настройки будут обсуждаться подробнее далее.

Примечание: старые приложения можно запускать с помощью программного обеспечения виртуальных машин. Больше информации о системе Virtual XP см. в Appendix A, «Using Virtualization on Windows 7» [1].

MS-DOS и 16-битные подсистемы Windows не работают на 64-битных версиях Windows. Если используете 64-битную версию Windows 7, и Вам все еще нужно запускать приложения MS-DOS или Windows 3.1, то можно загрузить и установить программу Microsoft Virtual PC с сайта microsoft.com, или использовать программное обеспечение VMWare от www.vmware.com. С любой из этих программ Вы можете настроить «виртуальный» компьютер, установить копию MS-DOS, Windows 3.1, или любой более поздней версии Windows, которая поддерживает запуск программ MS-DOS, и запустить старые приложения внутри этого симулированного рабочего окружения.

Удостоверьтесь, что установили Virtual PC «Guest Extensions» внутри хостовой операционной системы — эти расширения предоставляют важные улучшения. Например, они позволят Вам выполнять операции копирования и вставки между программами, работающими внутри и снаружи виртуального компьютера. Это не настолько легко, как встроенная поддержка старых приложений в 32-битных версиях Windows, однако работает очень хорошо.

Если у Вас Windows 7 Professional, Windows 7 Ultimate или Windows 7 Enterprise edition, то также можно использовать приложения MS-DOS, установленные в системе Virtual XP, которую можно бесплатно загрузить с сайта Microsoft.

[Если приложение MS-DOS говорит, что слишком много открытых файлов]

«Too Many Files Open» — если Вы запустили приложение MS-DOS, и получаете от него сообщение о слишком большом количестве открытых файлов, то нужно изменить config.nt (или создать пользовательский файл конфигурации). Вы должны поменять строку, которая выглядит как files = 40 на другую, где указано большее количество файлов, например files = 100. См. далее «Настройка Autoexec.nt и Config.nt», где даны инструкции по модификации config.nt.

[Приложения MS-DOS выводят странные символы]

Если старые приложения выводят некоторые кракозябры на экран, особенно в комбинации ??[, то Ваша программа ожидает поддержки драйвера ansi.sys. Вам нужно добавить следующую строку в файл config.nt, используя инструкции раздела «Настройка Autoexec.nt и Config.nt», которые найдете далее:

[Настройка дополнительных опций (Advanced Settings) для приложений DOS]

Если Вы встречаетесь с трудностями при запуске каких-то программ DOS, то можете более точно подстроить окружение VDM с учетом требований этого конкретного приложения, позволяя ему работать более качественно, или в некоторых случаях вообще позволить ему запуститься. Настройки свойств DOS могут повлиять на множество аспектов работы приложения, таких как ниже перечисленные (но не ограничиваясь ими):

• Диск и папка (директория) выбранные по умолчанию при старте приложения.
• Полноэкранный режим или работа окне.
• Использование стандартной (Conventional) памяти.
• Использование расширенной или дополнительной памяти (Expanded, extended memory).
• Уровень приоритета приложения в многопоточной среде.
• Горячие клавиши приложения.
• Видимая и фоновая обработка (Foreground, background processing).

Чтобы отредактировать эти свойства для программы DOS, выполните следующее:

1. Найдите файл программы или ярлычок для неё.
2. Сделайте на нем правый клик и выберите Properties (Свойства).

Пройдитесь по закладкам, и используйте кнопку ? (со знаком вопроса) для получения подсказок по настройкам. Обучающие программы и игры часто требуют подстройки опций Memory (память) и Compatibility (совместимость).

Замечание: если программа сохранена на разделе, отформатированном в файловой системе NTFS, то стандартная закладка Security (Безопасность) также будет среди закладок диалога Properties.

Совет: закладка Screen (экран) опций использования определяет, как изначально будет отображаться окно программы — в полный экран или в отдельном окне. Вы можете переключаться между этими режимами с помощью горячей комбинации клавиш Alt+Enter. Конечно, в полноэкранном режиме графический курсор мыши исчезнет. Когда Вы используете мышь в приложении, отображаемом как окно, то мышь будет работать со стандартными меню окна, и будет работать, когда Вы переключите контекст на Рабочий Стол (Windows desktop) или другие программы. Не требуется драйвер мыши, основанный на DOS, поддержка мыши предоставляется автоматически.

[Настройка Autoexec.nt и Config.nt]

Вы можете далее выбрать конфигурирование окружения MS-DOS и Windows 3.x модификацией эквивалента в Windows 7 для старых файлов CONFIG.SYS и AUTOEXEC.BAT. Эти файлы называются config.nt и autoexec.nt, и они используются для конфигурирования каждой DOS VDM, когда она запускаются. Просто запомните:

• Файлы CONFIG.SYS и AUTOEXEC.BAT, находящиеся в корневом каталоге жесткого диска, полностью игнорируются Windows 7. Причина, по которой они все еще там находятся — одурачить старые приложения, которые не захотят запускаться, если они не увидят, что эти файлы присутствуют.
• Файлы config.nt и autoexec.nt в каталоге \windows\system32 используются, но только тогда, когда Windows нуждается в запуске приложения MS-DOS или Windows 3.x. Любое изменение этих файлов даст эффект в следующий раз, когда Вы снова запустите старое приложение — не нужно перезапускать Windows.

Стандартные настройки в config.nt, которые устанавливаются, когда Windows инсталлирована, показаны в следующем листинге. Комментарии REM удалены для краткости. Если Вы обновили свой компьютер от более ранней версии Windows, то файл config.nt может отличаться, потому что инсталлятор может сохранить кое-что из настроек предыдущей операционной системы.

dos=high, umb
device=%SystemRoot%\system32\himem.sys
files=40

Совет: на своих компьютерах автор [1] всегда меняет эти файловые настройки для установки files=100, и добавляет строку с ansi.sys (для дополнительной информации по ansi.sys см. следующую секцию):

device=%SystemRoot%\system32\ansi.sys

Вы можете отредактировать файлы config.nt и autoexec.nt в простом текстовом редакторе, таком как Notepad (мне больше всего нравится Notepad2 [10]). Однако это защищенные файлы, поэтому Вы должны запустить Notepad с повышением привилегий, следуя процедуре:

1. Кликните Start (Пуск) -> All Programs (Все программы) -> Accessories (Стандартные).
2. Выполните правый клик на ярлычке Notepad и выберите Run As Administrator (запуск от имени администратора).
3. Подтвердите запрос UAC, или введите запрошенный пароль учетной записи Administrator (Администратор). Альтернативно можно просто выполнить команду notepad в окне приглашения командной строки Command Prompt, запущенного с повышенными привилегиями администратора.
4. Кликните File (Файл) -> Open (Открыть), и перейдите в папку \windows\system32. Выберите файл autoexec.nt или config.nt, какой пожелаете редактировать.

Большинство настроек, используемых в MS-DOS 6, все еще будут работать в config.nt, с некоторыми изменениями, показанными в таблице 29.2.

Таблица 29.2. Расширенные настройки (Enhanced Settings) для config.nt.

Команда Описание
device= Инсталлирует загружаемые драйверы устройства. Драйверы, которые попытаются напрямую адресовать аппаратуру, вероятно работать не будут; однако Вы сможете загрузить драйверы дисплея, такие как ANSI.SYS, и драйверы памяти, такие как EMM.SYS и HIMEM.SYS.
dosonly Позволяет из COMMAND.COM загружать только программы DOS. Программы Windows и UNIX не запустятся.
echoconfig Говорит VDM напечатать команды CONFIG и AUTOEXEC, как они были выполнены из файлов.
files= Устанавливает максимальное количество одновременно открытых файлов. Рекомендуется установить этот параметр в 100.
ntcmdprompt Заменяет интерпретатор COMMAND.COM на 32-битный интерпретатор команд Windows, cmd.exe. После того, как Вы загрузите TSR (напомню, что TSR означает Terminate and Stay Resident, т. е. программа, которая после своего завершения остается резидентной в памяти), или когда Вы выходите из приложения в DOS, то получите вместо старого интерпретатора cmd.exe, который будет иметь дополнительные достоинства полного 32-битного интерпретатора.

Если Вы захотите, то можете создать измененные копии файлов config.nt и/или autoexec.nt, и просто использовать их для определенных программ DOS. Чтобы сделать это:

1. Используйте Notepad, запущенный с повышенными привилегиями, чтобы создать новый файл (файлы) настроек с разными именами. Например, Вы можете сохранить измененный config.nt под именем config_wordperfect.nt.
2. Найдите файл иконки программы MS-DOS .exe или .com в Windows Explorer (в Проводнике).
3. Сделайте правый клик на эту иконку, и выберите Properties (Свойства). Выберите закладку Program (Программа) и кликните кнопку Advanced (Дополнительные настройки). Введите путь до Вашего измененного файла конфигурации.

Совет: корректное редактирование этих файлов это Вам не два пальца об асфальт. Рекомендуется использовать хорошее руководство по DOS, такое как MS-DOS 6 User Guide, или еще лучше [9].

[Проблемы с DOSKEY и ANSI.SYS]

Двумя чаще всего использовавшимися улучшениями на компьютерах с системой MS-DOS были DOSKEY и ANSI.SYS. DOSKEY предоставлял улучшенное редактирование командной строки: например, использование клавиш со стрелками ‘вверх’ и ‘вниз’, чтобы вспомнить ранее введенные команды. ANSI.SYS давал приложениям DOS способ простого управления позицией и цветом текста, выводимого на экран.

ANSI.SYS можно сделать доступным для программ MS-DOS простым добавлением строки device=ansi.sys в файл config.nt (или альтернативный файл конфигурации). К сожалению, для 32-битных Windows не предоставляется поддержка курсора ANSI для символьных (консольных) приложений.

Примечание: если Вы делаете изменения в autoexec.nt или config.nt после запуска программы MS-DOS из окна командной строки (Command Prompt window), Вы должны закрыть окно командной строки и открыть новое для подсистемы MS-DOS, чтобы изменения были перезагружены в новой измененной конфигурации.

С другой стороны, DOSKEY — который значительно повышал удобство работы в старые времена DOS — работает только в 32-битном окружении консоли Windows, и даже если Вы попытаетесь загрузить его в autoexec.nt, он не будет функционировать в шелле COMMAND.COM системы MS-DOS.

[Bat-файлы]

Хотя интерпретатор Windows Script Host это наиболее мощный инструмент для создания Ваших собственных полезных программ, также полезно знать, как использовать язык bat-файлов. Bat-файлы (командные файлы) позволяют Вам получить преимущество от тысяч готовых программ командой строки, которые можно найти для Windows.

Bat-файл на самом простом уровне это просто список команд для окна Command Prompt, которые набраны в одном файле, строка за строкой, и этому файлу присвоено расширение *.bat или *.cmd. Когда Вы вводите имя bat-файла в приглашении командной строки, Windows ищет файл с таким именем в текущей директории и затем в путях папок, обозначенных переменной окружения PATH. Windows обрабатывает каждую строку bat-файла как команду, и запускает их одну за другой, как если бы Вы вводили их друг за другом вручную. Даже на таком самом простом уровне bat-файл окажет большую помощь, если Вам нужно вводить одинаковые команды снова и снова.

Кроме этого простого сценария использования есть несколько команд, которые можно использовать для написания простейших «программ» в bat-файле, чтобы предпринимались разные действия в зависимости от того, что было введено пользователем в командной строке, или в зависимости от результата выполнения предыдущих команд. Эти команды «программирования» значительно улучшились со времен MS-DOS, так что написание полезных bat-файлов в Windows 7 намного проще, чем это было раньше в старой MS-DOS. В частности, операторы IF и FOR были значительно расширены по функционалу. Вы можете выдавать приглашение ввода для пользователя. Можно манипулировать строками и именами файлов для выполнения арифметических вычислений. Вы можете создавать подпрограммы внутри одного файла. И это далеко не все.

Более подробно программирование bat-файлов рассмотрено в книге «Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools», опубликованной издательством Que. Некоторую документацию можно найти онлайн, в Интернете. После прочтения этой главы перейдите на сайт www.microsoft.com и запустите поиск следующих фраз:

Command Shell Overview
Environment Variables
Using Batch Parameters
Using Batch Files
Using Command Redirection Operators
Cmd
Command-Line Reference

Затем откройте окно приглашения командной строки cmd.exe и для примера поэкспериментируйте с командами:

help cmd
help set
help for
help if

См. также статьи [4, 5, 6].

Советы по созданию bat-файлов. В таблице 29.3 перечислены несколько коротких bat-файлов, которые автор [1] использует на своих компьютерах. Эти короткие скрипты дают возможность редактировать файлы, менять путь, просматривать папку в Проводнике, если просто ввести букву и далее имя папки или имя файла. В них нет ничего особенного в плане программирования, однако они позволяют экономить время при работе с окном командной строки.

Если Вы создали папку c:\scripts и добавили её путь в переменную окружения PATH, как обсуждалось ранее в разделе «Настройка переменной окружения PATH», то возможно Вы захотите создать подобные bat-файлы в этой папке для своего собственного использования.

Таблица 29.3. Маленькие полезные bat-файлы.

Скрипт Описание
ap.bat:

@echo off
for %%p in (%path%) do if /%%p/ == /%1/ exit /b
set path="%1";%path%
Добавляет указанную по имени папку в PATH, если её там еще нет (это остается только пока открыто текущее окно командной строки, где выполнен этот bat-файл).

Пример: ap c:\test

bye.bat:

@logout
Завершает сеанс пользователя в Windows (log off).

Пример: bye

e.bat:

@if /%1/ == // (explorer /e,.) else explorer /e,%1
Откроет Windows Explorer в режиме папки для просмотра указанной по имени папки, или текущую папку, если в командной строке не был введен путь.

Пример: e d:

h.bat:

@cd /d %userprofile%
Меняет текущую директорию на директорию Вашего профиля пользователя (домашний каталог, home).

Пример: h

n.bat:

@start notepad "%1"
Открывает в Notepad файл для редактирования.

Пример: n test.bat

s.bat:

@cd /d c:\scripts
Делает папку c:\scripts текущей директорией, когда Вы хотите добавить новый скрипт или редактировать старый.

Пример: s

[Windows Script Host]

С некоторого времени Microsoft старательно позаботилась о том, чтобы обеспечить программистам доступ к внутренним функциям своих коммерческих приложений, таких как Word и Excel, а также и самой Windows. Это сделано через технологию, которая называется Component Object Model, или COM. Она позволяет разрабатывать программы таким образом, чтобы они совместно использовали свои данные и функциональные возможности вместе с другими программами — в любой другой программе, написанной на любом языке программирования. Если Вы когда-либо писали макросы для Word или Excel, Вы работали со скриптами и COM. Один из продуктов этих усилий — система Windows Script Host, или WSH [7], которая предоставляет быстрый и простой способ писать свои собственные программы утилит. Скрипты WSH имеют преимущество перед bat-файлами, потому что могут выполнять более сложные вычисления и манипуляции с текстовой информацией. Это может быть осуществлено более мощным способом, потому что Вы описываете действия в полнофункциональном языке программирования.

Скрипты могут генерировать, перерабатывать, манипулировать текстовыми файлами и данными, просматривать и менять настройки Windows, и пользоваться преимуществами служб Windows через объекты COM, предоставленными как стандартная часть Windows. Дополнительно, если у Вас есть установленное приложение, где разрешена работа COM, такое как WordPerfect, Microsoft Word или Excel, то скрипты могут даже задействовать эти приложения для представления информации в качественно отформатированных документах и диаграммах.

Windows поставляется с поддержкой двух разных скриптовых языков:

• VBScript — очень близко похож на язык макросов Visual Basic for Applications (VBA), который используется в Word и Excel.
• JScript — версия языка JavaScript от Microsoft, который широко используется для создания интерактивных страниц web-сайтов (JavaScript не то же самое, что Java. Java в целом другой язык программирования).

Дополнительно Вы можете загрузить и установить поддержку скриптов на других языках. Если, к примеру, у Вас есть опыт работы в UNIX или Linux, то Вы можете захотеть использовать языки скриптов Perl, Python или TCL. Вы можете получить WSH-совместимые версии этих языков на сайте www.activestate.com.

Если Вы уже разбираетесь в одном из вышеперечисленных языков скриптового программирования, то в любом случае используйте его. Если же Вы пока не работали с языками скриптов, то VBScript скорее всего самый лучший выбор для старта, потому что Вы сможете также использовать его для написания макросов для стандартных офисных приложений Microsoft. В этой секции будут приведены примеры, использующие VBScript.

Создание скриптов. Точно так же, как и bat-файлы, скрипты сохраняются как обычные текстовые файлы, которые можно редактировать с помощью Notepad или другим редактором текстовых файлов. Чтобы создать файл скрипта, выберите для него понятное описательное имя, что-то типа WorkSummaryReport, и добавьте к нему расширение, которое соответствует используемому Вами языку. Скрипт, написанный на языке VBScript, должен иметь расширение файла *.vbs.

К примеру, Вы пишете скрипт, который называется hello.vbs. Выполните для этого следующие шаги.

1. Откройте приглашение командной строки (Command Prompt) кликом Start (Пуск) -> All Programs (Все программы) -> Accessories (Стандартные) -> Command Prompt (Командная строка).

2. Окно командной строки откроется в каталоге по умолчанию \users\your_user_name (например C:\Documents and Settings\имя_пользователя>). Если Вы хотите создать скрипт в другой папке, то следует ввести команду cd, чтобы поменять текущую директорию. Как уже советовалось ранее, удобнее всего создавать скрипты в одной папке, наподобие C:\scripts, путь до которой добавлен в переменную окружения PATH. Но для рассматриваемого здесь примера это не важно, так что пропустим этот шаг и оставим для использования директорию по умолчанию.

3. Введите команду notepad hello.vbs. Когда редактор Notepad запросит у Вас создание нового файла, кликните Yes.

4. Введите в файле текст:

wscript.echo «Hello, this message comes from a script»

5. Сохраните скрипт через меню File (Файл) -> Save (Сохранить), или нажмите Ctrl+S. Вы можете оставить открытым окно Notepad, или закрыть его выбором в меню File (Файл) -> Exit (Выход), или нажмите Alt+F4.

6. Перейдите в окно командной строки.

7. Введите hello и нажмите клавишу Enter.

Если все хорошо, то Вы увидите окно диалога, показанное на скриншоте рис. 29.3. Кликните OK для закрытия этого окна.

command line and automation tools fig29 3

Рис. 29.3. Пример, как скрипт отображает простое текстовое сообщение.

WSH может отображать результат своей работы в графическом окне, или может показывать его в окне консоли, как это делают большинство программ командной строки. Как было показано в предыдущем примере, по умолчанию информация отображается в графическом окне, потому что по умолчанию интерпретатором для скриптов выбирается wscript. Обычно лучше поменять такой выбор по умолчанию, чтобы использовать консольный интерпретатор скрипта, который выводит текстовые сообщение в окно консоли. Чтобы сделать это, введите команду:

cscript //H:cscript //nologo //s

Обратите внимание, что в этой команде используется двойной слеш. Теперь снова введите команду hello. На этот раз вывод скрипта отобразится в окне приглашения командной строки.

[Некоторые примеры скриптов]

Здесь даны простые примеры, чтобы показать силу WSH для выполнения полезных задач и управления в среде Windows.

Управление дисками и сетью (Disk and Network Management). WSH поставляется с инструментами для опроса и модификации дисков, папок и фалов. Здесь дан пример языка VBScript, который выполняет довольно полезную задачу:

set fso = CreateObject("Scripting.FileSystemObject")
set drivelist = fso.Drives
for each drv in drivelist
   if drv.IsReady then
      wscript.echo "Drive", drv.DriveLetter, "has", drv.FreeSpace, "bytes free"
   end if
next

Скрипт отображает свободное пространство на каждом из дисков компьютера. Введите этот скрипт в файл с именем freespace.vbs, поместите его в Ваш каталог скриптов, и затем в строке приглашения команд введите команду freespace. Будет выведено примерно следующее:

Drive C: has 15866540032 bytes free
Drive D: has 27937067008 bytes free
Drive F: has 335872000 bytes free
Drive H: has 460791808 bytes free

WSH также может работать и с сетью. Следующий скрипт VBScript отобразит текущие привязки сетевых дисков Вашего компьютера:

set wshNetwork = CreateObject("WScript.Network") ' создается вспомогательный объект
set maps = wshNetwork.EnumNetworkDrives          ' сборка, описывающая сетевые диски
for i = 0 to maps.Length-2 step 2                ' цикл по сборке
   wscript.echo "Drive", maps.item(i), "is mapped to", maps.item(i+1)
next

[Windows Management Instrumentation]

Windows Management Instrumentation (WMI) это системная служба, которая дает виртуальный доступ к каждому аспекту компьютерной системы Windows, от компонентов аппаратуры до высокоуровневых системных служб [8]. Для некоторых компонентов WMI представляет только информацию. Другие компоненты можно поменять, и таким образом, как подразумевает имя WMI, эту технологию можно использовать для управления системой. Вы можете использовать WMI для запуска и остановки системных служб, отслеживания работы приложений и их остановки, создание привязки дисков к папкам и сетевым каталогам, давать общий доступ к папкам, и если установлены соответствующие обновленные драйверы WMI, можно даже управлять системными службами, такими как Internet Information Services, Microsoft Exchange, и Domain Name Service на операционной системе Windows Server.

Следующий скрипт перечисляет состояние каждой системной службы, установленной на компьютере. Этот скрипт можно назвать showservices.vbs. Нижнее подчеркивание в конце некоторых строк означает продолжение строки.

set services = GetObject"winmgmts:{impersonationlevel=impersonate," &_
               "authenticationlevel=pkt}!" &_
               "/root/CIMV2:Win32_Service")  ' получение информации WMI по службам
for each svc in services.Instances_          ' отображение информации о каждой службе
   wscript.echo svc.name, "State:", svc.State, "Startup:", svc.StartMode
next

Результат работы скрипта может выглядеть примерно так:

AeLookupSvc State: Stopped Startup: Manual
ALG State: Stopped Startup: Manual
AppIDSvc State: Stopped Startup: Manual
Appinfo State: Running Startup: Manual
AppMgmt State: Stopped Startup: Manual

Также помните, что для программ и скриптов командной строки можно использовать перенаправление вывода консоли в файл. К примеру, следующая команда поместит список служб в файл listing.txt точно так же, как это сделала бы любая другая традиционная программа Windows:

showservices >listing.txt

[Windows PowerShell]

Microsoft разработала новую рабочую среду командной строки, которая называется Windows PowerShell (WPS). Она установлена как стандартный аксессуар начиная с Windows 7 (вероятно, Биллу Гейтсу не давала покоя стройная система взаимодействия консольных программ в мире *NIX). WPS чаще всего выглядит и действует как окно программной строки, но на самое деле это очень странная сущность, которая дает доступ к некоторым очень мощным инструментам программирования. Здесь не будет полного описания, как использовать WPS, но будет показано, чем отличаются bat-файл и скрипты, и будут указаны ресурсы, которые помогут научиться большему.

Для описания WPS автор использует слово «странный». Может ли компьютерная программа быть странной? Определенно! С одной стороны, большинство команд Windows PowerShell (которые правильно называть cmdlet) генерируют потоки объектов, не текст. Объекты это попытка представления в компьютере вещей из реального мира. У них есть свойства, которые описывают атрибуты вещей, которые они представляют, и методы, которые позволяют Вам управлять вещами. Например, объект представляет определенный файл на жестком диске, и у него есть свойства Name (имя), Size (размер) и LastWriteTime (последнее время записи), и методы наподобие Delete (удалить), Edit (редактировать) и Open (открыть). Windows PowerShell работает с объектами в новым, необычным и в конечном счете очень мощным способом.

Если Вы введете команду dir в обычном окне Command Prompt, то среда интерпретации команд обработает dir и в ответ вернет блок текста листинга, где будут присутствовать имена файлов и папок текущего каталога. Команда dir была специально запрограммирована для печати текстовой информации о файлах в текстовом виде. И это все, что она может делать.

В системе WPS Вы можете ввести dir, и это также приведет к печать списка файлов, но неявно, за сценой происходит нечто совершенно другое. В WPS это просто краткая ссылка на Get-Childitem cmdlet, который при самом простом использовании генерирует поток объектов File; каждый объект представляет один из файлов в папке, и у каждого такого объекта есть свойства и методы (например, имя и размер). Когда объект (любого сорта) попадает в окно приглашения WPS, то система WPS печатает строку текста об объекте с наиболее важными его свойствами. Для объекта File это включает имя файла, размер и дату создания. Таким образом, когда Вы вводите dir, WPS генерирует поток объектов File и он представляется как красивый, разделенный табуляциями список файлов.

Конечный результат выглядит так же, как и в старом рабочем окружении командной строки, но все происходит более абстрактным способом. Cmdlet не знает, или не заботится о тексте или форматировании: он просто генерирует набор объектов File. И окно WPS превратит любой список объектов в удобочитаемый текстовый листинг. Файлы, учетные записи пользователей, службы Windows, и т. д. — любой объект, который выбрасывает соответствующий cmdlet, система WPS превращает в своей консоли в удобный листинг текста.

Дополнительно WPS включает в себя полнофункциональный объектно-ориентированный язык программирования, который имеет доступ к платформе программирования .NET компании Microsoft. Это означает, что скрипты WPS могут выполнять более сложные вычисления и обмениваться информацией с другими компьютерами и сетевыми («облачными») сервисами.

WPS даже позволяет Вам делать сложные вещи с объектами без программирования. Вы можете использовать символ канала | для направления потоков объектов от одного cmdlet к другому, и это позволяет выполнять очень сложные, специфические действия с инструментами, которые по отдельности очень простые и по природе выглядят самыми обычными. Например, следующая команда удалит все файлы в текущем каталоге, которые старше 6 месяцев:

dir | where-object {$_.LastWriteTime -lt (get-date).addmonths(-6)} | remove-item

Поначалу это выглядит сложным но не все так плохо. Эта командная строка состоит из трек отдельных cmdlet, выполняющих совместное действие:

• dir дает список объектов File в текущей директории. Здесь они не попадают в окно команд WPS, если бы это было так, то был бы выведен текстовый список. Но здесь вместо этого символ канала (pipe, |) инструктирует WPS передавать объекты следующей команде.
• where-object пропускает через себя некоторые объекты, основываясь на условии фильтрации внутри фигурных скобок. В этом примере пропускаются только те файлы, которые не изменялись больше 6 месяцев (здесь значение LastWriteTime меньше чем дата/время 6 месяцев ранее). Таким образом, эти объекты представляют собой просто старые файлы, которые канализируются в следующую команду.
• remove-item удаляет с жесткого диска файлы, соответствующие поступающим на вход объектам.

Предупреждение: не открывайте просто так окно WPS и не вводите эту команду, только чтобы увидеть, что получится! Возможно, что Вы удалите важные файлы из папки профиля текущего пользователя. Если Вы хотите посмотреть, как работает этот скрипт, введите первые 2 части этой команды (это напечатает список выбранных файлов, но не удалит их):

dir | where-object {$_.LastWriteTime -lt (get-date).addmonths(-6)}

Как было сказано ранее, Вы не ограничены простым использованием команд, которые вводите в окно WPS. WPS это полноценный язык программирования с переменными, циклами, подпрограммами, определенными пользователем объектами, и так далее. Вы можете использовать все это в приглашении ввода команды или в файлах скриптов. Также Вы можете создать ярлычки (которые называются псевдонимами, aliases) для наиболее часто используемых команд и скриптов, чтобы их можно было проще использовать позднее.

Для дополнительной информации про WPS, см. ресурсы в Интернете, и книжку «Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools» или «Windows PowerShell 2.0 Unleashed».

[Планировщик Задач (Task Scheduler)]

Планировщик Задач Windows позволяет Вам запрограммировать автоматический запуск на определенные дату и время или при возникновении нужных событий наподобие запуска системы, вход пользователя в систему, или даже при наступлении любого события, запись о котором попадает в системный лог Event Viewer.

Сама по себе служба Task Schedule незначительно влияет на производительность системы, хотя задачи, которые эта служба запускает, могут влиять. Однако Вы можете инструктировать не запускать определенные задачи, когда система чем-то загружена. Вы можете захотеть сделать это, например, для тех задач, которые генерируют некоторую дисковую активность.

Замечание: когда Task Scheduler запускает задачу, которая работает от имени другого пользователя (который в настоящее время не вошел в систему), то другой пользователь, который сейчас вошел в систему, не увидит, как работает запущенная задача, и не сможет с ней взаимодействовать. Удостоверьтесь, что задачи могут работать самостоятельно, без ввода пользователя, и чисто завершаться после выполнения своей работы. И еще имейте в виду, что как только приложение или сервис заработали, даже если они были запущены как задача планировщика, они будут влиять на производительность системы так же, как если бы они были запущены вручную.

Какие виды задач можно запускать через Task Scheduler? Как уже упоминалось, это те задачи, которые не требуют взаимодействия с пользователем. Например, это может быть типовая задача обслуживания, такая как дефрагментация жесткого диска, очистка папок от временных файлов, и так далее. Windows использует Task Scheduler для подобных целей, поэтому Вы можете заметить, что после установки Windows могут быть определены некоторые запланированные задачи. Task Scheduler также может ожидать появления любого события, которое может быть записано в Event Log.

Task Scheduler особенно полезен для запуска bat-файлов и скриптов, потому что эти запланированные программы могут быть разработаны для запуска без какого-либо взаимодействия с пользователем. Это действительно важный инструмент автоматизации, потому что Вы даже можете не быть за компьютером, когда они работают!

Есть 2 типа задач, которые Вы можете создать в Task Scheduler:

• Базовые задачи — разрабатываются для запуска с использованием текущей учетной записи пользователя, и поддерживают одно сработавшее событие или настроены на нужный момент времени.
• Задачи — могут быть запущены от указанной учетной записи пользователя, и могут быть сконфигурированы для запуска в любом случае, залогинен пользователь в системе, или нет. Задачи также могут быть запущены в режиме совместимости с Windows XP или Windows Server 2003 (compatibility mode), и могут быть запущены с приоритетом выше обычного, если это необходимо.

Замечание: очевидно, что компьютер должен находится в рабочем состоянии для запуска задачи, поэтому если Вы ожидаете очистки диска в 4:00 a.m., то гарантируйте, что в это время компьютер включен. Если запуск запланированной задачи был пропущен из за того, что компьютер был выключен, то Windows выполнит задачу в следующий раз, когда компьютер будет запущен, но задача теперь скорее всего будет работать, когда Вы используете компьютер, хотя вероятно Вы этого хотели избежать, когда планировали запуск задачи ночью.

Чтобы создать «базовую» задачу в Task Scheduler, выполните следующие шаги:

1. Запустите Task Scheduler, окно Task Scheduler отобразит в верхней центральной части общий список задач, которые запущены и/или завершились в последние 24 часа, и покажет список активных задач ниже. Здесь «активная задача» означает, что задача разрешена для запуска в указанное время или при указанном событии. Не обязательно, что «активная» означает задачу, которая работает прямо сейчас.

2. Область Add Actions (добавить действие) находится с правой стороны. Кликните Create Basic Task (создать базовую задачу). Запустится мастер для создания базовой задачи (Create Basic Task Wizard).

3. Введите имя задачи и её описание. Здесь можно ввести все что хотите, чтобы напоминать о том, что задача делает. Кликните Next (Далее) чтобы продолжить.

4. В окне Task Trigger (настройка условия запуска), выберите, когда запускать задачу. Вы можете выбрать запуск ежедневно (daily), еженедельно (weekly), каждый месяц (monthly), или однократно (one time); когда компьютер запуститься, когда Вы вошли под учетной записью (log on), или когда было записано в лог определенное событие. Вы можете использовать опцию, когда определенное событие записано (When a Specific Event Is Logged), чтобы задача сработала, когда записывается в Event Log нужное событие. Например, Вы можете использовать это для выполнения оповещений определенного типа, если произойдет событие ошибки диска. Вам понадобится ввести цифровой номер ID для идентификации события.

5. Кликните Next.

6. Задайте опции применимого времени, такого как время дня, как это необходимо. Кликните Next.

7. Выберите, что должна делать задача (открыть программу, послать email, или отобразить сообщение). Кликните Next для продолжения.

8. Если Вы выбрали Start a Program (запуск программы), используйте кнопку Browse, чтобы указать программу, bat-файл или скрипт. Для приложений Windows открывайте папку \Windows или \Windows\system32. Для сторонних приложений просмотрите подкаталоги в папке \Program Files. Для скриптов, которые Вы написали сами, откройте папку, где сохранен нужный скрипт или bat-файл. Когда предоставили необходимые опции или настойки командной строки, если хотите указать диск по умолчанию или папку для программы, введите путь до нужной папки.

Если Вы выбрали Send an Email (отправить электронное письмо), введите информацию об отправителе, получателе, сервере SMTP, сообщение (sender, receiver, SMTP email server, message), и т. д.

Если Вы выбрали Display a Message (отобразить сообщение), то введите заголовок сообщения (message title) и текст сообщения (message text). Затем кликните Next.

9. Просмотрите задачу в окне Summary (см. рис. 29.4). Если Вы хотите установить дополнительные опции (advanced options), такие как время ожидания, что делать если компьютер работает от батарей, и что делать после завершения задачи, проверьте Open Advanced Properties (открыть дополнительные опции) для этой задачи. Кликните Finish для завершения задачи.

command line and automation tools fig29 4

Рис. 29.4. Завершение конфигурации базовой задачи.

Для более продвинутого планирования используйте выбор Create Task (создать Задачу). Интерфейс Create Task использует интерфейс с множеством закладок вместо мастера. Закладка General включает опции безопасности, в то время как закладка Triggers позволяет Вам указать несколько условий (триггеров) для запуска задачи (задача будет выполнена, когда произошло любое из указанных событий). Закладка Actions поддерживает несколько действий в задаче, закладка Conditions включает опции для конфигурирования требований к времени ожидания (idle time), питанию (power), и сетевому соединению, и закладка Settings поддерживает условия для запуска и остановки задачи. Используйте Create Task вместо Create Basic Task, когда Вам нужно настроить эти дополнительные опции в своей задаче.

[Ссылки]

1. Microsoft Windows 7 In Depth site:docs.google.com.
2. Command-line reference AZ — TechNet — Microsoft site:technet.microsoft.com.
3. NTFS File Permissions site:ntfs.com.
4. Практические приемы программирования в bat-файлах.
5. Применение команды for в bat-файлах (technet.microsoft).
6. О параметрах командных файлов и не только.
7. Windows Script Host на базе VBScript (*.vbs).
8. Не знаете, что такое WMI? Тогда мы идем к Вам!
9. Microsoft DOS and Windows Command Line site:computerhope.com.
10. Замена Windows notepad на notepad2.
11. 170813command-line-and-automation-tools.zip — документация.

Скрипты выполняемые интерпретатором CMD.EXE — стандартной консольной оболочкой для Win2000/WinXP/Vista/Seven/Win8/Win2000 Server/Win2003/Win2008.
Иначе говоря — пакетники. Иногда еще их(не вполне оправданно) называют батниками, но классический батник использует возможности обеспечиваемые оболочкой предыдущих систем COMMAND.COM, возможности которого существенно меньше.

  • Блог пользователя yurkesha

Выключение компьютеров в домене по списку

Монолитный скрипт выключения компьютеров в домене по списку:

@ECHO OFF
SET "BEGIN_MARKER=:ENDFILE1"
SET "END_MARKER=:ENDFILE2"
FOR /F "usebackq tokens=1 delims=:" %%a IN (`FINDSTR /N /B /C:"%BEGIN_MARKER%" "%~0"`) DO SET "SKIP_LINE=%%a"
CALL :WORK "%SKIP_LINE%" "%END_MARKER%" "%~0"
GOTO :EOF

:WORK
FOR /F "usebackq skip=%~1 tokens=1 eol=; delims=" %%a IN (`TYPE "%~3"`) DO IF NOT "%%a"=="%~2" (CALL :PROCEDURE "%%a") ELSE (GOTO :EOF)
GOTO :EOF

:PROCEDURE
(ping -n 1 %~1|FIND /I "TTL=")&&(
ECHO Комп "\\%~1" Включен - выключаю...
shutdown -f /s /m \\%~1 -t 1
)||ECHO Комп "\\%~1" выключен
GOTO :EOF

:ENDFILE1
PC1
PC2
PC3
:ENDFILE2
  • Блог пользователя yurkesha

Единый логон-скрипт для AD

Моя попытка навести порядок и унификацию при подключении сетевых дисков в AD.

Возможна работа как через индивидуальные групповые политики, так и через политику AD по-умолчанию.

Идея состоит в следующем: храним описание вариантов подключений в текстовых файлах, с указанием группы для которой это работает, и анализируем при запуске грeппы конкретного пользователя с использованием dsget и dsquery.

Структура хранимых описаний такова: групповые и общедоменные назначачения хранятся в едином файле расположенном в корне NETLOGON(возможны варианты), а пользовательские назначения располагаются в папках с пользовательским именем входа  в систему.
Также в отдельных файлах храним описание запускаемых скриптов для групп и пользователей.

Цель — получить прозрачный и универсальный алгоритм входа в систему единый как для обычного входа, так и для терминального, без черезмерного усложнения групповых политик.

Структура решения:
\dsget.exe — файл из RK для получения списка групп пользователя
\dsquery.dll— файл из RK для получения списка групп пользователя
\dsquery.exe— файл из RK для получения списка групп пользователя
\logon.cmd — собственно тело скрипта
\logon.vbs — костыль для запуска без отображения окна консоли — именно он должен быть назначен в качестве логон скрипта
\rs_list.txt — список групповых и общедоменных назначений подключаемых дисков с указанием группы
\sc_list.txt — список групповых и общедоменных назначений выполняемых скриптов с указанием группы
\_SCRIPT\ — папка с групповыми и общедоменными скриптами
\_SCRIPT\1C.cmd — пример скрипта
\USER1\rs_list.txt- список назначений подключаемых дисков для конкретного пользователя(без указания группы)
\USER1\sc_list.txt— список назначений выполняемых скриптов для конкретного пользователя(без указания группы)
\USER2\rs_list.txtсписок назначений подключаемых дисков для конкретного пользователя(без указания группы)
\USER2\sc_list.txt- список назначений выполняемых скриптов для конкретного пользователя(без указания группы)
Прикрепляю файл с телом скрипта и тестовой структурой.

Приветствуются пожелания по доработке ;)
Добавил ключ рекурсивного анализа вхождения в группы.

И на всякий случай тело скрипта в текстовом виде:


@ECHO OFF
:: Буквы дисков зарезервированные под пользовательские сетевые подключения
SET "EXLUDE_LETTER=D: E: F: J: H: I: J:"
"%~dp0dsquery.exe" user -d "%USERDOMAIN%" -samid "%USERNAME%"|^
%~dp0dsget.exe user -memberof |findstr /I /C:"CN=Domain Users,"&&(
:: Удаление текущих сетевых подключений
FOR /F "usebackq tokens=2 delims= " %%a IN (`net use^|find ":"^|find "\\"^|FINDSTR /V /I "%EXLUDE_LETTER%"`) DO (
1>NUL 2>&1 NET USE /DELETE %%a)
:: Добавление групповых сетевых подключений
IF EXIST "%~dp0rs_list.txt" (
FOR /F "usebackq tokens=1,2,3 eol=; delims=|" %%a IN (`TYPE "%~dp0rs_list.txt"^|FIND "\\"`) DO (
"%~dp0dsquery.exe" user -d "%USERDOMAIN%" -samid "%USERNAME%"|^
%~dp0dsget.exe user -memberof -expand |1>NUL 2>&1 findstr /I /C:"CN=%%~c,"&&(
1>NUL 2>&1 NET USE %%a: %%b /PERSISTENT:NO)))
:: Выполнение групповых скриптов
IF EXIST "%~dp0sc_list.txt" (
FOR /F "usebackq tokens=1,2 eol=; delims=|" %%a IN (`TYPE "%~dp0sc_list.txt"^|FINDSTR /I ".cmd .bat"`) DO (
"%~dp0dsquery.exe" user -d "%USERDOMAIN%" -samid "%USERNAME%"|^
%~dp0dsget.exe user -memberof -expand |1>NUL 2>&1 findstr /I /C:"CN=%%~b,"&&(
IF EXIST "%~dp0%%~a" CALL "%~dp0%%~a")))
:: Добавление пользовательских сетевых подключений
IF EXIST "%~dp0%USERNAME%\rs_list.txt" (
FOR /F "usebackq tokens=1,2 eol=; delims=|" %%a IN (`TYPE "%~dp0%USERNAME%\rs_list.txt"^|FIND "\\"`) DO (
1>NUL 2>&1 NET USE %%a: %%b /PERSISTENT:NO))
:: Выполнение пользовательских скриптов
IF EXIST "%~dp0%USERNAME%\sc_list.txt" (
FOR /F "usebackq tokens=1 eol=; delims=|" %%a IN (`TYPE "%~dp0%USERNAME%\sc_list.txt"^|FIND /I ".cmd"`) DO (
IF EXIST "%~dp0%%~a" CALL "%~dp0%%~a"))
)
EXIT 0
  • Блог пользователя yurkesha

Интерпретатор CMD — вывод переменных со спецсимволами на экран и в файл

Известная, но слабоосвещенная тема — обработка в коммандном интерпретаторе CMD данных со спецсимволами.
В большинстве ситуаций она вполне решаема…
Плюс к этому периодически возникают задачи вывода в файл без перевода строки.
Несколько лет назад (на ру-борде) я выкладывал семпловый код, который достаточно подробно иллюстрирует примеры возможной работы:

@ECHO OFF&CLS
(ECHO Обработка переменных со спецсимволами «(«, «)»,»&»,»|»,»>»,»<«)
(ECHO SET «AAA=сообщение()&|<>команда»)
SET «AAA=сообщение()&|<>команда»

(ECHO Вывод на экран с обрамляющими двойными кавычками)
(ECHO ^(ECHO «%%AAA%%»^))
(ECHO «%AAA%»)

(ECHO Перенаправление значения переменной в файл file.txt с переводом строки то есть с CR/LF)
(ECHO с заменой существующего file.txt)
(ECHO SET /p»=%%AAA%%»^<nul 1^>file.txt^&ECHO.^>^>file.txt)
SET /p»=%AAA%»<nul 1>file.txt&ECHO.>>file.txt

(ECHO Перенаправление значения переменной в файл file.txt с переводом строки то есть с CR/LF)
(ECHO дописыванием в существующий file.txt)
(ECHO SET /p»=%%AAA%%»^<nul 1^>^>file.txt^&ECHO. 1^>^>file.txt)
SET /p»=%AAA%»<nul 1>>file.txt&ECHO. 1>>file.txt

(ECHO Перенаправление значения переменной в файл file1.txt без перевода строки то есть без CR/LF)
(ECHO с заменой существующего file1.txt)
(ECHO SET /p»=%%AAA%%»^<nul 1^>file1.txt)
SET /p»=%AAA%»<nul 1>file1.txt

(ECHO Перенаправление значения переменной в файл file1.txt без перевода строки то есть без CR/LF)
(ECHO с дописыванием в существующий file1.txt)
(ECHO SET /p»=%%AAA%%»^<nul 1^>^>file1.txt)
SET /p»=%AAA%»<nul 1>>file1.txt

(ECHO Вывод значения переменной на экран с переводом строки то есть с CR/LF)
(ECHO SET /p»=%%AAA%%»^<nul^&ECHO.)
(ECHO ^(ECHO Вторая строка^))
SET /p»=%AAA%»<nul&ECHO.
(ECHO Вторая строка)

(ECHO Вывод значения переменной на экран без перевода строки то есть с CR/LF)
(ECHO SET /p»=%%AAA%%»^<nul)
(ECHO ^(ECHO Вторая строка^))
SET /p»=%AAA%»<nul
(ECHO Вторая строка)

(ECHO Метод предварительной подготовки: )
(ECHO SET «AAA=%%AAA:&=^&%%»)
(ECHO SET «AAA=%%AAA:|=^|%%»)
(ECHO SET «AAA=%%AAA:<=^<%%»)
(ECHO SET «AAA=%%AAA:>=^>%%»)
(ECHO SET «AAA=%%AAA:(=^(%%»)
(ECHO SET «AAA=%%AAA:)=^)%%»)
SET «AAA=%AAA:&=^&%»
SET «AAA=%AAA:|=^|%»
SET «AAA=%AAA:<=^<%»
SET «AAA=%AAA:>=^>%»
SET «AAA=%AAA:(=^(%»
SET «AAA=%AAA:)=^)%»
(ECHO ^(ECHO %%AAA%%^))
(ECHO %AAA%)
PAUSE

Кодировка приведенного скрипта CP866.
Для ознакомления рекомендуется выполнить ;)
Само-собой в случае вопросов постараюсь ответить…

  • Блог пользователя yurkesha

Логофф сессий отключенных пользователей на терминальном сервере

Убить все отключенные сессии:

@ECHO OFF
FOR /F "USEBACKQ TOKENS=2 DELIMS= " %%a IN (`quser^|findstr /b /v "^>"^|findstr /i /v " ID "^|findstr /v /i "rdp-tcp"`) DO logoff %%~a
EXIT 0
  • Блог пользователя yurkesha

Перевод столбца в строку

Преобразование столбца в строку с заданными разделителями и обрамлением. Кодировка скрипта CP866.
Символ двойной кавычки при этом не удастся использовать как разделитель или обрамление.
Принцип работы такой: на входе файл с набором строк, на выходе файл с одной строкой где исходные строки обрамлены нужным разделителем(на самом деле набором символов) и разделяются заданным разделителем(также набор символов).

@ECHO OFF
:: Принимает четыре параметра из командной строки:
:: 1 - имя файла для обработки (обязательный!)
:: 2 - имя файла результата (необязательный!)
:: при незаданном параметре будет использован файл result.txt в текущей папке
SET FILE_RESULT=%~dp0result.txt
:: 3 - последовательность символов как разделитель для вывода (необязательный!)
:: при незаданном параметре будет использован символ
:: При указании ОБЯЗАТЕЛЬНО параметр брать в двойные кавычки!!!
SET RAZDEL=,
:: 4 - символ который будет использоваться для обрамления вывода по текущей строке (необязательный!)
:: при незаданном параметре будет использован символ '
:: При указании ОБЯЗАТЕЛЬНО параметр брать в двойные кавычки!!!
SET FRAME='
IF %~1==(
ECHO Имя файла для обработки не указано!
GOTO :ERR
)

IF NOT EXIST %~1 (
ECHO Файл для обработки не существует!
GOTO :ERR
)
IF NOT %~2== (
SET FILE_RESULT=%~2
)
IF NOT %~3== (
SET RAZDEL=%~3
)
IF NOT %~4== (
SET FRAME=%~4
)
FOR /F usebackq tokens=* delims= %%a IN (%~1) DO CALL :MAIN %%~a
GOTO :EOF
:MAIN
SET /p=%RAZDEL1%%FRAME%%~1%FRAME%<nul>>%FILE_RESULT%
SET RAZDEL1=%RAZDEL%
GOTO :EOF
:ERR
PAUSE
GOTO :EOF
  • Блог пользователя yurkesha

Подключение к WSUS машины не входящей в домен

Идем в ветку реестра

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate]

Экспортируем ее в файл и добавляем в реестр ПК в домен не входящих. Это приведет к обращению на сервер указанный в ветке. Для проверки запускаем в командной строке: wuauclt /detectnow Смотрим логи C:\windows\WindowsUpdate.log, идем в конец файла и находим строку вида:

2013-03-23 18:13:56:375  908 1074 PT +++++++++++  PT: Synchronizing server updates  +++++++++++
2013-03-23 18:13:56:375  908 1074 PT   + ServiceId = {3DA21691-E39D-4DA6-8A4B-B43877BCB1B7}, Server URL = http://192.168.0.10:8080/ClientWebService/client.asmx

где 192.168.0.10 — имя сервера, на котором у нас крутится WSUS. Если нет, проверяем значения ключей реестра

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\WUServer HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\WUStatusServer

их значение должно соответствовать 192.168.0.10:8080. Если соответствует, перезагружаемся и проверяем еще раз, если не соответствует — правим значения, перезагружаемся, проверяем. Ветка реестра, отвечающий за настройку политики WindowsUpdate в общем виде выглядит приблизительно так:

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate]
"AcceptTrustedPublisherCerts"=dword:00000001
"WUServer"="http://192.168.0.10:8080"
"WUStatusServer"="http://192.168.0.10:8080"

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU]
"NoAutoUpdate"=dword:00000000
"AUOptions"=dword:00000003
"ScheduledInstallDay"=dword:00000000
"ScheduledInstallTime"=dword:00000010
"NoAutoRebootWithLoggedOnUsers"=dword:00000001
"DetectionFrequencyEnabled"=dword:00000001
"DetectionFrequency"=dword:00000008
"UseWUServer"=dword:00000001
  • Блог пользователя serg kaac

Преобразование файлов 1CClientBankExchange в табличную форму

Лично мне периодически приходилось сталкиваться с обработкой данных не в табличной форме, а в «именованном формате» то есть когда каждый параметр пишется на отдельной строке в виде Параметр=Значение параметра причем файл имеет строго выраженную периодическую структуру и любой из параметров является необязательным. Порядок следования — произвольный.
Пример такого формата — стандартный формат 1С для общения с клиент-банками 1CClientBankExchange
На самом деле его можно даже средствами «малой механизации»(то есть чистый CMD-скрипт) преобразовать в табличную форму для дальнейшей обработки.
Привожу пример скрипта(кодировка Win1251):

@ECHO OFF CLS
COLOR 1E
CHCP 1251>NUL 2>&1

:: Папка с файлами для обработки. Полный путь в WIN1251!!!
SET "INPUT_DIR=D:\DATABASE"
:: Папка с выходными файлами. Полный путь в WIN1251!!!
:: Может совпадать с исходной папкой.
SET "OUTPUT_DIR=D:\DATABASE"
:: Папка для бэкапа входящих файлов. Полный путь в WIN1251!!!
:: Если она не указана то исходные файлы удалятся!!!
SET "BACKUP_DIR=D:\DATABASE\BACKUP"
:: Маска файлов для обработки
SET "FILE_MASK=to1c????.txt"

IF NOT EXIST "%INPUT_DIR%\" (ECHO Папка с файлами для обработки не найдена!&GOTO ERROR)
2>NUL DIR /B "%INPUT_DIR%\%FILE_MASK%"|1>NUL FIND /C /V ""||(ECHO Файлы для обработки не найдены!&GOTO ERROR)
IF NOT EXIST "%OUTPUT_DIR%" MD "%OUTPUT_DIR%"
IF NOT EXIST "%OUTPUT_DIR%" ECHO Не удалось создать папку для выходных файлов!&GOTO ERROR
IF NOT "%BACKUP_DIR%"=="" (
IF NOT EXIST "%BACKUP_DIR%" MD "%BACKUP_DIR%"
IF NOT EXIST "%BACKUP_DIR%" ECHO Не удалось создать папку для бэкапа файлов!&GOTO ERROR
)

ECHO Представляем системную дату в удобном виде:
ECHO wscript.ECHO YEAR(DATE) ^& RIGHT(0 ^& MONTH(DATE),2) ^& RIGHT(0 ^& DAY(DATE),2)>"%TEMP%\tmp.vbs"
FOR /F %%i IN ('cscript "%TEMP%\tmp.vbs" //Nologo') DO SET "TEKDATA=%%i"
IF EXIST "%TEMP%\tmp.vbs" DEL "%TEMP%\tmp.vbs"
ECHO %TEKDATA%

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq" %%i IN (`DIR /B /A:-D "%INPUT_DIR%\%FILE_MASK%"`) DO (0<"%INPUT_DIR%\%%i" SET /P "LINE_1=") &IF /I "!LINE_1!"=="1CClientBankExchange" CALL :MAIN "%%i"
ENDLOCAL
EXIT

:MAIN
SET "FILE_NAME=%~1"
SET "NEWFILE_NAME=%TEKDATA%_%~1"
ECHO Выводим строку заголовков
1>"%OUTPUT_DIR%\%NEWFILE_NAME%" (ECHO 01-Номер^|02-Дата^|03-Сумма^|04-ДатаСписано^|05-Плательщик^|06-ПлательщикИНН^|07-ПлательщикКПП^|08-Плательщик1^|09-ПлательщикСчет^|10-ПлательщикРасчСчет^|11-ПлательщикБанк1^|12-ПлательщикБанк2^|13-ПлательщикБИК^|14-ПлательщикКорсчет^|15-ДатаПоступило^|16-Получатель^|17-ПолучательИНН^|18-ПолучательКПП^|19-Получатель1^|20-ПолучательСчет^|21-ПолучательРасчСчет^|22-ПолучательБанк1^|23-ПолучательБанк2^|24-ПолучательБИК^|25-ПолучательКорсчет^|26-ВидПлатежа^|27-ВидОплаты^|28-СтатусСоставителя^|29-ПоказательКБК^|30-ОКАТО^|31-ПоказательОснования^|32-ПоказательПериода^|33-ПоказательНомера^|34-ПоказательДаты^|35-ПоказательТипа^|36-СрокПлатежа^|37-Очередность^|38-НазначениеПлатежа^|39-Имя файла для приема^|)

SET "POINTER="
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "USEBACKQ TOKENS=1* DELIMS== " %%a in ("%INPUT_DIR%\%FILE_NAME%") DO (
IF "%%a"=="КонецДокумента" CALL :TOFILE&SET "POINTER=0"
IF !POINTER!==1 (IF NOT "%%~b"=="" (
IF /I "%%~a"=="Номер" SET "PP_1=%%~b"
IF /I "%%~a"=="Дата" SET "PP_2=%%~b"
IF /I "%%~a"=="Сумма" SET "PP_3=%%~b"
IF /I "%%~a"=="ДатаСписано" SET "PP_4=%%~b"
IF /I "%%~a"=="Плательщик" SET "PP_5=%%~b"
IF /I "%%~a"=="ПлательщикИНН" SET "PP_6=%%~b"
IF /I "%%~a"=="ПлательщикКПП" SET "PP_7=%%~b"
IF /I "%%~a"=="Плательщик1" SET "PP_8=%%~b"
IF /I "%%~a"=="ПлательщикСчет" SET "PP_9=%%~b"
IF /I "%%~a"=="ПлательщикРасчСчет" SET "PP_10=%%~b"
IF /I "%%~a"=="ПлательщикБанк1" SET "PP_11=%%~b"
IF /I "%%~a"=="ПлательщикБанк2" SET "PP_12=%%~b"
IF /I "%%~a"=="ПлательщикБИК" SET "PP_13=%%~b"
IF /I "%%~a"=="ПлательщикКорсчет" SET "PP_14=%%~b"
IF /I "%%~a"=="ДатаПоступило" SET "PP_15=%%~b"
IF /I "%%~a"=="Получатель" SET "PP_16=%%~b"
IF /I "%%~a"=="ПолучательИНН" SET "PP_17=%%~b"
IF /I "%%~a"=="ПолучательКПП" SET "PP_18=%%~b"
IF /I "%%~a"=="Получатель1" SET "PP_19=%%~b"
IF /I "%%~a"=="ПолучательСчет" SET "PP_20=%%~b"
IF /I "%%~a"=="ПолучательРасчСчет" SET "PP_21=%%~b"
IF /I "%%~a"=="ПолучательБанк1" SET "PP_22=%%~b"
IF /I "%%~a"=="ПолучательБанк2" SET "PP_23=%%~b"
IF /I "%%~a"=="ПолучательБИК" SET "PP_24=%%~b"
IF /I "%%~a"=="ПолучательКорсчет" SET "PP_25=%%~b"
IF /I "%%~a"=="ВидПлатежа" SET "PP_26=%%~b"
IF /I "%%~a"=="ВидОплаты" SET "PP_27=%%~b"
IF /I "%%~a"=="СтатусСоставителя" SET "PP_28=%%~b"
IF /I "%%~a"=="ПоказательКБК" SET "PP_29=%%~b"
IF /I "%%~a"=="ОКАТО" SET "PP_30=%%~b"
IF /I "%%~a"=="ПоказательОснования" SET "PP_31=%%~b"
IF /I "%%~a"=="ПоказательПериода" SET "PP_32=%%~b"
IF /I "%%~a"=="ПоказательНомера" SET "PP_33=%%~b"
IF /I "%%~a"=="ПоказательДаты" SET "PP_34=%%~b"
IF /I "%%~a"=="ПоказательТипа" SET "PP_35=%%~b"
IF /I "%%~a"=="СрокПлатежа" SET "PP_36=%%~b"
IF /I "%%~a"=="Очередность" SET "PP_37=%%~b"
IF /I "%%~a"=="НазначениеПлатежа" SET "PP_38=%%~b"
SET "PP_39=\\%COMPUTERNAME%@%INPUT_DIR%\%FILE_NAME%"
)
)
IF /I "%%a"=="СекцияДокумент" (SET "POINTER=1"&CALL :INIT)
)
ENDLOCAL
ECHO Формирование файла "%OUTPUT_DIR%\%NEWFILE_NAME%" формата текст с разделителями завершено!
IF NOT EXIST "%BACKUP_DIR%\%TEKDATA%" MD "%BACKUP_DIR%\%TEKDATA%"
IF NOT "%BACKUP_DIR%"=="" (MOVE /Y "%INPUT_DIR%\%FILE_NAME%" "%BACKUP_DIR%\%TEKDATA%") ELSE (DEL /Q /F "%INPUT_DIR%\%FILE_NAME%")
GOTO :EOF

:INIT
FOR /L %%z IN (1,1,39) DO SET "PP_%%z="
GOTO :EOF

:TOFILE
ECHO Вывод платежки N %PP_1% в файл "%OUTPUT_DIR%\%NEWFILE_NAME%"
FOR /L %%z IN (1,1,39) DO (
1>>"%OUTPUT_DIR%\%NEWFILE_NAME%" SET /P "=!PP_%%z!^|" >"%OUTPUT_DIR%\%NEWFILE_NAME%" (ECHO.)
GOTO :EOF

:ERROR
PAUSE
COLOR

При этом получим на выходе текстовый файл с разделителем «|» между полями.
Первой строкой будет выведена строка заголовков:
01-Номер|
02-Дата|
03-Сумма|
04-ДатаСписано|
05-Плательщик|
06-ПлательщикИНН|
07-ПлательщикКПП|
08-Плательщик1|
09-ПлательщикСчет|
10-ПлательщикРасчСчет|
11-ПлательщикБанк1|
12-ПлательщикБанк2|
13-ПлательщикБИК|
14-ПлательщикКорсчет|
15-ДатаПоступило|
16-Получатель|
17-ПолучательИНН|
18-ПолучательКПП|
19-Получатель1|
20-ПолучательСчет|
21-ПолучательРасчСчет|
22-ПолучательБанк1|
23-ПолучательБанк2|
24-ПолучательБИК|
25-ПолучательКорсчет|
26-ВидПлатежа|
27-ВидОплаты|
28-СтатусСоставителя|
29-ПоказательКБК|
30-ОКАТО|
31-ПоказательОснования|
32-ПоказательПериода|
33-ПоказательНомера|
34-ПоказательДаты|
35-ПоказательТипа|
36-СрокПлатежа|
37-Очередность|
38-НазначениеПлатежа|
39-Имя файла для приема|

Само-собой не в приведенном виде — а в виде одной строки ;)

  • Блог пользователя yurkesha

Страховое копирование по списку

Относительно простой вариант резервного копирования по списку файлов/папок с учетом типа резервной копии и количества хранимых копий по типам. Классические типы: дневная-недельная-месячная-годовая копии. Кодировка скрипта CP866. В приаттаченом файле содержится сам скрипт, пример файла списка и консольные версии архиватора 7z.

@ECHO OFF CLS SETLOCAL :: Папка для хранения резервных копий - если надо не текущую впишите свою
:: По-умолчанию текущая папка
SET "BACKUP_DIR=%~dp0"
:: Постоянная часть имени архива - для уникальной идентефикации архива
SET "SOURCE_NAME=MY_BACKUP_1"
:: Имя и месторасположение файла со списком для резервного копирования
:: Имена папок пишутся либо с символом "\" в конце либо без него
SET "BACKUP_LIST=%~dp0listbackup.txt"
:: Кодировка списка - доступные варианты: UTF-8, WIN, DOS
SET "LIST_CHARSET=DOS"
::Тип бэкапа: DAY, WEEK, MONTH, YEAR
::День недели число
::1 - Воскресенье
::2 - Понедельник
::3 - Вторник
::4 - Среда
::5 - Четверг
::6 - Пятница
::7 - Суббота
:: День недели бэкап которого считается недельным
SET "WEEK_DAY_BACKUP=1"
:: Бэкап последнего числа месяца считается месячным
:: Бэкап последнего числа года считается годовым
:: Количество хранимых страховых копий по типам
SET "REZERV_DAY=6"
SET "REZERV_WEEK=5"
SET "REZERV_MONTH=11"
SET "REZERV_YEAR=50"
:: ПРИМЕЧАНИЕ: имя архива будет иметь маску:
:: %SOURCE_NAME%_ГГГГ_ММ_ДД_%TYPE_BACKUP%.7z
ECHO.
ECHO %DATE% - %TIME:~0,5% Резервное копирование:
ECHO тело скрипта: "%~0"
ECHO список: "%BACKUP_LIST%"
CALL :BACKUP_PATHS
CALL :SYS_TYPE
CALL :DATE_SET
CALL :ARC
CALL :REZERV_TRIM
ENDLOCAL
GOTO :EOF
:SYS_TYPE
:: Определение разрядности системы для использования нужной версии архиватора
ECHO "%PROCESSOR_ARCHITECTURE%""%PROCESSOR_ARCHITEW6432%"|1>NUL 2>NUL FIND /I "AMD64"&&SET "ARC_DIR=%~dp0X64"||SET "ARC_DIR=%~dp0X32"
:: Отбрасывание последнего символа "\" в пути архиватора
SET "LAST_CHAR=%ARC_DIR:~-1%"
IF "%LAST_CHAR%"=="\" SET "ARC_DIR=%ARC_DIR:~0,-1%"
IF NOT EXIST "%ARC_DIR%\7z.exe" (
ECHO Не обнаружен архиватор по пути "%ARC_DIR%"!
ECHO Положите соответсвующую версию 7z.exe по пути .\X64 или .\X86 и перезапустите скрипт!
CALL :ERROR 1
)
GOTO :EOF
:BACKUP_PATHS
:: Отбрасывание последнего символа "\" в пути бэкапа
SET "LAST_CHAR=%BACKUP_DIR:~-1%"
IF "%LAST_CHAR%"=="\" SET "BACKUP_DIR=%BACKUP_DIR:~0,-1%"
:: Создание целевой папки для резервного копирования с проверкой существования
IF NOT EXIST "%BACKUP_DIR%" MD "%BACKUP_DIR%"
IF NOT EXIST "%BACKUP_DIR%" (
ECHO Указанная папка для создания копии недоступна!
CALL :ERROR 2
)
ECHO Резервная копия будет создана в папке %BACKUP_DIR%
GOTO :EOF
:DATE_SET
:: Определение параметров текущей даты и типа бэкапа
ECHO wscript.ECHO YEAR(DATE)^&"_"^&RIGHT(0^&MONTH(DATE),2)^&"_"^&RIGHT(0^&DAY(DATE),2)^&"_"^& DAY(DATESERIAL(YEAR(DATE),MONTH(DATE)+1,1-1))^&"_"^&WEEKDAY(DATE)^&"_"^&RIGHT(0^&hour(TIME),2)^&"_"^&RIGHT(0^&minute(TIME),2) 1>"%TEMP%\tmp.vbs"
FOR /F "TOKENS=1,2,3,4,5,6,7 DELIMS=_" %%a IN ('cscript "%TEMP%\tmp.vbs" //Nologo') DO SET "TEK_YEAR=%%a"&SET "TEK_MONTH=%%b"&SET "TEK_DAY=%%c"&SET "LAST_DAY=%%d"&SET "TEK_WEEK=%%e"&SET "TEK_HOUR=%%f"&SET "TEK_MINUTE=%%g"
IF EXIST "%TEMP%\tmp.vbs" DEL "%TEMP%\tmp.vbs"
SET "TYPE_BACKUP=DAY"
IF "%WEEK_DAY_BACKUP%"=="%TEK_WEEK%" SET "TYPE_BACKUP=WEEK"
IF "%TEK_DAY%"=="%LAST_DAY%" SET "TYPE_BACKUP=MONTH"
IF "%TEK_MONTH%"=="12" (IF "%TEK_DAY%"=="%LAST_DAY%" (SET "TYPE_BACKUP=YEAR"))
CALL SET "REZERV_NUM=%%REZERV_%TYPE_BACKUP%%%"
SET "ARC_NAME=%SOURCE_NAME%_%TEK_YEAR%_%TEK_MONTH%_%TEK_DAY%~%TYPE_BACKUP%"
IF EXIST "%BACKUP_DIR%\%ARC_NAME%.7z" (
ECHO Резервная копия за этот день уже создана!
ECHO Удалите или переименуйте ее и запустите скрипт повторно!
CALL :ERROR 3
)
GOTO :EOF
:ARC
IF EXIST "%ARC_DIR%\7z.exe" (
1>nul "%ARC_DIR%\7z.exe" a -t7z "%BACKUP_DIR%\%ARC_NAME%.7z" @"%BACKUP_LIST%" -ms -mmt -mx7 -bd -scs%LIST_CHARSET% -ssw
ECHO Страховая копия создана...
)
GOTO :EOF
:REZERV_TRIM
ECHO Удалены устаревшие резервные копии:
FOR /F "SKIP=%REZERV_NUM% USEBACKQ TOKENS=1 DELIMS=" %%a IN (`DIR /O:-N /B "%BACKUP_DIR%\%SOURCE_NAME%_20??_??_??~%TYPE_BACKUP%.7z"`) DO (
DEL /Q /F "%BACKUP_DIR%\%%a"
ECHO "%BACKUP_DIR%\%%a"
)
GOTO :EOF
:ERROR
ECHO Произошло досадное недоразумение - резервная копия не создана ...
ENDLOCAL
EXIT %~1

В случае необходимости ведения лога перенаправьте информационные сообщения скрипта в файл.

  • Блог пользователя yurkesha

Чтение данных из реестра в переменную окружения

Продвинутый кросплатформенный модуль для встраивания в скрипты, позволяющий в удобной форме получать данные из реестра для дальнейшего использования(как всегда — кодировка скрипта CP866):

@ECHO OFF
CLS
:: 1-й параметр = раздел реестра для чтения
:: 2-й параметр = имя считываемого параметра - указать пустые кавычки для чтения параметра "По умолчанию"
:: 3-й параметр = имя переменной окружения куда считываем результат
:: Пример чтения именованного параметра:
CALL :REG_READ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" "USERINIT" "MY_VAR1"
ECHO Именованный параметр значение "%MY_VAR1%"
ECHO Именованный параметр тип "%MY_VAR1_TYPE%"
:: Пример чтения безымянного параметра "По умолчанию"
CALL :REG_READ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" "" "MY_VAR2"
ECHO Параметр "По умолчанию" значение "%MY_VAR2%"
ECHO Параметр "По умолчанию" тип "%MY_VAR2_TYPE%"
GOTO :EOF
:REG_READ
IF NOT "%~2"=="" (
FOR /F "USEBACKQ TOKENS=* DELIMS=" %%a IN (`REG QUERY "%~1" /v "%~2" 2^>NUL ^|FINDSTR /I /B /C:" %~2" 2^>NUL`) DO CALL :TRANSLATE "%%a" "%~2" "%~3"
1>NUL 2>&1 REG QUERY "%~1" /v "%~2"||(SET "%~3_TYPE="&SET "%~3=!")
)
IF "%~2"=="" (
FOR /F "USEBACKQ TOKENS=* DELIMS=" %%a IN (`REG QUERY "%~1" /ve 2^>NUL ^|FINDSTR /B /C:" " 2^>NUL`) DO CALL :TRANSLATE "%%a" "" "%~3"
1>NUL 2>&1 REG QUERY "%~1" /ve||(SET "%~3_TYPE="&SET "%~3=!")
)
CALL SET "%~3=%%%~3:~0,-1%%"
GOTO :EOF
:TRANSLATE
SET "TEMP_STR=%~1"
IF NOT "%~2"=="" CALL SET "TEMP_STR=%%TEMP_STR:*%~2=%%"
CALL SET "TEMP_STR=%%TEMP_STR:*REG_=%%"
FOR /F "USEBACKQ TOKENS=1*" %%a IN ('"%TEMP_STR%"') DO (
SET "%~3_TYPE=REG_%%~a"
SET "%~3=%%~b"
)
GOTO :EOF

Добавлена возможность чтения из реестра параметра «По-умолчанию».
Проверена работа на WinXP, Win7, Win7x64.

Предполагается что будет корректно работать на всех системах начиная с WinXP и до Win8 включительно.
Не учитывается  ненативный запуск — то есть если на x64 вызвать cmd-скрипт из x86-окружения то подмена HKLM\SOFTWARE\ на HKLM\SOFTWARE\Wow6432Node\ произойдет без вашего желания. Прямой же запуск скрипта ВСЕГДА отрабатывает в нативном окружении.

ЗЫ — Что делать с «(значение параметра не задано)» на системах Win7(судя по всему Vista и старше) для пустого параметра «По умолчанию» я пока в универсальном виде не придумал. Такое значение автоматически присваивается неописанному пустому параметру и, к сожалению, оно зависимо от языка системы.

  • Блог пользователя yurkesha

  • Скрипт на python под windows
  • Скрыть все значки с рабочего стола windows 10 горячие клавиши
  • Скрыт рабочий стол windows 10 как его вернуть рабочий стол
  • Слабая чувствительность микрофона windows 10
  • Скрипт копирования файлов из папки в папку windows