跳转到内容
新建笔记

CMD

终端窗口
# create filefolder
mkdir filefolderName # /p
# create file
type nul > test.cpp
copy nul > test.cpp
echo. > test.cpp
where vim

示例1:启动s1.exe server。

终端窗口
@echo off
:: 关闭命令回显,使得批处理执行时不会显示这些命令本身。
:start
echo Starting s1.exe in server mode...
:: 打印信息到控制台,告知用户正在启动s1.exe。
start /b s1.exe server
:: 使用start命令启动s1.exe程序,并带参数server。/b参数表示在不创建新窗口的情况下启动。
echo s1.exe is now running in server mode. Press Ctrl+C to exit or close this window to terminate the process.
:: 打印信息告知用户程序正在以服务器模式运行,并提示如何退出。
:wait
choice /m "Press any key to close the window and terminate the process or just close the window to keep it running."
:: 使用choice命令提示用户输入,如果用户按下任意键,则执行下面的if语句。
if errorlevel 1 goto end
:: 如果用户按下任意键(errorlevel会被设置为1),则跳转到end标签处。
goto wait
:: 如果用户没有按键,再次跳转到wait标签,形成循环。
:end
echo Terminating s1.exe...
:: 打印信息告知用户正在终止s1.exe。
taskkill /f /im s1.exe
:: 使用taskkill命令强制终止进程名是s1.exe的进程。
echo s1.exe has been terminated.
:: 打印信息告知用户s1.exe已被终止。
pause
:: 暂停批处理执行,等待用户按下任意键后继续。