Cách sử dụng tùy chọn choice command và set command trong viết tệp batch

choice command và set command là hai tùy chọn sử dụng thiết lập đặt điều kiện cho hành động trước khi xử lý và thực thi các lệnh tiếp theo. Sự khác nhau cơ bản của 2 tùy chọn này nếu như choice command chỉ sử dụng với 1 phím trong khi set command có thể sử dụng nhiều phím cộng với phím enter lựa chọn.

Khi viết tệp batch sẽ sử dụng menu trong đó liệt kê danh sách các tùy chọn cùng với thiết lập điều kiện chạy một mục nào đó trong danh sách. Điều kiện đó chính là sử dụng choice command và set command

Các ví dụ minh họa

Giả sử khi viết một tệp batch custom_script.cmd đơn giản sử dụng hai tùy chọn choice command và set command xử lý menu bên dưới

Copy

@echo off

title custom script

cls



:menu

cls

echo.Run Default Program

echo.

echo. ID Option

echo.

echo. 1 Internet Explorer

echo. 2 Notepad

echo. 3 Paint

echo. 4 Snipping Tool

echo. 5 Windows Media Player

echo. 6 Control Panel

echo. 7 File Explorer

echo. 8 Task Manager

echo. 9 Services

echo.

Sử dụng tùy chọn choice command

Copy

@echo off

title custom script

cls



:menu

cls

echo.Run Default Program

echo.

echo. ID Option

echo.

echo. 1 Internet Explorer

echo. 2 Notepad

echo. 3 Paint

echo. 4 Snipping Tool

echo. 5 Windows Media Player

echo. 6 Control Panel

echo. 7 File Explorer

echo. 8 Task Manager

echo. 9 Services

echo.

choice /c:123456789 /n /m "Select ID for continue : "

if %errorlevel% EQU 1 "C:Program FilesInternet Exploreriexplore.exe" & goto menu

if %errorlevel% EQU 2 notepad && goto menu

if %errorlevel% EQU 3 mspaint && goto menu

if %errorlevel% EQU 4 SnippingTool && goto menu

if %errorlevel% EQU 5 "%ProgramFiles%Windows Media Playerwmplayer.exe" && goto menu

if %errorlevel% EQU 6 control & goto menu

if %errorlevel% EQU 7 explorer & goto menu

if %errorlevel% EQU 8 taskmgr && goto menu

if %errorlevel% EQU 9 services.msc && goto menu

Sử dụng tùy chọn set command

Copy

@echo off

title custom script

cls



:menu

cls

echo.Run Default Program

echo.

echo. ID Option

echo.

echo. 1 Internet Explorer

echo. 2 Notepad

echo. 3 Paint

echo. 4 Snipping Tool

echo. 5 Windows Media Player

echo. 6 Control Panel

echo. 7 File Explorer

echo. 8 Task Manager

echo. 9 Services

echo. E Exit

echo.

set /p option=Select ID and press Enter :

if %option% EQU 1 "C:Program FilesInternet Exploreriexplore.exe" & goto menu

if %option% EQU 2 notepad && goto menu

if %option% EQU 3 mspaint && goto menu

if %option% EQU 4 SnippingTool && goto menu

if %option% EQU 5 "%ProgramFiles%Windows Media Playerwmplayer.exe" && goto menu

if %option% EQU 6 control & goto menu

if %option% EQU 7 explorer & goto menu

if %option% EQU 8 taskmgr && goto menu

if %option% EQU 9 services.msc && goto menu

if %option% EQU E exit

Như vậy có thể thấy sự khác nhau cơ bản nếu sử dụng tùy chọn choice chỉ sử dụng được tất cả 9 tùy chọn từ 1 đến 9 và điều kiện sử dụng if %errorlevel%, trong khi sử dụng tùy chọn set có thể sử dụng nhiều hơn 9 tùy chọn với điều kiện tự đặt không bắt buộc ví dụ đặt tùy chọn option thì sử dụng điều kiện if %option%.

Nhược điểm của tùy chọn set command phải sử dụng nhiều từ ít nhất 2 phím trở lên trong đó có phím Enter, ví dụ khi sử dụng tùy chọn Yes No để xử lý hành động

– choice command

Copy

choice /c:YN /n /m "Are you sure? (Y/N) "

if %errorlevel% EQU 1 explorer & goto menu

if %errorlevel% EQU 2 goto menu

Như vậy với tùy chọn này chỉ cần nhấn 1 phím Y hoặc N để xử lý hành động

– set command

Copy

set /p option="Are you sure? (Y/N) "

if %option% EQU Y explorer & goto menu

if %option% EQU N goto menu

Như vậy với tùy chọn này cần nhấn phím Y + Enter hoặc N + Enter để xử lý hành động

Kết luận

Khi xây dựng menu nếu chỉ có 9 tùy chọn trở xuống thì sử dụng điều kiện choice command nhưng từ 10 tùy chọn trở lên sử dụng điều kiện set command

View Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Published by
4 years ago