Knowledge and understanding of CMD command syntax under Window

1, What is cmd?

cmd is a command line window running under windows system. You can open the run box through Win+R, enter cmd and press enter to open it.

cmd, like the command line under linux, has powerful operation capabilities including file management, network operation, text viewing and process operation. Although the command line under linux is not included, some commands under windows can still help us solve some problems and improve efficiency.

2, Common commands

cd enters the file directory

dir list directory files

/A          Displays the file with the specified properties.
  attribute         D  catalogue                R  Read only file
               H  Hide file            A  Prepare archived files
               S  System file            I  No content index file
               L  Analytic point             -  Prefix indicating "no"
  /B          Use empty format(No title information or summary). 
  /C          Displays the thousands separator in the file size. This is the default. use /-C come
              Disables the display of delimiters.
  /D          It is the same as wide format, but the documents are listed by column classification.
  /L          Use lowercase.
  /N          New long list format with the file name on the far right.
  /O          List files in sort order.
  Arrangement order     N  By name(Alphabetical order)     S  By size(from small to large)
               E  By extension(Alphabetical order)   D  By date/time(From first to last)
               G  Group directory priority           -  Prefix in reverse order
  /P          Pause after each information screen.
  /Q          Displays the file owner.
  /R          Displays the alternate data stream for the file.
  /S          Displays the files in the specified directory and all subdirectories.
  /T          Controls the time field displayed or used for classification.
  time slot      C  Creation time
              A  Last visited
              W  Last written time
  /W          Use wide list format.
  /X          Display as non-8.3 The short name generated by the file name. Format is /N Format,
              The short name is inserted before the long name. If there is no short name, in its location
              Display blank.
  /4          Displays the year with four digits
  • Common commands
  1. Recursively list the files at the end of doc under this path

dir /s /b *.doc

  1. View the existing directory under the local path

dir /aD

  • Copy copy file
  • xcopy copy directory
  • move shear
  • ren rename
  • Replace replace
replace /a Path 1 path 2  # Add the file to a new place, similar to copying

Customize cmd interface

title rename cmd name

title this is test

mode sets the window size (width, height)

mode 100,50

colour sets the background and font color of cmd

0 = black       8 = grey
1 = blue       9 = Light blue
2 = green       A = Light green
3 = Light green     B = Light green
4 = gules       C = Light red
5 = purple       D = lavender
6 = yellow       E = canary yellow
7 = white       F = Bright white

"COLOR fc" # produces light red on bright white

variable

  1. Set the variable, taking the name variable as an example

set name=xxx

  1. Print variables

set name

  1. Release variable

set name=

Common parameters

  • set /a expression

set /a var name=4/2

  • The set /p command line switch allows the variable value to be set to one line of user input
@echo off
set /p name=Please enter a number
echo The number you entered is:%name%
pause >nul

Permanent variable settings

setx path "%path%; folder directory"

Special characters

  • |Pipe symbol
  • &Similar and
  • &&Execute the following after the previous execution is successful
  • ||The previous execution failed before the subsequent execution
  • () for the sake of beauty
# dir executes successfully, print sucess, otherwise false
dir && echo sucess || echo false

wildcard

  • ? Represents a single character

dir /b ??.mp4 # lists mp4 files with two character names

  • *Represent all

dir /b *.mp4 # lists all mp4 files

Logical structure

if syntax format

  1. To determine whether a file or folder exists, use the if exist statement

if exsit f:\1.txt (echo sucess) else (echo false)

  1. Determines whether two strings are equal. Use if "string 1" = = "string 2" statement

if "1"=="1" (echo sucess) else (echo false)

  1. To determine whether a variable has been assigned, use the if defined str statement

if defined name (echo succse)

  1. Judge whether two values are equal, if value 1 equ value 2 statement

if 2 gtr 1 (echo sucess)

  • Conditional character
  • Equal to equ equal
  • Greater than gtr greater than
  • Greater than or equal to geq
  • Less than less than
  • Less than or equal to leq
  • Not equal to neq

for syntax format

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

  %variable  Specify a single letter replaceable parameter.
  (set)      Specify a file or group of files. Wildcards can be used.
  command    Specifies the command to execute for each file.
  command-parameters
             Specify parameters or command line switches for a specific command.

Use in batch programs FOR To specify a variable, use the %%variable
 Instead of using %variable. Variable names are case sensitive, so %i differ %I.

If command extension is enabled, the following are supported FOR Other formats for commands:

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

    If the set contains wildcards, the specified matches the directory name, not the file name.

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

    Check to [drive:]path Is the root directory tree, pointing to the directory in each directory FOR sentence.
    If in /R If no directory specification is specified, the current directory is used. If the set is only a single point(.)Characters,
    Enumerate the directory tree.

FOR /L %variable IN (start,step,end) DO command [command-parameters]
This set represents a sequence of numbers in increments from start to end. So,(1,1,5)The sequence will be generated
    1 2 3 4 5,(5,-1,1)The sequence will be generated(5 4 3 2 1)

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

    Or, if so usebackq option:

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

    fileset Is one or more file names. Continue to fileset Before the next file in,
    Each file is opened, read, and processed. Processing includes reading the file and dividing it into lines of text,
    Each line is then parsed into zero or more symbols. Then call with the found symbolic string variable value For Cycle.
    By default,/F By the first white space symbol separated in each line of each file. Skip blank lines.
    You can specify an optional "options" Parameter overrides the default parsing operation. The quoted string includes a
    Or more keywords that specify different parsing options. These keywords are:

        eol=c           - Refers to the end of a line comment character(Just one)
        skip=n          - Refers to the number of lines ignored at the beginning of the file.
        delims=xxx      - Refers to the separator set. This replaces spaces and tabs
                          Default separator set.
        tokens=x,y,m-n  - It refers to which symbol of each line is passed to each iteration
                          of for Itself. This results in the assignment of additional variable names. m-n
                          The format is a range. adopt nth Symbol assignment mth. If
                          The asterisk of the last character in the symbol string,
                          Then the additional variables will be resolved after the last symbol
  1. View the directory file under the current file

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

  1. Separated by spaces from the specified file, open after the second column and output the contents of each line

for /F "token=2-3,delims=" %i in (e:\1.txt) do echo %i

Redirection operator

>Output > > increase output < input

Numeric code description of the handle

  • 0: keyboard input
  • 1: Output to command prompt window
  • 2: Error output

Di > right. TXT 2 > error.txt # if there is an error, output it to error.txt

Network command

ping

ping host -t # Keep ping
ping host -n count # Number of packets sent

ifconfig

ipconfig /all # View all network configuration information
ipconfig /renew # Update all adapters and obtain ip again
ipconfig /release # Release ip
ipconfig /flushdns # Refresh dns

netstat displays the current network protocol connection

netstat -ano # view the current connection

net

  1. net user # view user information
  2. lusrmgr.msc # graphical user management
  3. net localgroup # local user group management
  4. net share

net share f=F: / share F disk net share f /delete cancel sharing net start + service # start service

netsh

Netsh dump > path # backup all network settings

netsh int ip # view network card settings set address name="wlan" source=static addr=192.168.0.2 mask=255.255.255.0 gateway=192.168.0.1 # set static ip set address name="wlan" source=dhcp # automatically obtain ip

netsh winsock reset # reinstall the network protocol ping when the local network is unavailable

Netsh firewall set opmode = enable # enable firewall

netsh wlan show profiles # view the saved network profiles of the current system

netsh wlan show profiles name="ssid" key=clear | findstr key content # view the information of a ssid, including the password

Advanced command

attrib

Type nul > 1.txt # create an empty file attrib +h 1.txt # set the file as hidden

goto jump function

goto part1

:part1
echo test
exit

call caller

call demo.bat

start opens the program

net use IPC

  1. Establish an empty connection

net use \IP\ipc$ "" /user:""

  1. Establish a non empty connection

net use \IP\ipc $"user name" / user: "password"

  1. Map the default share and map the opposite c disk to the local z disk

net use z: \IP\c $"password" / user: "user name"

  1. Delete an IPC $connection

net use \IP\ipc$ /del

  1. Delete shared mapping

net use c: /del

reg add delete modify query registry command (regedit is a graph table)

REG Operation [Parameter List]

  Operation  [ QUERY   | ADD    | DELETE  | COPY    |
               SAVE    | LOAD   | UNLOAD  | RESTORE |
               COMPARE | EXPORT | IMPORT  | FLAGS ]

case

  1. ping the intranet to find the surviving host

For / L%% I in (1,1255) do Ping - N 1 192.168.0.% I | findstr bytes > > iplist.txt

  1. Batch create files or folders

For / L%% i in (1,1100) do MD new folder% i

  1. Batch find file names with specific suffixes

for /r f:\ %i in (*.txt) do echo %%i

  1. Retrieve keywords in files
@echo off
title 
:start
set /p name=Please enter the content you want to search:
type test.txt | findstr %name%
if %name%==exit (goto end) else (goto start)
:end
exit
pause >nul
  1. Set ip

netsh interface ip set address name="wlan" source=static addr=x mask=x gateway=x 1

View basic information

command

function

winver

system information

gpedit.msc

– Group Policy

regedit.exe

-Registry

eventvwr

event viewer

lusrmgr.msc

User group viewer

services.msc

Local service settings

devmgmt.msc

Device manager

compmgmt.msc

computer management

diskmgmt.msc

Disk management utility

Common tools

command

function

regedt32

-Registry Editor

calc

start calculator

taskmgr

– Task Manager (2000/xp/2003)

mmc

Open the console

write

-Writing board

notepad

– open Notepad

mspaint

– drawing board

mstsc

-Remote Desktop Connection

charmap

– start character mapping table

osk

Open on screen keyboard

magnify

– magnifying glass utility

mplayer2

-Media player

Posted on Fri, 26 Nov 2021 10:27:21 -0500 by Drachlen