Quantcast
Channel: windows –易春木
Viewing all articles
Browse latest Browse all 57

windows 程式開發: Batch File Command 概述一下

$
0
0

有一些常出現的關鍵字如下, 把這些學起來
就已經會了一大半了, 加油!!

常見關鍵字/符號:

@
ECHO
FOR
IF
PAUSE
%VARIABLE%
%DIGIT
REM
CLS

開始介紹:

——————————————————————————–

@ 隱藏其後的指令,不會顯示於Command Prompt中.

可用於簡化畫面;也或者不想讓人知道下了何種指令?(Hacking?)
Syntax:
@[Command]
Example:
@echo Off

ECHO 顯示其後的訊息

Syntax:
echo [On|Off|Message|.]
Example:
echo 顯示echo目前狀態(On or Off)
echo Off 隱藏Windows Command Prompt中的訊息
echo On 恢復顯示Windows Command Prompt中的訊息
echo Hello world! 在Command Prompt顯示Hello world!
echo. 顯示空行
echo N|del *.* 預先提供答案給指令. (del *.*? => No)

FOR 對清單中每個成員重複執行相同指令.

Syntax:
FOR %%argument IN (list) DO command
argument – 從A-Za-z任何字元皆可
list – 由逗號,或空格所隔開的字串皆可
command- 指令
Example:
FOR %%i IN (A,B,C) DO echo %%i
印出A,B,C
FOR %%f IN (*.TXT *.BAT *.DOC) DO type %%f
印出所有txt, bat, doc檔案內容
FOR %%f IN (*.PAS) DO call compile %%f
Complile所有PAS檔案

IF 判斷式.

Syntax:
IF [not] condition (
command [command-parameter]
) ELSE (
command [command-parameter]
)
Example:
IF string1==string2 echo string1 equal to string2
如果string1等於string2,在螢幕上印出string1 equal to string2
IF exist a.txt echo del a.txt
如果a.txt存在則刪除他

PAUSE 暫停執行批次程式,並且顯示 Press any key to continue…

Syntax:
PAUSE
Example:
pause

SET 顯示,設定環境變數

Syntax:
SET [ variable=[string]]
Example:
set 顯示目前環境變數
set P 列出所有以’P’開頭的環境變數
set USER=Tom 將USER加入環境變數中
set PATH=C:\test;%PATH%
將C:\test加入目前的環境變數中(只對當前的Command Prompt有效)
set /P str=Message
在螢幕上顯示Message,並將使用者輸入設定為變數str

%Variable% 代表該環境變數的值

Syntax:
%Variable%
Example:
set USER=Tom
echo %USER% 螢幕將顯示Tom

%DIGIT batch file所接受的參數

Syntax:
%digit digit可接受數字為1~9
Example:
C:\test.bat string
%1將等於"string"

REM 註解符號

也可以用 :: 代替
Syntax:
REM [Message]
:: [Message]
Example:
REM this is comment
:: this is comment

CLS 清除畫面

Syntax:
CLS
Example:
cls


Viewing all articles
Browse latest Browse all 57

Trending Articles