아래 몇가지만 알아서 윈도에서 자주쓰는 batch file을 만들자
- Input 받아 command에 넣기
- input validate하기
- file/folder 있는지 확인후 만들기, 없으면 에러나 만들기
- if else
- for loop
- 외부 프로그램 실행 = start
- input받아 실행하기
- 현재날짜 format대로 출력
- 현재 dir 얻기, 이전 cur path로 돌아가기
- sleep, timeout
- 외부파일에서 parameter 순차적으로 읽기
- 환경variable에서 읽기, PATH나 user variable like msm
- 오늘날짜 구하기
http://en.wikibooks.org/wiki/Windows_Batch_Scripting
http://stackoverflow.com/search?q=windows+batch
1. Input이 없을때 나가기, 처음 3개 character비교하기
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- rem argument check, no arument exit if -%1-==-- echo Argument one not provided & exit /b rem user input, %1, %2 and so on set a=%1 rem set a=abcd echo %a:~0,3% if %a:~0,3%==COM echo same if exist file.txt ( echo The file exists. ) else ( echo The file does not exist. ) rem directory check if exist temp ( echo The temp exists. ) else ( echo The temp does not exist. ) rem if directory is not available, create it if not exist temp mkdir temp goto :mylabel echo Hello 1 REM Hello 1 never gets printed. :mylabel echo Hello 2 goto :eof rem robocopy, xcopy, copy sleep 5 rem timeout timeout /t 5 #user input set /P user_input=Enter an integer:
2. 간단한 Monkey 예제
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- set arg1=%1 set iteration=1 set num=1 rem note: it end with \, %mypath:~0,-1% will remove \ set mypath=%~dp0 set log_path=%mypath%Logs if exist log_path( echo Logs folder exists already. ) else ( mkdir log_path echo Logs Folder created! ) :loop adb root adb wait-for-device adb remount sleep 5 start adb_logs.bat %iteration% %log_path% start Kernel_logs.bat %iteration% %log_path% rem adb shell monkey -p com.android.music -p com.android.soundrecorder -s 100 --throttle 500 1000000000 adb shell monkey -p com.android.music -p com.android.soundrecorder -p com.android.voicedialer -s 100 --throttle 500 1000000000 set /a iteration= %iteration% + %num% echo %iteration% sleep 600 mv C:\ProgramData\Qualcomm\QPST\Sahara\Port_%arg1% C:\ProgramData\Qualcomm\QPST\Sahara\Port_%arg1%_%iteration% goto loop
3. input path 받아 fastboot flash 하기
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- setlocal enabledelayedexpansion for %%i in (1) do ( set /p c=enter a build location? echo waiting for device echo Flashing boot image fastboot.exe flash boot !c!\boot.img echo Flashing userdata image fastboot.exe flash userdata !c!\userdata.img echo Flashing system image fastboot.exe flash system !c!\system.img echo Flashing recovery image fastboot.exe flash recovery !c!\recovery.img echo Flash completed, rebooting device fastboot.exe continue sleep 3 )
4. function 예제
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- @echo off call :power %1 %2 echo %result% rem Prints 16, determined as 2 * 2 * 2 * 2 goto :eof rem __Function power______________________ rem Arguments: %1 and %2 :power setlocal set counter=%2 set interim_product=%1 :power_loop if %counter% gtr 1 ( set /A interim_product = %interim_product% * %1 set /A counter = %counter% - 1 goto :power_loop ) endlocal & set result=%interim_product% goto :eof
5. 간단한 fastboot flash 처리예제, environment variable 읽어서 처리하기. msm is Env variable
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- if -%msm%- == -- ( echo no msm defined, set msm8992 set msm=msm8992 ) set out=out\target\product\%msm% rem if no parameter rem check current directory then $OUT directory if -%1-==-- ( echo Argument one not provided, Assume current directory call :function_fastboot echo IMG flashed done goto :eof ) rem if parameter(PATH) are given rem check network path or local path set in_path=%1 echo %in_path:~0,2% if %in_path:~0,2%==\\ ( echo Network path, pushd %in_path% and will do popd later pushd %in_path% call :function_fastboot popd ) else ( echo LOCAL path found CD %in_path% cd %in_path% call :function_fastboot ) goto :eof rem ________________________ rem FUNCTION flash FASTBOOT rem -------------------------------------------- :function_fastboot setlocal if exist system.img ( echo current directory has IMG files fastboot flash aboot emmc_appsboot.mbn fastboot flash boot boot.img fastboot flash cache cache.img fastboot flash persist persist.img fastboot flash recovery recovery.img fastboot flash userdata userdata.img fastboot flash system system.img ) else ( if exist %out%\system.img ( echo IMG found in %out% fastboot flash aboot %out%\emmc_appsboot.mbn fastboot flash boot %out%\boot.img fastboot flash cache %out%\cache.img fastboot flash persist %out%\persist.img fastboot flash recovery %out%\recovery.img fastboot flash userdata %out%\userdata.img fastboot flash system %out%\system.img ) else ( echo no IMG file found!!!! EXIT. exit /b ) ) endlocal goto :eof
오늘 날짜구하기 예제
@echo off rem ------------------------------- rem Fred Oh (email) rem 2015 01 01 starting new life rem ------------------------------- For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b) rem For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b) For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b) echo %mydate%_%mytime% For /f "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~8,2%%ldt:~10,2%%ldt:~12,2% echo %ldt% rem %date% = Mon 10/12/2015 set year=%date:~-4% rem echo year=%year% set month=%date:~4,2% rem echo month=%month% set day=%date:~7,2% rem echo day=%day% set today=%month%%day% echo today is %today%
'Programming' 카테고리의 다른 글
Web Sequence diagram blog에 넣기 (0) | 2015.04.16 |
---|---|
Popcorn Time clone해서 build하기 (0) | 2015.04.14 |
BAT/CMD windows script to EXE executable converter (0) | 2015.03.17 |
Bash shell script (0) | 2015.03.14 |
syntax highlight test (0) | 2015.03.03 |