Bat脚本
1.常用语法
(1).接受运行参数
@echo off
setlocal
TITLE 脚本窗口名称
echo 设置窗口字体颜色
COLOR A
set /p var=接受参数:
echo 打印接收参数%var%
echo 调用外部脚本
python G:/demo.py %var%
echo 执行完毕,任意键退出
PAUSE >nul
EXIT
(2).以管理员身份运行
echo 以管理员身份运行
PUSHD %~DP0 & cd /d "%~dp0"
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas
(3).关闭进程
echo 关闭Redis服务端与客户端
taskkill /im redis-server.exe /f
echo 关闭6379端口
set port=6379
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port2%"') do taskkill /pid %%m
(4).启动exe
echo 启动指定目录exe程序
echo 名称 切换盘符 进入具体目录指定exe文件 并执行命令
start "Redis服务端" /d G:\ cmd /k "cd software\Redis-x64-3.0.504&&redis-server.exe redis.windows.conf"
echo 打开网页
cd /d C:\Google\Chrome\Application
start chrome.exe https://www.baidu.com
(5).nircmd
@echo off
chcp 65001
set desktop=C:\Users\anubis\Desktop
set NIRCMD_PATH="C:\Windows\nircmd.exe"
echo [1] 开始聚焦 aTrust 窗口
%NIRCMD_PATH% win activate title "窗口名称"
timeout /t 2 >nul
echo [2] 移动鼠标
%NIRCMD_PATH% setcursorwin 100 50
echo [3] Tab 定位到密码输入框
%NIRCMD_PATH% sendkey 0x9 press
%NIRCMD_PATH% sendkey 0x9 press
echo [4] 设置剪贴板 -> password
%NIRCMD_PATH% clipboard set "password"
echo [5] 粘贴
%NIRCMD_PATH% sendkey ctrl down
%NIRCMD_PATH% sendkey v down
%NIRCMD_PATH% sendkey ctrl up
%NIRCMD_PATH% sendkey v up
echo [5] 聚焦程序窗口
%NIRCMD_PATH% win activate title "窗口名称"
timeout /t 2 >nul
@echo off
chcp 65001
setlocal enabledelayedexpansion
set desktop=C:\Users\anubis\Desktop
set NIRCMD_PATH="C:\Windows\nircmd.exe"
echo [0] 启动指定目录exe,并等待启动
cd /d "C:\Program Files (x86)\Sangfor\"
start "" "text.exe"
timeout /t 3 >nul
echo [1] 递归循环检测窗口
:LOOP
:: 检测窗口是否存在
set "windowTitle=窗口名称"
set "windowFound=false"
for /f "tokens=*" %%a in ('tasklist /v ^| findstr /i "%windowTitle%"') do (
set "windowFound=true"
)
:: 如果窗口存在,则继续执行
if !windowFound! equ true (
echo [3] aTrust 窗口存在
goto CONTINUE
) else (
echo [2] aTrust 窗口不存在等待中...
timeout /t 3 >nul
goto LOOP
)
:CONTINUE
echo [4] 开始聚焦窗口
%NIRCMD_PATH% win activate title "窗口名称"
timeout /t 2 >nul
echo [5] 移动鼠标
%NIRCMD_PATH% setcursorwin 100 50
echo [6] Tab 定位到密码输入框
%NIRCMD_PATH% sendkey 0x9 press
%NIRCMD_PATH% sendkey 0x9 press
echo [7] 设置剪贴板 -> password
%NIRCMD_PATH% clipboard set "password"
echo [8] 粘贴 -> password
%NIRCMD_PATH% sendkey ctrl down
%NIRCMD_PATH% sendkey v down
%NIRCMD_PATH% sendkey ctrl up
%NIRCMD_PATH% sendkey v up
echo [9] Tab 定位到确定按钮并点击
%NIRCMD_PATH% sendkey 0x9 press
%NIRCMD_PATH% sendkey 0x9 press
echo [8] 点击 enter 键
%NIRCMD_PATH% sendkey enter press
:: 结束脚本
endlocal