bat实例

1.创建以当前时间命名的文件夹

@echo off
echo %date%
set current_date=%date:~0,4%%date:~5,2%%date:~8,2%
set current_time=%time:~0,2%%time:~3,2%%time:~6,2%
set filename=%current_date%%current_time%
echo %filename%
mkdir %filename%

 2.全盘符搜索文件

@echo off
set "FileName=AutoCreateDir.bat"
for %%a in (E F G) do (
  ::如果存在该盘符
  if exist %%a:\ (
    echo Finding in disk %%a
    for /f "delims=" %%b in ('dir /a-d /s /b "%%a:\*%FileName%" 2^>nul') do (
      if /i "%%~nxb" equ "%FileName%" (
        echo Find %FileName% in %cd%
        call AutoCreateDir.bat
        ::退出循环
        GOTO :END
      )
    )
  )
)
:END
echo Find file finished.

 3.变量自增

::在循环中变量自增后,因为批处理运行命令的机制,直到循环结束该变量值才能正确打印显示出来。
::使用setlocal enabledelayedexpansion后可以在循环中正确打印显示变量的值,需要用!!括起来
@echo off
setlocal enabledelayedexpansion
set /a a=2
for /l %%c in (1 1 10) do (
    set /a a+=1
    echo !a!
)
echo %a%
pause

 4.将目录下所有文件更名为指定名称(不包含子目录)

@echo off  
setlocal enabledelayedexpansion  
cd %cd%\test
echo %cd%
set a=0
::在当前目录循环遍历所有文件,不遍历子目录
for %%i in (*.*) do ( 
    :: /a表示要进行数学运算
    set /a a+=1  
    if /i not "%%~xi"==".bat" ( 
        ren "%%i" "!a!.txt" 
        echo Rename %%i to !a!.txt.
    )
)  
pause 

 5.将文件按照类型分类存储在不同文件夹下

@echo off 
set usage=E:\version\test
::待拆分的源文件夹 srcdir
set src=srcdir
rem 拆分的目标文件夹 ClassifyDir
set dest=ClassifyDir

::创建分类文件夹
if not exist %dest%\bats (
    mkdir %dest%\bats
)
if not exist %dest%\txts (
    mkdir %dest%\txts
)
 
for /f "delims=" %%i in ('dir /b /s /a-d %src%') do ( 
    if /i "%%~xi"==".bat" (
        copy "%%i" "%dest%\bats\%%~nxi"  
    ) else if /i "%%~xi"==".txt" (
        copy "%%i" "%dest%\txts\%%~nxi"  
    )
)

6.倒计时5s计时器

@ECHO OFF
:: 5s倒计时计数器
:: 1.以a=(5,4,3,2,1)的顺序进行循环
:: 2.ping的参数,-n 2表示ping两次
:: 3.>NUL表示不回显在cmd
FOR /l %%a in (5,-1,1) do (
    echo closing in %%as&ping -n 2 127.0.0.1>NUL
)

7.寻找文件名包含某个字符串的文件

@echo off
:: findstr /C:字符串,表示搜索某个字符串
for /f "delims=" %%i in ('dir /b^|findstr /C:test') do ( @echo %%i ) pause

 8.自动解压安装版本

@echo off
set filetounzip="\\U31V12.17.20B05-20170713-CGSL-JDK8.zip"
set versionpath="U31V12.17.20B05-20170713-CGSL-JDK8" set currentpath=%cd% set xmlpath="c:\version\bats\auto_installtion_client.xml" set setupbatpath="c:\version\bats\silent-setup.bat" set confpath="\\conf" ::如果存在该文件夹则删除 echo %currentpath%\%versionpath% if exist %currentpath%\%versionpath% ( rd /s /Q %currentpath%\%versionpath% ) else ( md %versionpath% ) ::解压文件 cd C:\AutoInstall\tool\HaoZip HaoZipC x %currentpath%\%filetounzip% -o%currentpath%\%versionpath% ::进入下载的版本文件路径 set FILES= for /f "delims=" %%i in ('dir /b %currentpath%\%versionpath%') do (set FILES=%FILES%%%i) echo %FILES% echo %currentpath%\%versionpath%\%FILES% cd %currentpath%\%versionpath%\%FILES% ::替换xml和bat文件 copy %xmlpath% %currentpath%\%versionpath%\%FILES%%confpath% echo copy xml finished copy %setupbatpath% %currentpath%\%versionpath%\%FILES% echo copy bat finished ::执行安装脚本 call silent-setup.bat

 

函数调用

sh脚本
function hello() {
    echo "Hello!"; } function hello_param() { echo "Hello $1 !"; } echo "无参函数调用"; hello; echo "函数传参调用"; hello_param ben;

转载于:https://www.cnblogs.com/laislabonita/p/7221459.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值