【发布时间】:2021-02-21 13:47:56
【问题描述】:
我正在尝试创建一个脚本,以允许用户在打印之前将默认 Windows 打印机临时设置为选定的打印机(PDF、XPS、OneNote),然后在指定时间后自动将默认设置回原始打印机。
我已经设法合并了许多不同示例的元素以到达脚本运行的阶段,尽管它在列表的开头和结尾创建了一个额外的条目。 我想过滤用户显示的打印机列表,只显示适当列表中的打印机(例如 PDF、XPS、OneNote)。
任何帮助将不胜感激。
@echo off
setlocal enableDelayedExpansion
FOR /F "tokens=2* delims==" %%A in (
'wmic printer where "default=True" get name /value'
) do SET DefaultPrinter=%%A
ECHO.
ECHO ==============================================================
ECHO Current Default Printer
ECHO ==============================================================
ECHO.
ECHO Default Printer = %DefaultPrinter%
ECHO.
pause
Cls
ECHO ==============================================================
ECHO Processing locally installed printers
ECHO ==============================================================
::build "array" of printers
set printerCnt=0
for /f "eol=: delims=" %%F in ('wmic printer get Name') do (
set /a printerCnt+=1
set "printer!printerCnt!=%%F"
)
::print menu
for /l %%N in (1 1 %printerCnt%) do echo %%N - !printer%%N!
echo(
:get selection
set selection=
set /p "selection=Enter a printer number: "
echo You picked !printer%selection%!
set NewPrinter=!printer%selection%!
::trim selection
set str=%NewPrinter%
for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%str%"
TIMEOUT 10
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%DefaultPrinter%"
[编辑 /] 我已经包含了我最终使用的代码。这是基于@Hackoo 提供的答案,我也合并了@Magoo 提供的元素。可能有一种更优雅的方式来编写这段代码,但我是新手,所以按照我的理解进行了布局。
@echo off
Mode 70,30 & color F0
Title Healthmail Printer Selection
FOR /F "tokens=2* delims==" %%A in (
'wmic printer where "default=True" get name /value'
) do SET "DefaultPrinter=%%A"
ECHO.
ECHO ==============================================================
ECHO Current Default Printer
ECHO ==============================================================
ECHO.
ECHO Default Printer = [%DefaultPrinter%]
ECHO.
TIMEOUT /T 2 /NoBreak>nul
Cls
ECHO.
ECHO ==============================================================
ECHO Available Printers
ECHO ==============================================================
Setlocal EnableDelayedExpansion
::build "array" of printers
Set "FilterList=%Temp%\FilterList.txt"
REM Create a file to filter just for PDF printers
>"%FilterList%" (
echo PDF
)
set printerCnt=0
@for /f "delims=" %%a in (
'wmic printer get Name ^|findstr /G:%FilterList%"'
) do (
@for /f "delims=" %%b in ("%%a") do (
set /a printerCnt+=1
set "printer!printerCnt!=%%~nb"
)
)
::If no printers have been found, go to filterlistlong
:zerofilterprinters
IF !printerCnt!==0 GOTO :filterlistlong
IF !printerCnt! gtr 0 GOTO :printmenu
:filterlistlong
Set "FilterListLong=%Temp%\FilterListLong.txt"
REM Create a file to filter with each word like XPS,OneNote and PDF
>"%FilterListLong%" (
echo PDF
echo XPS
echo OneNote
)
set printerCnt=0
@for /f "delims=" %%a in (
'wmic printer get Name ^|findstr /G:%FilterListLong%"'
) do (
@for /f "delims=" %%b in ("%%a") do (
set /a printerCnt+=1
set "printer!printerCnt!=%%~nb"
)
)
:printmenu
SET "choices="
for /l %%N in (1 1 %printerCnt%) do SET "choices=!choices!%%N"&SET "printer%%N=!printer%%N!"&IF %printercnt% gtr 1 echo %%N - !printer%%N!
echo(
:get selection
:: If no available options, exit
IF NOT DEFINED choices GOTO :EOF
:: If only 1 option, auto-select that
IF %choices%==1 SET /a selection=1&GOTO autoselect
choice /c %choices% /m "Enter a printer number: "
set selection=%ERRORLEVEL%
:: Error condition
IF %selection% gtr %printercnt% GOTO :EOF
:: Control-C
IF %selection% equ 0 GOTO :EOF
cls
ECHO ==============================================================
ECHO You selected: !printer%selection%!
ECHO ==============================================================
echo.
echo. Please proceed to print document.
echo.
echo. This window will close automatically in 30 seconds.
::GOTO :setprinter
:autoselect
set NewPrinter=!printer%selection%!
cls
ECHO ==============================================================
ECHO Printer selected: !printer%selection%!
ECHO ==============================================================
echo.
echo. Please proceed to print document.
echo.
echo. This window will close automatically in 10 seconds.
:setprinter
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%NewPrinter%"
TIMEOUT /T 10 /nobreak > NUL
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%DefaultPrinter%"
GOTO :EOF
If Exist "%FilterList%" Del "%FilterList%"
If Exist "%FilterListLong%" Del "%FilterListLong%"
【问题讨论】:
-
如果您想过滤您的枚举打印机列表,您应该能够将其直接合并到您的
WMIC命令中。例如:… Printer Where "Name Like 'OneNote%%' Or Name Like '%%PDF%%' Or Name Like '%%XPS%%'" Get …。我没有直接将其合并到我的答案中,(或其最近添加的附加编辑,以同时满足 Windows 7 和 Server 2008 R2),但如果您愿意,它不应该给您带来问题进行调整。
标签: windows batch-file cmd