Some tricks with Windows Batch Scripting

Phần tổng hợp các tricks khi viết batch script
(Bài viết dựa trên googling, và những kinh nghiệm cá nhân)

Tips khi viết batch file

① Get/format OS date time:

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set MIN=%dt:~10,2%
set SEC=%dt:~12,2%

Từ đây có thể lấy được thông tin date time theo những format khác nhau tùy theo từng mục đích sử dụng
Ex:

set DATE_YYYY_MM_DD_FORMAT=%YYYY%-%MM%-%DD%
set CURRENT_DATE_TIME=%YYYY%/%MM%/%DD% %HH%:%MIN%:%SEC%

và kết quả thu được:
2014-09-05
2014/09/05 15:10:16

Ngoài ra, có thể dùng các biến môi trường của hệ thống: %date%%time% với default format trong phần setting region.
echo %date%%time%
2014/09/05 15:10:16

② for loop:
Cơ bản: http://www.robvanderwoude.com/for.php

■ listing thư mục

rem path to directory
set USR_DIR=C:\Users
for /F "tokens=*" %%I in ('DIR /B /AD "%USR_DIR%"') do (
rem do something here
)

Tương tự như trên, bằng cách thay đổi câu lệnh dir trong ngoặc, ta có thể get được list file trong thư mục…

■ Dùng lệnh set để list ra những biến môi trường
Gõ câu lệnh sau trên CMD:
SET User

Kết quả của câu lệnh:
USERDOMAIN=YOURPC
USERNAME=You
USERPROFILE=C:\Documents and Settings\You

Như vậy, có thể dùng câu lệnh SET để list ra các biến có khai báo tương tự nhau.

Thử với câu lệnh khác:

SET Pro
Kết quả sẽ trả về như dưới đây

PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 14 Stepping 8, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0e08
ProgramFiles=C:\Program Files
PROMPT=$P$G

Với câu lệnh loop và :
FOR /F "tokens=2* delims=_=" %A IN ('SET PROCESSOR_') DO @ECHO PROCESSOR %A : %B

Tham khảo: http://www.robvanderwoude.com/battech_array.php

③ Một số biến môi trường thường dùng


DATE The current date using same region specific format as DATE.
TIME The current time using same format as TIME.
ERRORLEVEL The current ERRORLEVEL value, automatically set when a program exits.
SYSTEMDRIVE C:
SYSTEMROOT C:\Windows
TEMP and TMP User Variable C:\Users\{Username}\AppData\Local\Temp
Under XP this was \{username}\Local Settings\Temp
USERNAME {username}
USERPROFILE %SystemDrive%\Users\{username}
WINDIR C:\Windows
RANDOM A random decimal number between 0 and 32767.

(ref: http://ss64.com/nt/syntax-variables.html)

More reference:
Batch Files & Batch Commands: http://www.robvanderwoude.com/batchcommands.php

Các tricks trên batch file & command prompt
* Xuống dòng:
echo.
* Multi lines: sử dụng ^
echo hello ^
o2o^
!^

(ref: http://stackoverflow.com/questions/69068/long-commands-split-over-multiple-lines-in-vista-dos-batch-bat-file/4455750#4455750)
* Ghi ra file:
echo "hello" > file
echo " world" >> file
* Split string: sử dụng ~
set test_str="hello world" > file
echo %test_str:~0,6%
echo %test_str:~6,5%

More reference:
[1] http://www.robvanderwoude.com/clevertricks.php
[2] http://www.guidingtech.com/12909/brilliant-command-prompt-cmd-tricks/
[3] …

(to be updated…)