Common learning cmd command

1. The internal / external distinction command

Inside: The system comes with exe files in C: \ Windows \ System32 view

Example: ping ping.exe file is commonly used in the system32
Here Insert Picture Description

Outside: third party exe files are DOS applications

Example: xcopy copy the directory and all its files contain commands
Here Insert Picture Description

2. Common Commands

command/? View help
command /? | More split-screen display content will not jump directly in the end
Ctrl + c to withdraw from the command
exit exit cmd

A. CMD change style

  • title change cmd window title
    format: title title
  • View mode / settings window shape
    Format: mode column, row
  • Set background color cmd font color
    format: color back to the default colors black and white
    color font color, background
    color 1c background in blue light red
    Here Insert Picture Description
  • Other cmd attribute settings - Right-click the title bar
    Here Insert Picture Description

B. Change directory

  • cd open the specified file directory
    format: cd path to
    a directory on the cd ...
    cd current directory.
    cd View current path
    cd / root directory of the current drive letter
  • Drive: Switch to the specified letter
    format: F:
  • dir view the files in the current directory
  • dir / AH to view hidden files in the current directoryHere Insert Picture Description
  • md Create a directory
    format: md directory name
  • rd delete the directory
    format: rd directory name
  • del Delete a file
    format: del file path

C. File Operations

  • move 移动文件源文件会被删除
    格式:move 源文件路径 目标文件路径
    不在同一文件路径内 请使用绝对路径
    move c:\test.txt d:\test.txt
    源文件路径同目标文件路径相同 即对文件重命名
    move a.txt b.txt 将a重命名为b
  • copy 复制文件源文件不被删除
    格式:copy 源文件路径 目标文件路径
  • replace 替换相同文件
    格式 replace c:\a.txt d:
    将d:\ 下 a.txt 替换为同c:\a.txt 一样的文件
  • rename 重命名文件
    格式:rename 路径 名字
    缩写 ren
  • type 查看文件内容
    格式:rename 路径
  • start 打开文件
    用于打开 应用程序 文本 网址
    • start www.baidu.com 打开网址
    • start chrome 打开chrome浏览器
    • start 1.txt 打开文本
    • start . 使用文件管理器打开当前目录
    • start 路径 使用文件管理器打开指定目录
      打开不在当前路径的文件 请使用绝对路径

C.变量

  • set 设置临时变量
    set 变量名 = 值
    set 变量名 查看变量
    set 变量名 = 空 删除变量
    set /a 表达式计算 set /a 1+2 得 3
    set /p 变量名=用户输入提示 接收用户输入
  • 环境变量
    Here Insert Picture Description
    查看时间 set %DATE% %TIME%
    判断上一条执行命令执行是否成功 echo %ERRORLEVEL% 成功为0
  • 输出变量值
    echo %变量名%
  • 设置永久变量 setx PATH “%path%:文件夹路径”

D.特殊字符

字符 功能
| 管道符 将符号左边结果作为右边的参数
& 前面命令执行失败 &后的命令仍然执行
&& 只有&&前的命令正确执行 后面才执行(类似于与运算)
|| 只有前面失败了后面才执行(类似于if else 的else)
() 提升优先级
  • 通配符
    * 代表0-n个任意字符
    ? 代表1个任意字符

E.if判断语句

  • 判断文件存在
    格式:if exist 文件路径
  • 判断字符串是否相等
    格式:if ”字符串1“ == ”字符串2“
  • 判断变量是否被定义
  • if /i 忽略大小写比较
    格式:if defined 变量名 语句
  • 判断数值相等
    格式:if 数值 equ 数值
  • 数值判断符
符号 功能
equ 相等 equal
gtr 大于 greater than
gtq 大于等于 greater than or equal
lss 小于 less than
lsq 小于等于 less than or equal
neq 不等于 no equal
  • 例子
if /i "hello" == "Hello" (echo 相等) else (echo 不相等)
> 相等

F.for 循环

cmd 中变量使用 %i
编写bat脚本 使用 %%i

  • 遍历数字
for %%i in (1,2,3) do (echo %%i)
> 1 2 3
  • 遍历指定目录内的文件
for %%i in (D:\*) do (echo %%i)
  • 遍历指定目录内的文件夹
for /d in (D:\*) do (echo %%i)
  • 遍历某个文件根目录下所有指定文件
for /r D:\ %%i in (*.txt) do (echo %%i)
遍历出D:\下所有子目录内txt文件 
  • 带起止步长的循环
for /L %%i in (1,2,5) do (echo %%i)
> 1 3 5
  • 遍历文件内容
;姓名 年龄
小明   18,
小李   19,
for /f "eol=; skip=2 delims=, tokens=1-4,7-8" %i in (test.txt) do (echo %i %j)
> 小李 18
> 小明 19
eol 指定一个注释
skip=n 跳过n行
delims 行分隔符s
tokens 每行某个字符范围传递到变量内
命令解读:
循环打开test.txt 内的内容 按行提取 行与行的分割(delims)为,
;开头的行为注释(eol) 跳过两行(skip=2) %i变量为行内1-4字符 %j变量为行内7-8字符

G.注释

  • ::
  • rem

E.重定向

  • >
    Ping 192.168.1.1> ping.txt
    the ping result into ping.txt file does not automatically create this file replication have deleted the original content
  • >> As in the previous but not replication but does not remove the additional original content
  • <Parameter left to the right as a result of
    sort <text.txt contents of the file on the right sort
    type null> text.txt create an empty txt file named text

F.attrib file attributes

  • Show file attributes
    attrib file path
  • Document attributes +
    Here Insert Picture Description
    Example:
    Attrib + R & lt set file attributes named text.txt in a readable
    attrib + h + r text.txt set hidden file attribute to read
  • Clear file attributes -
    attrib -h named text.txt in the hidden file attribute cleared

G. sort sort files

  • Sort the results in a new document
(sort<text.txt) > text1.txt
  • Reverse order
    sort / r

H. infinite loop 0%

Repeat the previous code which

I.goto Jump

: Part 1
Code
go to part1
jump: part1 execute its code

J.call subroutine call

Format: call path

demo1.txt
echo hello
demo2.txt
call demo1
echo world
> hello world

If not the same level directory, specify an absolute path

Released four original articles · won praise 0 · Views 73

Guess you like

Origin blog.csdn.net/weixin_42043407/article/details/104308859