最近在工做中遇到一些纯粹重复的工做,最终都经过脚本方式达到了自动化,大大提升效率。好比以前每次发布zip包都须要手动编译lua文件、替换lua引用为二进制文件的引用,选择对应文件打zip包,每次都须要几分钟,还容易出错、遗漏,很不geek,经过脚本后实现了彻底自动化:php
再好比Android项目中用到了插件,因为正式打包和本地编译的gradle脚本不一样,以及Android Studio对模块的编译支持不够,致使每次都须要手动修改文件名(如本地开发时build.gradle
修改成build.gradle.tmp
、build.gradle.local
修改成build.gradle
,正式编包时再修改回去),再copy插件目录出去单独开发调试,最后在把改动合入工程,终端也须要一个按钮来启动插件,十分繁琐且容易形成代码不一样步,经过脚本能够实现工程内一键编译运行:python
因为以前没有用过bat脚本,因此作自动化时速度不是很快,所以花了一天时间整理一下bat脚本的使用要点,只是一个纲领,没有深刻讲解,由于我以为须要的时候去学习细节才是最高效的,但必须了解总体框架才能快速定位到需求对应的命令,所以整理了这篇文章。
此外,Windows7已经支持了powershell,其语法更接近bash,比bat不知道灵活到哪里去了,我为何不用powershell呢?主要是目前powershell速度远没有cmd快,正好个人自动化任务都比较简单,若是用powershell可能启动时间比运行时间还长。
为何不用python、perl等脚本语言?这些脚本须要环境配置,考虑到这些脚本工具可能被其余人使用,因此但愿开箱即用,并且bat足够简单,足够知足需求。shell
相似于编程语言库,这些命令是Windows内置的,能够做为脚本的基本元素,能够在cmd运行,也能够写入cmd运行。首先介绍最重要的两个命令:help
,/?
,利用help
能够查看当前内置的命令:数据库
F:\BatchFileProgramming>help 有关某个命令的详细信息,请键入 HELP 命令名 ASSOC 显示或修改文件扩展名关联。 ATTRIB 显示或更改文件属性。 BREAK 设置或清除扩展式 CTRL+C 检查。 BCDEDIT 设置启动数据库中的属性以控制启动加载。 CACLS 显示或修改文件的访问控制列表(ACL)。 CALL 从另外一个批处理程序调用这一个。 CD 显示当前目录的名称或将其更改。 CHCP 显示或设置活动代码页数。 CHDIR 显示当前目录的名称或将其更改。 CHKDSK 检查磁盘并显示状态报告。 CHKNTFS 显示或修改启动时间磁盘检查。 CLS 清除屏幕。 CMD 打开另外一个 Windows 命令解释程序窗口。 COLOR 设置默认控制台前景和背景颜色。 ......
利用/?
能够详细的了解某个命令:编程
F:\BatchFileProgramming>call /? 从批处理程序调用另外一个批处理程序。 CALL [drive:][path]filename [batch-parameters] batch-parameters 指定批处理程序所需的命令行信息。 若是命令扩展被启用,CALL 会以下改变: CALL 命令如今将卷标看成 CALL 的目标接受。语法是: CALL:label arguments 一个新的批文件上下文由指定的参数所建立,控制在卷标被指定 后传递到语句。您必须经过达到批脚本文件末两次来 "exit" 两次。 第一次读到文件末时,控制会回到 CALL 语句的紧后面。第二次 会退出批脚本。键入 GOTO /?,参看 GOTO :EOF 扩展的描述, 此描述容许您从一个批脚本返回。 另外,批脚本文本参数参照(%0、%一、等等)已以下改变: ......
有了这两个命令,我也就不须要像网上那些文章同样详细解释每一个命令了,查阅文档便可。这里列一些经常使用的,建议优先掌握,较为生僻的在须要时详细学习便可。
经常使用命令:vim
REM,echo,color,title,prompt,cls,date,time,start,exit,call,tree,type,pause,shutdown,at
经常使用环境变量(Environment variables are special variables that contain its values set by the operating system itself, other applications or by manual):bash
%CD%,%PATH%,%RANDOM%
文件和文件夹相关的命令:网络
dir,mkdir,rmdir,chdir,ren,replace,copy,xcopy,del,pushd,popd,move
网络:app
net,ping,telnet,tlntadmn,tracert,ipconfig,hostname,ftp,netstat,nbtstat,arp
若是遇到须要但本身又不知道的,google便可。框架
若是只有这些命令,那么运行bat和在命令行执行没什么区别,最多把命令保存下来了方便之后运行。bat也支持一些编程语言的特性,虽然简陋且不够优雅,但应付简单的自动化任务基本够用。我以为Dennis Ritchie和Brian Kernighan的The C Programming Language,是介绍一门语言的模板,因此这里也按照该书的结构安排。
bat没有类型。set
命令很重要,用于赋值,经过%name%
引用变量,且变量赋值的=
两边不能有空格:
C:\Users\vimerzhao\Desktop>set a=1 C:\Users\vimerzhao\Desktop>echo a a C:\Users\vimerzhao\Desktop>echo %a% 1
bat对运算符的支持和其余语言大同小异:
operators | description |
---|---|
() | grouping |
!、~、- | unary operators |
*、/、%、+、- | arithmetic operators |
<<、>>、<、> | logical shift and re directional operators |
& | bitwise and |
^ | bitwise exclusive or |
| | bitwise or |
=、*=、/=、%=、+=、-=、&=、^=、|=、<<=、>>= | assignment operators |
, | separator |
&& | for using multiple commands |
|| | for executing one from many commands |
bat能够经过for和goto实现循环,经过if实现条件语句。bat经过switch的概念支持不一样类型的遍历,switch和Linux命令的option很像,就是选项,常见的有四个:
switch | description |
---|---|
for /d |
the ‘/d’ switch along with the ‘for’ command is used for looping through several directories |
for /r |
the ‘/r’ switch along with the ‘for’ command is used for looping through directories and sub directories |
for /l |
the ‘/l’ switch along with the ‘for’ command is used for looping through a range of specified numbers |
for /f |
the ‘/f’ switch along with the ‘for’ command is used for looping through a wide variety of files, command and strings |
for循环最多见的应用就是遍历文件夹:
C:\Users\vimerzhao\Desktop>@echo off C:\Users\vimerzhao\Desktop>echo 显示所有文件 C:\Users\vimerzhao\Desktop>for %a in (*) do echo %a 001.PNG 002.PNG ...
在bat脚本中因为%与变量引用冲突,要写成
for %%a in (*) do echo %%a
此外,能够经过内置的语法对文件作处理(如显示完整路径、文件名、后缀名等):
command | description |
---|---|
%~I | expands %I removing any surrounding quotes (") |
%~fI | expands %I to a fully qualified path name |
%~dI | expands %I to a drive letter only |
%~pI | expands %I to a path only |
%~nI | expands %I to a file name only |
%~xI | expands %I to a file extension only |
%~sI | expanded path contains short names only |
%~aI | expands %I to file attributes of file |
%~tI | expands %I to date/time of file |
%~zI | expands %I to size of file |
%~$PATH:I | searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string. |
%~dpI | expands %I to a drive letter and path only |
%~nxI | expands %I to a file name and extension only |
%~fsI | expands %I to a full path name with short names only |
%~dp$PATH:I | searches the directories listed in the PATH environment variable for %I and expands to thedrive letter and path of the first one found |
%~ftzaI | expands %I to a DIR like output line |
if语句除了支持操做符还支持几个自定义的关键字:
operators | meaning |
---|---|
equ | equal |
neq | not equal |
lss | less than |
leq | less than or equal |
gtr | greater than |
geq | greater than or equal |
最后,bat也支持简单的子程序调用,和汇编很像,经过%n能够获取参数,从1开始,如如下代码:
REM filename: test.bat @echo off call :procedure "argument 1" goto:eof :procedure echo repeat part or modular code echo %1 goto:eof
输出为:
F:\BatchFileProgramming>test.bat repeat part or modular code "argument 1"
以上基本都是一些提纲挈领的概述,本身也不算精通每一个细节,相信只要心中有总体框架,再加上一点自动化的意识,长此以往自会驾轻就熟。
做者:达文西z连接:https://www.imooc.com/article/75823来源:慕课网本文原创发布于慕课网 ,转载请注明出处,谢谢合做