Windows執行檔的各種啟動方法(初學者的命令行 #2 )

隨著Windows的版本歷程,它的可執行檔有著許多「變形」,本文彙總如下。

可執行檔

除了cmd.exe的內部命令外,某個可執行檔,不管它的格式或副檔名是什麼,只要能被命令直譯器搜尋到,就可以被執行。而搜尋的依據是環境變數PATH設定的路徑決定。

如果被執行的命令沒有副檔名,則搜尋時會搭配環境變數PATHEXT的順序來匹配符合的檔案。

▼ 顯示PATH和PATHEXT的內容
PATH

我們可以用where來確認某個執行檔的存位位置。

C:\Users\jerry>where notepad.exe     
C:\Windows\System32\notepad.exe      
C:\Windows\notepad.exe               

C:\Users\jerry>where cmd.exe         
C:\Windows\System32\cmd.exe          

執行的方法

直接輸入可執行檔的檔名,若有指定路徑則直接由路徑裡找檔案,沒有路徑則用PATH搜尋,沒有副檔名則依PATHEXT的順序尋找。

另一個方法是用 cmd.exe /c 或 /k 執行:

/C      執行 string 所指定的命令並結束命令字元視窗     
/K      執行 string 所指定的命令但留在命令提示字元視窗   

cmd.exe的內部內部 start 也是用來啟動可執行檔的,其格式是:

start "名稱" 可執行檔名 [參數]

doskey.exe也可以用來建立cmd.exe的巨集:

doskey 巨集=可執行檔
doskey /macros

定義好的巨集可以在cmd.exe裡直接執行,用 /macros 則可列出所有的巨集。我們可以把常用的巨集編寫在一個文字檔裡,用/macrofile載入。

▼ 巨集文字檔範例

[cmd.exe]
  alias=doskey /macros
  ls=dir /w $*
  ll=dir /od $*
  svc=services.msc
  cd3=cd ..\..\..\$*
  cd2=cd ..\..\$*
  p=cd "c:\Program Files"
  pp=cd "c:\program Files (x86)"

▼ 載入巨集

doskey /macrofile=z:\util\macros.txt
alias

cmd.exe的內部命令

cmd.exe執行後便能執行其內部命令,清單如下:

內部命令 功能說明
ASSOC Change file extension associations
CALL Call one batch program from another
CD Change Directory – move to a specific Folder
CLS Clear the screen
COLOR Change colors of the CMD window
COPY Copy one or more files to another location
DATE Display or set the date
DEL Delete one or more files
DIR Display a list of files and folders
ECHO Display message on screen
ENDLOCAL End localisation of environment changes in a batch file
ERASE Delete one or more files
EXIT Quit the current script/routine and set an errorlevel
FOR /F Loop command: against a set of files
FOR /F Loop command: against the results of another command
FOR Loop command: all options Files, Directory, List
FTYPE File extension file type associations
GOTO Direct a batch program to jump to a labelled line
IF Conditionally perform a command
MD Create new folders
MKLINK Create a symbolic link (linkd)
MOVE Move files from one folder to another
PATH Display or set a search path for executable files
PAUSE Suspend processing of a batch file and display a message
POPD Return to a previous directory saved by PUSHD
PROMPT Change the command prompt
PUSHD Save and then change the current directory
REM Record comments (remarks) in a batch file
REN Rename a file or files
RD Delete folder(s)
SET Display, set, or remove session environment variables
SETLOCAL Control the visibility of environment variables
SHIFT Shift the position of batch file parameters
START Start a program, command or batch file
TIME Display or set the system time
TITLE Set the window title for a CMD.EXE session
TYPE Display the contents of a text file
VER Display version information
VERIFY Verify that files have been saved
VOL Display a disk label
:: Comment / Remark

MS-DOS的「歷史遺跡」

.com和.exe都是由MS-DOS時代遺留至今的可執行檔格式,二者差異已可忽略了。.com在Windows 10版本1809裡仍留存在,但數量不多,例如:chcp.com、format.com、more.com等。

腳本形式的可執行檔

可以執行.com、.exe和cmd.exe內部命令,再加上較簡單的流程控制等的文字檔,只要副檔名是.bat或.cmd,即可自動被系統執行。

我們可以建立一個設定操作環境的批次檔,例如叫做 z:\util\myenv.bat,用來取代啟動ConEmu Admin task裡的CmdInit.cmd:

set PATH=z:\util;%PATH%
cd /d Z:\util

myenv.bat把Z:\util目錄加到搜尋執行檔的PATH變數裡,以後我們自己添加在z:\util 裡的命令就會第一個被找到而執行。

conemu

控制台命令

Windows 10仍有幾個副檔名是.cpl(Control Panel)的命令,是控制台的設定視窗。

檔名 用途
appwiz.cpl 控制台\程式集\程式和功能
bthprops.cpl 藍牙與其他裝置
desk.cpl 顯示器
desk.cpl,0 桌面圖示設定
firewall.cpl 控制台\系統及安全性\Windows Defender 防火牆
FlashPlayerCPLApp.cpl Flash Player設定管理員
hdwwiz.cpl 裝置管理員
inetcpl.cpl 網際網路 - 內容
intl.cpl 地區設定
irprops.cpl 紅外線
javacpl.cpl Jave控制面板(若有安裝JRE/JDK)
joy.cpl 遊戲控制器
main.cpl 滑鼠 - 內容
mmsys.cpl 聲音
ncpa.cpl 控制台\網路和網際網路\網路連線
powercfg.cpl 控制台\硬體和音效\電源選項
sapi.cpl 語音內容
sysdm.cpl 系統內容 (我的電腦→內容)
telephon.cpl 位置資訊(電話)
timedate.cpl 日期和時間
wscui.cpl 控制台\系統及安全性\安全性與維護

微軟主控台

微軟主控台(Micorsoft Management Console)是嵌入式的設定管理程式,副檔名是.msc,常見的如下:

檔名 用途
compmgmt.msc 電腦管理(我的電腦→管理)
devmgmt.msc 裝置管理員
diskmgmt.msc 磁碟管理
eventvwr.msc 事件檢視器(另有eventvwr.exe)
fsmgmt.msc 共用資料夾
gpedit.msc 本機群組原則編輯器
lusrmgr.msc 本機使用者和群組
perfmon.msc 效能監視器
services.msc 服務
taskschd.msc 工作排程器
WF.msc 具有進階安全性的 Windows Defender 防火牆

這麼多!?

這麼多命令怎麼記啊?嚇死人了,還是繼續用圖形介面好了...

其實經常用的命令多下個幾次後,不知不覺間就記起來了,千萬不要因為數量而拒絕追求效率的目標了。

請參考影片的示範:

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *