效能工具之批处理文件.bat/.cmd在工作中的应用

什么是批处理文件

  • 批处理文件是指文件后缀名是.bat.cmd的文件
  • 批处理文件中可以去编写很多命令,用于给电脑自动执行
  • 触发条件就是双击这个.bat.cmd文件 即会自动执行文件中的命令,从而实现我们想要的效果
  • 批量处理一些命令——即为:批处理文件
  • 就前端而言,我们常常会在vscode的终端去执行一些命令
  • 或者git的bash命令,基本一个意思

若是对于批处理命令不太熟悉的,可以看看这篇命令的总结:https://zhuanlan.zhihu.com/p/54572985

.bat和.cmd批处理文件有什么区别

  • 先有的.bat,后有的.cmd(历史发展问题)
  • 二者基本上是一样的
  • 就相当于word文档中的.doc.docx的区别
  • 二者都可以在windows系统(古老的dos也行)直接运行的

长相如下图:

[图片上传失败...(image-449d79-1702137337003)]

编写批处理文件的话,可以直接使用电脑自带的文本文档,写完以后,直接改文件后缀名即可。

那么,批处理文件有哪些应用呢?举几个例子,请继续往下看...

应用场景一:一键启动多个软件

  • 我们每天去上班,第一件事就要打开各种工作软件,少则五六个,多则十几个,比如:
  • 要分别双击去打开钉钉、飞书、微信、谷歌火狐Edge浏览器、等...
  • 双击操作要执行多次
  • 针对这种情况,我们就可以编写一个简单的.bat文件
  • 在开机以后,只要双击这个.bat,就会自动打开工作中需要使用的软件
  • 做到双击一次,可以打开多个软件,减少双击次数

在这里补充几个前置知识。

什么是电脑桌面快捷方式

通俗理解

  • 快捷方式是一个软件程序启动入口(后缀名为.lnk的文件),相当于一个地址链接
  • 这个链接指向电脑软件安装路径目录的.exe文件
  • .exe文件就是executable file,也就是可执行文件,双击就可以打开这个软件了,以谷歌浏览器为例如下截图:

[图片上传失败...(image-6b6ec7-1702137337003)]

  • 如上图双击chrome.exe文件,就会自动打开谷歌浏览器
  • 实际上双击桌面的chrome浏览器快捷方式,就是双击这个chrome.exe文件
  • 具体我们可以桌面选中一个快捷方式(我们也可以发送此exe文件到桌面作为快捷方式),并右键,查看属性即可了然,如下图:

[图片上传失败...(image-ab5d98-1702137337003)]

编写启动多个软件的批处理命令

上述是双击桌面快捷方式启动软件,我们可以使用批处理命令去启动软件,比如我们要在批处理文件中启动谷歌浏览器,可以使用start命令:

<pre class="js hljs language-javascript" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; display: block; margin-top: 0px !important; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">start "" "C:\Program Files\Google\Chrome\Application\chrome.exe"</pre>

即为start命令接收一些参数,其中要告知其需要 启动的程序的路径,这样的话,就可以启动想要启动的软件了。

比如,我们在开机后,要启动 钉钉、微信、谷歌浏览器、Edge浏览器、火狐浏览器,那么我们就可以,使用txt编写批处理文件,如下命令代码:

<pre class="hljs language-crmsh" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; display: block; margin-top: 0px !important; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">start "" "D:\Program Files (x86)\DingDing\DingtalkLauncher.exe"
start "" "D:\Program Files\Tencent\WeChat\WeChat.exe"
start "" "C:\Program Files\Google\Chrome\Application\chrome.exe"
start "" "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"</pre>

保存以后,再手动修改文件后缀名为 .bat 或者 .cmd 然后双击就行了

应用场景二:一键启动多个vscode代码项目

  • 工作中,前端常常要使用vscode启动多个代码项目
  • 正常我们都是要找到对应的代码文件夹,然后使用vscode打开
  • 换句话说,还是要多点击几次,那么我们依然能够使用批处理文件,直接打开,让我们少点击几次

前置知识 命令提示符黑窗口中的code命令(vscode自带的快捷命令)

  • code命令是安装vscode后其提供的
  • 意思就是在命令行中执行此命令,即可调用vscode打开文件夹或者文件
  • 常用命令:

命令 code .

图示如下:

[图片上传失败...(image-53298c-1702137337002)]

[图片上传失败...(image-dd8579-1702137337002)]

另有命令:code xxx ,如下:

<pre class="js hljs language-javascript" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; display: block; margin-top: 0px !important; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">// 执行命令 code xxx ,xxx可以是文件,或者是文件夹

code index.html // 使用vscode打开当前目录的index.html文件
code dist // 使用vscode打开当前目录的dist文件夹</pre>

code命令搭配批处理文件命令打开项目代码

  • 假设我有两个前端代码项目需要启动
  • 这两个代码文件夹分别在这两个地方:
  • D:\study\v3_titeD:\study\RuoYi-Vue
  • 那么编写一个bat批处理文件,双击即可直接打开这两个项目了,如下代码

<pre class="js hljs language-javascript" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; display: block; margin-top: 0px !important; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@echo off

REM 打开第一个代码项目
start /B code D:\study\v3_tite

REM 打开第二个代码项目
start /B code D:\study\RuoYi-Vue

echo let's coding...</pre>

应用场景三:编写一个nginx自动执行脚本用于reload

  • 假设我们的服务器是windows系统的,每次打包dist文件夹后,丢到对应服务器nginx配置的目录
  • 发布完毕以后,我们需要重启一下nginx
  • 正常的话,我们执行nginx -s reload命令
  • 同样的,我们可以编写一个批处理文件,通过输入1,2,3,4交互的方式来控制nginx
  • 这样也能够提升效率,效果图如下:

[图片上传失败...(image-e03dfc-1702137337002)]

代码:

<pre class="js hljs language-javascript" style="box-sizing: border-box; font-family: var(--bs-font-monospace); font-size: 0.875em; display: block; margin-top: 0px !important; margin-bottom: 1.25rem; overflow: auto; color: rgb(36, 41, 46); background: rgb(233, 236, 239); padding: 1rem; max-height: 35rem; line-height: 1.5; position: relative; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">cls
@ECHO OFF
SET NGINX_PATH=C:
SET NGINX_DIR=C:\nginx-1.18.0\

TITLE Nginx 批处理
GOTO OPTION
:OPTION
CLS
ECHO.
ECHO. * * * * Nginx 批处理管理程序 * * * * *
ECHO. * *
ECHO. * 1 启动Nginx *
ECHO. * *
ECHO. * 2 关闭Nginx *
ECHO. * *
ECHO. * 3 重启Nginx *
ECHO. * *
ECHO. * 4 退出命令行黑窗口*
ECHO. * *
ECHO. * * * * * * * * * * * * * * * * * * *
ECHO.
ECHO.请输入选择项目的序号:
set /p ID=
IF "%id%"=="1" GOTO cmd1
IF "%id%"=="2" GOTO cmd2
IF "%id%"=="3" GOTO cmd3
IF "%id%"=="4" EXIT
PAUSE
:cmd1
ECHO.
ECHO.启动Nginx......
IF NOT EXIST %NGINX_DIR%nginx.exe ECHO %NGINX_DIR%nginx.exe不存在
%NGINX_PATH%
cd %NGINX_DIR%
IF EXIST %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe
ECHO.OK
PAUSE
GOTO OPTION
:cmd2
ECHO.
ECHO.关闭Nginx......
taskkill /F /IM nginx.exe > nul
ECHO.OK
PAUSE
GOTO OPTION
:cmd3
ECHO.
ECHO.关闭Nginx......
taskkill /F /IM nginx.exe > nul
ECHO.OK
GOTO cmd1
GOTO OPTION</pre>

注意,上述有两个变量,即为:SET NGINX_PATH=C:
SET NGINX_DIR=C:\nginx-1.18.0\ 大家可以将其替换成自己服务器上nginx安装目录即可使用

总结

  • 批处理文件很强大
  • 合理使用,能做很多有意思的事情...

若是对于批处理命令不太熟悉的,可以看看这篇命令的总结:https://zhuanlan.zhihu.com/p/54572985

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容