I am attempting to write my first batch program to automate installing printers. I run into a problem when I try and execute
IF %input%==N (rundll32 printui.dll PrintUIEntry /in /n\\printerserver\%printer%) ELSE (rundll32 printui.dll PrintUIEntry /in /y /n\\printserver\%printer%)
It throws an error saying "rundll32 not expected"
I'm wondering if this is an error with the wrong IF syntax since I'm new to batch and, rundll32 printui.dll PrintUIEntry /in /n\\printserver\printer
works flawlessly in CMD.
:: List the printers available to install
ECHO Please type from the list the printer you would like to install (Case Sensitive): List, of, printers
:: Gives the use the choice of which printer to install and stores it in a variable
SET /p %printer%=CHOICE /c List, of, printers /cs /n /m "Please type the printer of your choice:"
:: Asks if the printer being installed should be the default printer
ECHO Would you like the printer being installed to be your default printer?
ECHO Y,N
:: Asks if the printer being installed should be the default printer and stores the answer as a variable
SET /p %input%=CHOICE /c
:: Installs the printer
IF %input%==N (rundll32 printui.dll PrintUIEntry /in /n\\printserver\%printer%) ELSE (rundll32 printui.dll PrintUIEntry /in /y /n\\printserver\%printer%)
Pause
:: Want I want to happen V
:: Ask user if they want to set printer as default
:: Store input as a variable
:: Ask what printer they want installed
:: Store input as a variable
:: Run rundll32 printui.dll PrintUIEntry /in /n\\client1\printer1 if they said no to default printer
:: Run rundll32 printui.dll PrintUIEntry /in /y /n\\client1\printer1 if they said yes to default printer
Above is the code in question to give more context, this post gave me the command I needed to install printers of batch, as well as this
Its possible I messed up a variable since I'm new to batch, but am unsure where I went wrong in this script.
SET /p %printer%=CHOICE /c List, of, printers /cs /n /m "Please type the printer of your choice:"
Thus attempts to set the variable [contents of variable printer
] to the string that follows the =
.
choice
is a command which accepts a single character from a group of options (the string following /c
). The string you specify List, of, printers
contains invalid characters (Space,) and duplicates (i
,t
,r
,s
). The characters available are roughly alphabetics and numerics. Choice
, when executed as CHOICE /c abcde /cs ...
will then set errorlevel
to the index within the string abcde
that the keystroke executed matches, so 1=a, 2=b, etc.
The correct coding would be
CHOICE /c abcde /cs ...
if errorlevel 5 instruction for e
if errorlevel 4 instruction for d
if errorlevel 3 instruction for c
if errorlevel 2 instruction for b
instruction for a
OR
CHOICE /c abcde /cs ...
goto label%errorlevel%
:label1
instructions for a
....
:label2
instructions for b
....
:label3
instructions for c
....
:label4
instructions for d
....
:label5
instructions for e
....
Note that set /p
sets the string entered [usually from the keyboard] into the variable (but if Enter alone is used, then the variable remains unchanged). Since the string is uncontrolled, it may contain characters that present difficulties to process (like &
, %
etc) or unbalanced quotes and hence is regarded as an unreliable input method when dealing with undiciplined users. This can be partially overcome with the syntax if "%input%" == "something"
- but this is intolerant of unbalaced quotes or some input characters.