zoukankan      html  css  js  c++  java
  • bat脚本简单命令

    1.if 判断

    (1.1)判断字符串是否为空:

        if "%var1%" == " " ( echo null)
          else(echo not null )

    (1.2)判断字符串是否不等于test  

        if not "%var1%" == "test" ( echo not equal)
        else(echo equal )

    (1.3)判断文件是否存在

      if exist test1.txt (echo exist file)

    (1.4)判断数值是否相等

      if 1 equ 2 (echo equal) else (echo 1 not equal 2)

    (1.5)if--else if---else

    if "%type_tmp%" == "Release_Static" (
      echo "++++++++++++++++++++++it is Release Static+++++++++++++++++++"
      set type="Release Static"
    )else if "%type_tmp%" == "Debug_Static" (
      echo "++++++++++++++++++++++it is Debug Static+++++++++++++++++++++++"
      set type="Debug Static"
    )else (
      echo "++++++++++++++++++++++++change type+++++++++++++++++++++++++++++"
      set type=%type_tmp%
    )

    (1.6) if 中的即不等于aaa也不等于bbb:以下

    if not "%proj%" == "aaa" (
      if not "%proj%" == "bbb" (
      echo "%proj% is not aaa nor bbb and so exit"
      exit 1
      )
    )

    有的时候,我们发现我们的if else 已经按照上面操作了,还是报错,这可能就跟格式有关系了,注意括号前至少要一个空格。

    2.findstr 找到包含指定字符的文件

    例子: dir /B |findstr "test"

    3.sleep 

    bat中没有sleep这个命令,如果要等待,你不能写sleep 10 这样,那要怎么实现呢,很简单,用ping 就可以了

    ping 127.0.0.1 可以sleep 几秒,可以通过-n 来控制ping 的连接数(具体:ping /?),比如ping 127.0.0.1 -n 10 表示ping 10个连接数:

    至于要计算出到底用了时间,可以使用time命令来计算。

    4.time 

    set datevar=%date:~0,4%%date:~5,2%%date:~8,2%
    set mytime=%time:~0,2%%time:~3,2%
    set result_dir=%datevar%%mytime%

    echo result_dir=%result_dir%

    如下:

    看到结果时201807161835表示2017年7月16号18:35

    这里涉及到bat的字段串的截获,如上,输入date得到的是2017/07/16,~0,4表示的是位置0后的4个字符,~5,2表示位置5后面的2个字符,其他类似。

    5.判断远程路径是否存在

    参考:http://www.bathome.net/thread-31314-1-1.html

    if exist \%RemoteServerIp%\%RemoteServerDir% (goto  auto_bak)  else echo "not exist!!"

    或者:

  • 相关阅读:
    2019.03.20 mvt,Django分页
    2019.03.18 连接my sql
    第三方包
    飞机大战完结篇
    Python复习基础篇
    final、权限、内部类
    接口、多态
    继承、super、this、抽象类
    String类、static、Arrays类、Math类
    Scanner类、Random类、ArrayList类
  • 原文地址:https://www.cnblogs.com/zndxall/p/9168031.html
Copyright © 2011-2022 走看看