返回「计算机、信息技术与工程」

【batshell】bat脚本

更多
Markdown 结构化数据
本文目录 31 个章节

【batshell】bat脚本

创建时间:2020/9/23 22:06

  • 【bat/shell】bat脚本
    • 特性
    • Variable
      • Assignment
      • Reading
      • Listing
      • Variable Scope (Global vs Local)
      • 一些特殊的变量
      • Tips
    • 常用命令
      • echo
      • rem ::
      • pause
      • start & call
        • start
        • call
      • set
        • 字符串操作相关
      • for
        • 无开关
        • 开关/L
      • 判断语句
      • goto
      • 目录相关(创建,删除,判断存在)
        • 删除
      • copy && xcopy
        • copy - 复制文件
        • Xcopy 支持文件/文件夹新建
      • move
      • %~dp0
      • 日期和时间
    • ref

特性

DOS is case insensitive.

Variable

Assignment

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

Reading

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

Listing

SET command with no arguments - list all variables for the current command prompt session

SET will list all regular (static) variables for the current session. This listing excludes the dynamic environmental variables like %DATE% or %CD%.

Variable Scope (Global vs Local)

By default, variables are global to your entire command prompt session.

SETLOCAL - make variables local to the scope of your script. After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL , calling EXIT , or when execution reaches the end of file ( EOF ) in your script.

一些特殊的变量

%~dpn - the full path to the parent folder of the ith command line argument. %~dp0 - the path of the folder for the script file

%CD% - 扩展到当前目录字符串。 %DATE% - 用跟 DATE 命令同样的格式扩展到当前日期。 %TIME% - 用跟 TIME 命令同样的格式扩展到当前时间。 %RANDOM% - 扩展到 0 和 32767 之间的任意十进制数字。 %ERRORLEVEL% - 扩展到当前 ERRORLEVEL 数值。 %CMDEXTVERSION% - 扩展到当前命令处理器扩展版本号。 %CMDCMDLINE% - 扩展到调用命令处理器的原始命令行。 %HIGHESTNUMANODENUMBER% - 扩展到此计算机上的最高 NUMA 节点号。

Tips

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

SET /? - the help text for SET

常用命令

echo

打开回显或关闭请求回显功能,或显示消息。如果没有任何参数,echo命令将显示当前回显设置。

语法

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

示例:

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

rem ::

注释 当关闭回显时,rem和::后的内容都不会显示; 当打开回显时,rem后的内容会显示出来,然而::后的内容仍然不会显示。

pause

暂停命令。运行 Pause 命令时,将显示下面的消息: Press any key to continue…(或:请按任意键继续…)

start & call

start

调用外部程序,所有的DOS命令和命令行程序都可以由start命令来调用。

MIN 开始时窗口最小化 SEPARATE 在分开的空间内开始 16 位 Windows 程序 HIGH 在 HIGH 优先级类别开始应用程序 REALTIME 在 REALTIME 优先级类别开始应用程序 WAIT 启动应用程序并等候它结束 parameters 这些为传送到命令/程序的参数

call

Call xxx.bat parameter1 p2 p3 参数字符串若包含空格,必须使用引号,否则被拆成多个参数

set

  1. 显示变量: set 显示批处理当前已定义的所有变量及其值 set s 显示所有以s开头的变量及值

  2. 设置和调用变量,set aa=abcd,就是把aa定义为abcd。

  3. 删除变量:set aa=

字符串操作相关

字符串截取 set 目标字符串=%源字符串:~起始值,截取长度%

for

无开关

for %%i in (a,"b c",d) do echo %%i %%variable是批处理程序里面的书写格式,在DOS中书写为%variable

开关/L

实现对循环次数的直接控制

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

创建5个文件夹:

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

判断语句

实用场景:调用bat时若没有传参则退出

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

/b 表示只退出批处理脚本,不退出命令行

Demo2:

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

goto

goto 标签 注释内容(可以用作说明goto的条件和执行内容) :标签 注释内容(可以用作标签下方段的执行内容)

目录相关(创建,删除,判断存在)

string a = "test";
string tmp = "est";
string b = "t" + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

判断是否存在: if not exist %Directory% 删除目录(文件夹): rd /s /q D:\NETDATA 删除文件: del “%Directory%%file1%” 创建目录: md %Directory2%\picTmp

删除

删除指定目录所有指定文件 del /f /s /q /a 你的文件路径*.删除的后缀

/F 强制删除只读文件。 /S 从所有子目录删除指定文件。 /Q 安静模式。删除全局通配符时,不要求确认。 /A 根据属性选择要删除的文件。

copy && xcopy

copy - 复制文件

file1和file2表示文件夹,不会新建文件 copy a.txt .\file1\ 若file1不存在,则显示:系统找不到指定路径 copy file1 .\file2\ 会把file1中的文件复制到file2中

Xcopy 支持文件/文件夹新建

copy a.txt .\file1\ 若file1不存在,则自动创建。 如果file后面去掉斜杠『\』,系统要求做出判断是建立文件还是文件夹

常用选项 /E 复制目录和子目录,包括空的。 /I 如果目标不存在,又在复制一个以上的文件,则假定目标一定是一个目录。 /Y 禁止提示以确认改写一个现存目标文件。 /F 复制时显示完整的源和目标文件名。

xcopy Dir1 Dir2 /E /Y /I /F

move

移动文件 move “file.txt” “%targetDir%”

%~dp0

%0代表bat文件本身的完整路径 ~dp 是变量扩展选项(d - driver,代表盘符,p - path代表路径)

其他扩展选项:

%~f0 - 完整的路径+文件名 %~n0 - 文件名(无扩展名) %~x0 - 文件扩展名 %~s0 - 扩充的路径只含有短名(“s”为Short,短的) %~a0 - 将 %0 扩充到文件的文件属性(“a”为attribute,即属性) %~t0 - 将 %0 扩充到文件的日期/时间(“t”time) %~z0 - 将 %0 扩充到文件的大小(Size 大小)

日期和时间

日期 格式: %date% 结果: 2012-07-31

时间呢 格式: %time% 结果: 10:21:21.68

格式: %date:~x,y%以及%time:~x,y% 说明: x是开始位置,y是取得字符数

获取完整的日期和时间 格式: %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2% 结果: 20120731111039

如果时间早于10点,那么只显示日期不显示时间,如:20120731 格式: %date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2% 结果: 20120731052539

ref

https://www.jianshu.com/p/5a1a882ead95