有梦想的鱼
写代码我得再认真点,当我最终放下键盘时,我实在不想仍有太多疑惑
1.批处理以及它的作用

1.微软系统自带的原生开发语言,不需要构建任何开发环境就可以执行的脚本。
2.可以匹配文件。新建文件日志这些
3.编写一些可以被windows执行的脚本,例如编译解决方案,或者安装windows服务。卸载windows服务

2.输出Hello World
//关闭命令显示
@echo off 
echo Hello World
pause
3.常用批处理实践

1.安装.net开发的windows服务

@echo off
color 0a
set Filename=Source\Applications\RDA\RAD.Module.Synchronizer\RDA.Module.Synchronizer\bin\Debug\STAr.Aquarius.RDA.Module.Synchronizer.exe
set Servicename= Windows Synchronizer
set Frameworkdc=%SystemRoot%\Microsoft.NET\Framework\v3.5
if exist "%Frameworkdc%" goto netOld 
:DispError 
echo .Net Framework 3.5 is not installed on your machine, the installation will be terminated soon..
echo .Net Framework 3.5 is not installed on your machine, the installation will be terminated soon.  >InstallService.log
goto LastEnd 
:netOld 
cd %Frameworkdc%
echo The corresponding .net Framework 3.5 is installed on your machine, you can install this service. 
echo The corresponding .net Framework 3.5 is installed on your machine, you can install this service >InstallService.log
echo.
echo. >>InstallService.log

echo "%Filename%">1.txt

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe %~dp0\"%Filename%" 
net start SendSms
echo -----------------------------
echo         Service installed successfully
echo -----------------------------
pause

2.调用msbuild编译解决方案

3.windows 服务附加调试

3.批处理运算操作

1.算术运算

  • 1.* / % + - (命令模式:cmd输入 "set /a 1+2")

  • 2.括号改变执行优先级 (文本模式:先要定义变量接收结果,然后用%号包裹输出变量值)

2.重定向运算

    1. 使用> 符号 结果会覆盖原有内容
    1. 使用>> 符号追加到已有内容后面
@echo off
set /a tmp=10+2
echo %tmp% > put.txt
set /a tmp_1 = 10*20
echo %tmp_1% >> put.txt

3.多命令运算

    1. 使用&&符号 &&之前的命令执行错误就不会执行 && 后面的内容
    1. 使用|| 第一个执行错误会执行第二个

4.管道符号 A|B

将第一个参数的结果作为第二个参数的输入

  • 1.假设a文件夹下有b和c文件夹,1.txt和2.txt文件,查找以txt后缀结尾的文件,并输出到tmp.txt文件中
@echo off
dir | find ".txt" > tmp.txt
pause
  • 2.查找当前主机和其他地址建立网络连接的内容,并输出到tmp.txt文件中
@echo off
netstat -an | find "ESTABLISHED" > tmp.txt
pause

4.批处理文件参数传递

%1 代表第一个参数,%2代表第二个参数,参数之间用空格隔开,例如有一个test.cmd批处理,跳转到所在目录然后执行test.cmd 参数1 参数2
就可以调用这个批处理输出 用户输入的2个参数值

@echo off
echo %1
echo %2 
pause
@rem 执行这个批处理文件 test.cmd 参数1 参数2
4.基本命令
  • 1.goto 流程控制转向
@echo off
echo 使用goto命令
goto last
set tmp="Hello World"
echo %tmp%
@rem 设置跳到的标识
:last
echo 跳过命令之后执行的语句
pause
  • 2.start 重新启用一个单独的命令窗口,然后在新窗口执行指定命令
      1. start ["Title"] 设置命令窗口的标题
      1. start /max & /min 设置命令窗口最大最小化
      1. start /wait 等待新窗口执行结束
start /max
@echo off
echo 使用start命令
@rem 开始一个新的程序窗口,标题为new window 等待新窗口执行 在新窗口执行输出New window
start 'new window' /wait echo new window
echo
pause
  • 3.if 逻辑控制命令

      1. if exist & if not exist 条件为真或为假
      1. if string==string () else ()
      1. if number==number () else ()
      1. if /i A==a (echo 123) else echo 234 忽略大小写
start /max
@echo off
echo use if command
@rem 
if exist a.txt (echo find file) else (no find)
pause
posted on 2022-05-25 22:18  有梦想的鱼i  阅读(560)  评论(0编辑  收藏  举报