• Windows命令行查找并kill进程及常用批处理命令汇总


    Windows命令行查找并kill进程及常用命令汇总

    打开命令窗口

    开始—->运行—->cmd,或者是 window+R 组合键,调出命令窗口。

    一、cmd命令行杀死Windows进程方法

    1、根据进程名称批量kill

    1)、执行tasklist|more检索进程

    2)、执行 tasklist|findstr "进程名(模糊匹配)" 确认进程具体名词

    3)、执行 TASKKILL /F /IM 进程名称 /T 杀死进程

    4)、执行tasklist|findstr "QQ“ 确认

    2、根据端口号杀死进程

    1)、执行 netstat -ano|findstr "端口号" 确认进程ID

    2)、根据进程id执行 tasklist|findstr "Pid"确认进程名称

    3)、执行 TASKKILL /F /IM 进程名称 /T 杀死进程

    C:\Users\Administrator>TASKKILL /F /IM redis-se

    rver.exe /T

    二、常用命令

    常用命令汇总

     

    1、盘符

    切换分区

    1. F:\>
    2. F:\>d:
    3. D:\>e:
    4. E:\>f:
    5. F:\>i:
    6. I:\test\dir1>

    2、目录相关

    1)、dir 显示目录中的文件和子目录列表。

    命令示例

    2)、cd显示当前目录名或改变当前目录。

     

    命令示例

    1. I:\test\dir>cd..
    2. I:\test>cd dir1
    3. I:\test\dir1>cd ..
    4. I:\test>cd I:\test\dir
    5. I:\test\dir>cd I:\python
    6. I:\python>

    3)、Chdir 显示当前目录名或改变当前目录。

    命令示例

    1. I:\python>chdir
    2. I:\python
    3. I:\python>chdir I:\test\dir
    4. I:\test\dir>chdir ..
    5. I:\test>

    4)、MD 创建目录。

    命令示例:

    5)、rd删除一个目录。

    命令示例:

    3、文件相关

    1)、Echo显示消息,或者启用或关闭命令回显。或者创建文件

    echo 字符串 > [路径\]  文件名.扩展名

    注释:>>和>都可以将命令的输出内容输入到某文件中,若文件不存在,则同时创建该文件

    >>为追加

    >为覆盖

    命令示例:

    1. I:\test\dir2>echo hello fieldyang>test
    2. I:\test\dir2>echo hello fieldyang>>test
    3. I:\test\dir2>echo hello fieldyang>>test
    4. I:\test\dir2>type test
    5. hello fieldyang
    6. hello fieldyang
    7. hello fieldyang
    8. I:\test\dir2>echo hello world>>test
    9. I:\test\dir2>echo hello world>test
    10. I:\test\dir2>type test
    11. hello world
    12. I:\test\dir2>

    2)、Replace 替换文件。

    示例示例:

    3)、type显示文本文件的内容。

    TYPE [drive:][path]filename

    命令示例:

    1. I:\test\dir2>type test
    2. hello world
    3. hello fieldyang
    4. hello fieldyang
    5. hello fieldyang
    6. hello fieldyang

    4)、more逐屏显示输出。

    命令示例:

    1. I:\test\dir2>more I:\python\mysql_createdb.py
    2. #usr/bin/python
    3. #-*- coding:utf-8 -*-
    4. #author:Fieldyang
    5. import io
    6. import os
    7. import MySQLdb
    8. import time
    9. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    10. I:\test\dir2>type I:\python\mysql_createdb.py|more
    11. #usr/bin/python
    12. #-*- coding:utf-8 -*-
    13. #author:Fieldyang
    14. import io
    15. import os
    16. import MySQLdb
    17. import time
    18. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"

    5)、Del删除一个或多个文件。

    命令示例:

    6)、Attrib 显示或更改文件属性。

    命令示例:

    1. I:\test\dir2>type test0
    2. helo world
    3. I:\test\dir2>attrib test0
    4. A I:\test\dir2\test0
    5. I:\test\dir2>attrib +r test0
    6. I:\test\dir2>echo helo field >>test0
    7. 拒绝访问。
    8. I:\test\dir2>attrib -r test0
    9. I:\test\dir2>echo helo field >>test0
    10. I:\test\dir2>type test0
    11. helo world
    12. helo field
    13. I:\test\dir2>

    7)、Copy 将一份或多份文件复制到另一个位置。

    命令示例:

    8)、move 移动文件并重命名文件和目录

    命令示例:

    9)、ren 重命名文件

    4、字符检索相关

    1)、find 在文件中搜索字符串。

    命令示例:

    1. I:\test\dir2>find "NLS_LANG" I:\python\mysql_createdb.py
    2. ---------- I:\PYTHON\MYSQL_CREATEDB.PY
    3. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    4. I:\test\dir2>find "NLS_LANG" I:\python\*.py
    5. ---------- I:\PYTHON\1.PY
    6. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    7. ---------- I:\PYTHON\2.PY
    8. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    9. ---------- I:\PYTHON\CMD.PY
    10. ---------- I:\PYTHON\MONITOR.PY
    11. ---------- I:\PYTHON\MYSQL-REDIS.PY
    12. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    13. ---------- I:\PYTHON\MYSQL_CREATEDB.PY
    14. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    15. ---------- I:\PYTHON\REDIS-HASH.PY
    16. ---------- I:\PYTHON\REDIS-LIST.PY
    17. ---------- I:\PYTHON\REDIS-SET.PY
    18. ---------- I:\PYTHON\REDIS-STRING.PY
    19. ---------- I:\PYTHON\REDISTY.PY
    20. ---------- I:\PYTHON\TEST.PY
    21. I:\test\dir2>

    2)、findstr 在文件中寻找字符串。

    命令示例

    1. I:\test\dir2>findstr NLS_LANG I:\python\*.py
    2. I:\python\1.py:os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    3. I:\python\2.py:os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    4. I:\python\mysql-redis.py:os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    5. I:\python\mysql_createdb.py:os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    6. I:\test\dir2>type I:\python\mysql_createdb.py |findstr NLS_LANG
    7. os.environ['NLS_LANG'] = "AMERICAN_AMERICA.ZHS16GBK"
    8. I:\test\dir2>

    5、时间设置

    1)、Date显示或设置日期。

    DATE [/T | date]

    显示当前日期设置和输入新日期的提示,请键入

    不带参数的 DATE。要保留现有日期,请按 Enter。

    如果命令扩展被启用,DATE 命令会支持 /T 开关;

    该开关指示命令只输出当前日期,但不提示输入新日期。

    2)、time显示或设置系统时间。

    TIME [/T | time]

    显示当前时间设置和输入新时间的提示,请键入

    不带参数的 TIME。要保留现有时间,请按 Enter。

    如果命令扩展被启用,TIME 命令会支持 /T 命令行开关;该命令行开关告诉

    命令只输出当前时间,但不提示输入新时间。

    6、进程相关

    1)、Tasklist显示在本地或远程机器上当前运行的进程列表。

     

    命令示例:

    1. I:\test\dir2>tasklist|more
    2. 映像名称 PID 会话名 会话# 内存使用
    3. ========================= ======== ================ =========== ============
    4. System Idle Process 0 Services 0 8 K
    5. System 4 Services 0 24 K
    6. Registry 108 Services 0 34,768 K
    7. smss.exe 424 Services 0 232 K
    8. csrss.exe 648 Services 0 2,236 K
    9. ^C^C
    10. I:\test\dir2>tasklist|findstr System
    11. System Idle Process 0 Services 0 8 K
    12. System 4 Services 0 24 K
    13. SystemSettings.exe 8448 Console 18 1,148 K
    14. I:\test\dir2>

    2)、Taskkill 杀死进程

    命令示例

    3)、Netstat显示协议统计信息和当前 TCP/IP 网络连接。

    输入命令:

    netstat -ano

    该命令列出所有端口的使用情况。

    在列表中我们观察被占用的端口,比如是 1224,首先找到它。

    示例:

    7、其他

    1)、Set 显示、设置或删除 cmd.exe 环境变量。

    命令示例:

    1. I:\test\dir2>set a=hello
    2. I:\test\dir2>set b=world
    3. I:\test\dir2>set /A c=50
    4. 50
    5. I:\test\dir2>set /A d=150
    6. 150
    7. I:\test\dir2>set f=%a%and%b% +(%c%+%d%)
    8. I:\test\dir2>echo %f%
    9. helloandworld +(50+150)
    10. I:\test\dir2>set e=%c%+%d
    11. I:\test\dir2>echo %e%
    12. 50+150
    13. I:\test\dir2>set /A g=%c%+%d%
    14. 200
    15. I:\test\dir2>echo %G%
    16. 200
    17. I:\test\dir2>

    2)、Path 为可执行文件显示或设置一个搜索路径。

    3)、Choice从选择列表选择一个项目并返回所选项目的索引。

    命令示例

    1. I:\test\dir1>CHOICE /C YNC /M "确认请按 Y,否请按 N,或者取消请按 C。"
    2. 确认请按 Y,否请按 N,或者取消请按 C。 [Y,N,C]?Y
    3. I:\test\dir1> CHOICE /C ab /M "选项 1 请选择 a,选项 2 请选择 b。"
    4. 选项 1 请选择 a,选项 2 请选择 b。 [A,B]?A
    5. I:\test\dir1>

    4)、Goto将 cmd.exe 定向到批处理程序中带标签的行。

    示例参考后面脚本

    5)、Call 从批处理程序调用另一个批处理程序。

    命令示例

    1. I:\test\dir1>type test3.bat
    2. echo You call another bat script test3.bat
    3. echo Thanks for your call!
    4. echo Goodbye!
    5. I:\test\dir1>call test3.bat
    6. I:\test\dir1>echo You call another bat script test3.bat
    7. You call another bat script test3.bat
    8. I:\test\dir1>echo Thanks for your call!
    9. Thanks for your call!
    10. I:\test\dir1>echo Goodbye!
    11. Goodbye!
    12. I:\test\dir1>

    8、符号相关

    1)、@ 关闭命令回显

    命令示例:

    1. I:\test\dir1>for %a in (1,2,3,4,5) do echo %a
    2. I:\test\dir1>echo 1
    3. I:\test\dir1>echo 2
    4. 2
    5. I:\test\dir1>echo 3
    6. 3
    7. I:\test\dir1>echo 4
    8. 4
    9. I:\test\dir1>echo 5
    10. 5
    11. I:\test\dir1>for %a in (1,2,3,4,5) do @echo %a
    12. 1
    13. 2
    14. 3
    15. 4
    16. 5
    17. I:\test\dir1>@echo off
    18. set /A a=5
    19. 5
    20. set /A b=10
    21. 10
    22. set c=%a%+%b%
    23. set /A d =%a%+%b%
    24. 15
    25. echo %c%
    26. 5+10
    27. echo %d
    28. %d
    29. echo on
    30. I:\test\dir1>

    2)、| 管道符:同Linux,前一个命令的输出作为后一个命令的输入

    Command1|command2

    示例

    3)、&  顺序执行多条命令,而不管命令是否执行成功

    Command1&command2

    示例 :先写“hello world”到test1,然后创建test0文件

    可以看到 写入失败,但是创建文件成功

    4)、&& 顺序执行多条命令,当碰到执行出错的命令后将不再执行后面的命令

    Command1&&command2

    示例 :先写“hello world”到test1,执行成功则创建test0文件

    可以看到 写入失败,创建文件失败

    5)、|| 顺序执行多条命令,当碰到执行正确的命令后将不再执行后面的命令

    Command1||command2

    示例 :先写“hello world”到test1,执行不成功则创建test0文件

    可以看到写入失败,但是创建文件test0成功

    示例 :修改test1文件可以写入,先写“hello world”到test1,执行成功则不创建test0文件

    可以看到写入成功,不再创建test0文件

    三、简单脚本

    1、for循环

     

    示例1:循环创建 test0-test4

    for %i in (0,1,2,3,4) do echo >test%i

    示例2:循环写入到test0

    2、if else 判断

     

    示例1:存在test0输出hello test0 否则输出 not exist test0

    示例2:存在test5 打印hello test5 否则创建test5文件

    示例3:如果变量i==C则创建dir_C目录,否则创建file_%i文件

    示例4:如果定义变量则输出变量值,否则输出No define variable a1

    3、if -else if-else 多层嵌套

    示例1:如果定义变量则继续判断值是否匹配,没有定义则输出No define variable a1

    4、choice根据用户输入执行不同操作

    以下脚本演示Windows位置参数,choice选择、call调用、变量匹配

     test2.bat

    1. @echo off
    2. :start
    3. choice /c YNCPQ /m "确认请选Y,否请按N,取消按C,展示参数请安P,退出请按Q" /T 10 /D C
    4. if errorlevel 5 goto quit
    5. if errorlevel 4 goto para
    6. if errorlevel 3 goto cancel
    7. if errorlevel 2 goto no
    8. if errorlevel 1 goto yes
    9. :yes
    10. echo You choice yes!
    11. call I:\test\dir1\test3.bat
    12. goto end
    13. :no
    14. echo You choice no!
    15. goto end
    16. :cancel
    17. echo You choice cancel!
    18. goto end
    19. :para
    20. if NOT "a%3"=="a" ( if "%3" == "cc" (echo hello %3) else (echo The bat script para is %1 %2 %3)) else if NOT "a%2"=="a" (echo The bat script para is %1 %2) else if not "a%1"=="a" (echo The bat script para is %1 ) else (echo No defined parameter)
    21. goto end
    22. :end
    23. goto start
    24. :quit
    25. echo Goodbye!
    26. pause
    1. I:\test\dir1>type test3.bat
    2. echo You call another bat script test3.bat
    3. echo Thanks for your call!
    4. echo Goodbye!

  • 相关阅读:
    vscode 配置自定义code snippets 来快速生成你的常用代码
    Maven 如何配置推送的仓库
    R语言绘制精美图形 | 火山图 | 学习笔记
    无需专业技能,轻松创建个人博客:Cpolar+Emlog在Ubuntu上的搭建指南
    Java -- 每日一问:后台服务出现明显“变慢”,谈谈你的诊断思路?
    【数学建模】复杂水平井三维轨道设计附论文和matlab代码
    LTD243次升级 | 网页可单设分享图 • 小程序页可下载附件 • 官网商城支持分类+组合筛选、商品排序
    浅谈微服务的发展以及可观测性
    【matplotlib 实战】--折线图
    算法 括号生成-(递归回溯+同向双指针)
  • 原文地址:https://blog.csdn.net/Field_Yang/article/details/131145877