07 August 2013

Introduction

In DOS, OS/2, and also Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter.

Common batch scripts

current path

@echo off

set BASE_DIR=%~dp0
echo %BASE_DIR%

set choice=a
echo %choice%

echo 当前盘符:%~d0
echo 当前盘符和路径:%~dp0
echo 当前批处理全路径:%~f0
echo 当前盘符和路径的短文件名格式:%~sdp0
echo 当前CMD默认目录:%cd%

choice

choice /c:abc /M "apple,banana,cake"
echo %errorlevel%
::应先判断数值最高的错误码
if errorlevel 3 goto a
if errorlevel 2 goto b
if errorlevel 1 goto c

:a
echo apple
goto end

:b
echo banana
goto end

:c
echo cake
goto end

:end
echo good bye
pause
goto :eof

call

::call [[Drive:][Path] FILE_NAME [Bathparameters]] [:labe] [Arguments]]
call test1 git_pull
call :test abc
pause
exit

:test
rem is parameter1 null?
rem if not "%1" equ ""
if "%1"=="" (
    echo parameter is required.
    goto :eof
    ::call end
)

echo call label %1
goto :eof
::call end

list current directories

setlocal enabledelayedexpansion
rem for /f "delims=" %%i in ('dir /b /a-d /s "*"')  do (
FOR /D %%i IN (*)  do (
    echo %%i
)
endlocal
pause
exit

create database

rem echo drop database if exists tt; create database tt character set utf8;|mysql -ucbay -pcbay
rem mysql -ucbay -pcbay -e "drop database if exists tt; create database tt character set utf8;"
mysql -ucbay -pcbay --execute="drop database if exists tt; create database tt character set utf8;"
if errorlevel 1 echo failed
if errorlevel 0 echo successful
pause
exit

date time

echo %date%-%time%
rem 提取年月日信息
echo %date:~0,10%
rem 提取星期几信息
echo %date:~-3%
rem 提取时间中的时和分
echo %time:~0,5%
rem 提取时和分和秒信息
echo %time:~0,-3%

echo %date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,2%

for /f "tokens=1-3 delims=- " %%1 in ("%date%") do set ttt=%%1%%2%%3 
for /f "tokens=1-3 delims=.: " %%1 in ("%time%") do set ttt=%ttt%-%%1%%2%%3 
echo goodtime=%ttt%

set CURDATE=%date:~0,4%%date:~5,2%%date:~8,2%
set CURTIME=%time%
set HH=%CURTIME:~0,2%
set MM=%CURTIME:~3,2%
set SS=%CURTIME:~6,2%
REM 小时数如果小于10,则在前面补0
if "%HH%"==" 0" set HH=00
if "%HH%"==" 1" set HH=01
if "%HH%"==" 2" set HH=02
if "%HH%"==" 3" set HH=03
if "%HH%"==" 4" set HH=04
if "%HH%"==" 5" set HH=05
if "%HH%"==" 6" set HH=06
if "%HH%"==" 7" set HH=07
if "%HH%"==" 8" set HH=08
if "%HH%"==" 9" set HH=09

set CURTIME=%HH%%MM%%SS%
set FILE_NAME=%CURDATE%%CURTIME%.dat
echo %FILE_NAME%
pause
exit

read file

for /r %%a in (*.txt) do (
rem set b=%%a
set b=%b%hello
echo !b!
)

append string

setlocal enabledelayedexpansion
set i=0
set str=

:string_start
if %i% LSS 100 (
    set /a i=i+1
    if !i! EQU 1 (
    set str=%str%!i!.mpg
    goto :string_start
    )
    set str=%str%+!i!.mpg
    goto :string_start
)
echo %str%

format string

:format_string
set i=0
set str=%1
set str=%str:"=%
:format_start
if %i% LSS 50 (
    set /a i=i+1
    rem set str=%str%-
    if "!str:~%i%,1!"=="" set str=%str%-
    goto :format_start
)
rem echo %str%
rem return the result in 2nd parameter
set %2=%str%
goto :eof

Examples

Batch Menu

@echo off
rem setlocal enabledelayedexpansion
rem created by Johnny.Zhang
rem 15910095297@139.com
rem 2013-06-19
rem windows 7
if "%1" neq "" set BASE_DIR=%1

:main
cls
echo.
echo ------------------------------------------------------------------------------
echo. USER: %username%    HOST: %computername%    DATE: %date:~3,10%
echo ------------------------------------------------------------------------------
echo. 1.initialize: preparation before compiling the project.
echo. 2.clean     : remove all files generated by the previous build.
echo. 3.compile   : compile the source code of the project.
echo. 4.test      : run tests using a suitable unit testing framework.
echo. 5.package   : package the compiled code in its distributable format.
echo. 6.install   : install the package into the local repository.
echo. 7.deploy    : deploy the final package to the remote repository.
echo. 8.run       : run the project.
echo. 9.site      : generate the project's site document.
echo. c.clean     : remove all eclipse project files.
echo. d.dependency: provides the capability to manipulate artifacts.
echo. e.eclipse   : generate eclipse project files for the project.
echo. g.git pull  : sync the local repository with the remote.
echo. 0.exit      : exit the program.
echo ------------------------------------------------------------------------------
choice /c:123456789cdeg0 /M "Your choice:"
echo ------------------------------------------------------------------------------
call :%errorlevel%
if errorlevel 0 goto end
echo [WARNING] Invalid choice!

:end
echo -------------------------------------------------------------------------------
echo Press any key to return to the main menu.&&pause>nul
goto main

:call_func
rem echo call_func %1
if defined BASE_DIR (
    call %BASE_DIR%\quickstart.bat %1
) else (
    call quickstart.bat %1
)
rem function end
goto :eof

:1
echo 1.initialize: preparation before compiling the project.
call :call_func init_pr
rem function end
goto :eof

:2
echo 2.clean: remove all files generated by the previous build.
call :call_func mvn_clean
rem function end
goto :eof

:3
echo 3.compile: compile the source code of the project.
call :call_func mvn_compile
rem function end
goto :eof

:4
echo 4.test: run tests using a suitable unit testing framework.
call :call_func mvn_test
rem function end
goto :eof

:5
echo 5.package: package the compiled code in its distributable format.
call :call_func mvn_package
rem function end
goto :eof

:6
echo 6.install: install the package into the local repository.
call :call_func mvn_install
rem function end
goto :eof

:7
echo 7.deploy: deploy the final package to the remote repository.
call :call_func mvn_deploy
rem function end
goto :eof

:8
echo 8.run: run the project.
call :call_func mvn_run
rem function end
goto :eof

:9
echo 9.site: generate the project's site document.
call :call_func mvn_site
rem function end
goto :eof

:10
echo c.clean: remove all eclipse project files.
call :call_func mvn_eclipse_clean
rem function end
goto :eof

:11
echo d.dependency: provides the capability to manipulate artifacts.
call :call_func mvn_dependency
rem function end
goto :eof

:12
echo e.eclipse: generate eclipse project files for the project.
call :call_func mvn_eclipse
rem function end
goto :eof

:13
echo g.git pull: sync the local repository with the remote.
call :call_func git_pull
rem function end
goto :eof

:14
echo 0.exit: exit the program.
rem function end
exit

REFERENCES



blog comments powered by Disqus