批处理脚本-网络打印机安装工具v1.2
@echo off & title 网络打印机安装工具
:: time:20170830 测试环境:win7x64
color 0B & cd /d %~dp0
setlocal enabledelayedexpansion
::Menu文件里面的是显示的菜单内容,List文件里面是标签列表
::更新说明: Menu文件和List在temp生成
::此处自定义预设配置文件
set IniFile=printerdata.ini
::以下为主代码,没什么问题,请勿修改,以免出错
:Menu
cls & call :GetMenuList
set "Choice_12=" & set "Line=" & set "IniLabel=" & set "NoLabel="
set "Skip=" & set "Str_Num=" & set "Num=" & set "ln=" & set "sec="
set "PrinetrName=" & set "ip=" & set "infName=" & set "inf="
type "%Menu_2%"
set /p Choice_12=请输入对应数字编号:
if defined Choice_12 (
echo %Choice_12%|findstr /c:" " >nul && (
goto InputError
) || (
echo %Choice_12%|findstr ^[0-9][0-9]*$ >nul && (
goto GetIniValue
) || (
goto InputError
)
)
)
:InputError
echo, & echo 请务必输入纯数字,且不能为空或有空格。
ping 127.0.0.1 -n "2">nul
goto Menu
:GetIniValue
for /f "tokens=2 delims=、" %%a in ('findstr ^%Choice_12%、 "%Menu_1%"') do set "IniLabel=%%~a"
if not defined IniLabel (
echo, & echo 您选择的标签项编号不存在,请重新选择。
ping 127.0.0.1 -n "2">nul
goto Menu
)
for /f "tokens=1 delims=:" %%a in ('findstr /n ^\[%IniLabel%\]$ "%IniFile%"') do set "Line=%%~a"
if defined Line (
set "Skip=skip=!Line! "
) else (
echo, & echo 配置文件标签项 [%IniLabel%] 可能丢失,请重新选择。
ping 127.0.0.1 -n "2">nul
goto Menu
)
for /f "%Skip%delims=" %%a in ('type "%IniFile%"') do (
echo %%a | findstr ^\[*\] >nul && goto Execute
set "%%~a" >nul 2>nul
)
:Execute
echo, & del /f /q "%tmp%\ChoiceMenu.*" 2>nul
for %%a in (PrinetrName,ip,infName,inf) do (
if not defined %%~a (
set /a NoLabel+=1
echo 配置文件标签项 [%IniLabel%] 中的变量名 %%~a 丢失变量值,请检查。
)
)
if defined NoLabel (
set /a sec=2+NoLabel
ping 127.0.0.1 -n "!sec!">nul
goto Menu
)
echo 开始执行,请等待。。。
@ rundll32 printui.dll,PrintUIEntry /dl /n "%PrinetrName%" /q
@ cscript %Systemroot%\System32\Printing_Admin_Scripts\zh-CN\prnport.vbs -a -r "IP_%ip%" -h "%ip%" -o raw
@ rundll32 printui.dll,PrintUIEntry /if /b "%infName%" /f "%inf%" /r "IP_%ip%" /m "%infName%" /z
echo, & echo 执行完毕,请按任意健退出。
pause>nul
exit
:GetMenuList
set "Menu_1=%tmp%\ChoiceMenu.List"
set "Menu_2=%tmp%\ChoiceMenu.Menu"
del /f /q "%tmp%\ChoiceMenu.*" 2>nul
if not exist "%IniFile%" (
echo 您预设的配置文件不存在,请检查。
ping 127.0.0.1 -n "3">nul
exit
)
for /f "tokens=1 delims=[]" %%a in ('findstr ^\[*\]$ "%IniFile%"') do (
set /a Str_Num+=1
(echo !Str_Num!、%%~a)>>"%Menu_1%"
)
if not exist "%Menu_1%" (
echo 配置文件中未发现可用标签项,请检查。
ping 127.0.0.1 -n "3">nul
exit
)
(echo ╭────────╮
echo ╭─────────┤网络打印机安装工具v1.0 ├─────────╮
echo │ ╰────────╯
echo │ │
for /f "delims=" %%a in ('type "%Menu_1%"') do (
if not defined Num (
set Num= %%~a
set /a ln+=1
) else (
set Num=!Num! %%~a
set /a ln+=1
)
::下面的数字2是如果要每行选项少一点,可以更改
if !ln! equ 2 (
echo !Num!
set ln=0
set "Num="
)
)
if defined Num echo !Num!
echo.
echo.
echo.
echo ╰───────────────────────────────╯)>"%Menu_2%"
goto :eof
以下是printerdata.ini 文件的内容
[1F前台打印机]
PrinetrName=1F前台打印机
ip=192.168.1.2
inf=E:\柯尼卡打印机驱动\彩色驱动\WIN7或以上驱动\Win_x64\KOAXYJ__.inf
infName=KONICA MINOLTA C266SeriesPCL

浙公网安备 33010602011771号