China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 15:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch processing to batch add printers (including print servers) View 3,152 Replies 7
Original Poster Posted 2008-12-15 14:37 ·  中国 广东 深圳 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
Purpose:
Batch - add printers according to the printer configuration file. For example, a computer needs to install a local parallel port printer and a network print server at the same time (not sharing the printer of other computers, but the printer that can be connected to the switch with a network cable).

Implementation steps:
1. Poll the "Printer X.txt" in the directory. According to the port description in it, judge whether it is a local LPT port. If it is a local LPT printer, ignore it. If it is a print server, automatically create a new TCP/IP port.
2. Restart the print service to make the newly created local IP port take effect.
3. Poll the "Printer X.txt" in the directory. According to the printer name, driver location, printer model and printer port, add the print server.

Key statement:
rundll32 printui.dll,PrintUIEntry /?

Defect:
Because I don't need local printers with USB ports and don't use shared printers, there are no statements added to judge such printers.

★★★★★★★★★
The following is the batch code

@echo off
cls


rem The following is to judge the port
set count=0
:addreginfo
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto eof)
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr /I "打印机IP="') do set pIP=%%a
if /I "%pIP%"=="LPT1:" goto addreginfo
if /I "%pIP%"=="LPT2:" goto addreginfo


rem The following is to add the information in the registry about the port
set regpath1=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
set regpath2=HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\IP_
:addreg1
@reg add "%regpath1%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath1%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
:addreg2
@reg add "%regpath2%%pip%" /v "Protocol" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "Version" /d "00000001" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "HostName" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "IPAddress" /d "%pip%" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "HWAddress" /d "" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "PortNumber" /d "9100" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Community" /d "public" /f >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Enabled" /d "00000000" /f /t REG_DWORD >nul 2>nul
@reg add "%regpath2%%pip%" /v "SNMP Index" /d "00000001" /f /t REG_DWORD >nul 2>nul
goto addreginfo
:eof


rem The following is to restart the print service to make the new port take effect
:restartservice
cls
echo.
echo Please wait, enabling the printer port!
echo.
net stop spooler >nul 2>nul
net start spooler >nul 2>nul


rem The following is to add the printer
set count=0
:instprinter
set /a count=%count%+1
if exist 打印机%count%.txt (set filename=打印机%count%.txt) else (goto over)
cls
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机IP="') do set pIP=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机名称="') do set pname=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机型号="') do set pmodel=%%a
for /f "tokens=2 delims==" %%a in ('type %filename% ^|findstr "打印机INF位置="') do set pinf=%%a
echo.
echo Please wait, adding printer %count%, the relevant information is as follows:
echo.
echo IP: %pIP%
echo Name: %pname%
echo Model: %pmodel%
echo Driver: %pinf%
if /I "%pIP%"=="LPT1:" (goto ok)
if /I "%pIP%"=="LPT2:" (goto ok) else (set pip=IP_%pip%)
:ok
rundll32 printui.dll,PrintUIEntry /if /b "%pname%" /f "%pinf%" /r "%pIP%" /m "%pmodel%"
goto instprinter


:over
cls
echo.
echo Printer addition is complete!
pause
exit


★★★★★★★★★
The content of the network print server configuration file "Printer 1.txt":

Printer IP=44.44.44.44
Printer Name=Konica 220 Print Server
Printer Model=KONICA MINOLTA 220
Printer INF Location=D:\sysbak\Printers\220 Drivers\TSK.inf


★★★★★★★★★
The content of the local printer configuration file "Printer 2.txt":

Printer IP=LPT1:
Printer Name=Local HP 3050
Printer Model=HP LaserJet 3050 Series PCL 5e
Printer INF Location=D:\sysbak\Printers\hp3055\Drivers\2K_XP_Vista\hppcp501.inf


[ Last edited by exzzz on 2008-12-15 at 14:46 ]
Attachments
添加网络打印机0302.zip (2.09 KiB, Downloads: 112)
Floor 2 Posted 2008-12-15 14:41 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
It is suggested to change the first line to @echo off, and delete all other @
Floor 3 Posted 2008-12-15 14:45 ·  中国 广东 深圳 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
Thanks, moderator, for your concern!

I took a little shortcut, heh heh. The part with @..... was copied from my other batch script, and I was too lazy to replace it. After echo off, I'm used to cls, a bad habit I formed years ago that I still can't get rid of...
Floor 4 Posted 2009-03-18 06:59 ·  中国 北京 联通
新手上路
Credits 4
Posts 3
Joined 2006-02-19 12:09
20-year member
UID 50617
Status Offline
What about adding a USB - connected printer? How should it be written?
Floor 5 Posted 2009-03-19 01:40 ·  中国 广东 深圳 电信
初级用户
★★
游手好闲 + 无所事事 ..
Credits 194
Posts 167
Joined 2007-04-30 09:43
19-year member
UID 87022
Gender Male
Status Offline
I haven't installed USB before, but you can first install it manually once. Check what characters are there in the port properties of the printer. I remember it should be something like USB01: or the like. You can modify the printer IP in the txt file and that should work.

And according to the characteristics of USB devices, you should be able to install it on any port randomly, such as network or LPT, then uninstall it after installation. Then when you restart, it will be automatically installed in the background.
Floor 6 Posted 2009-03-19 09:59 ·  中国 北京 263网络通信电信数据中心
新手上路
Credits 4
Posts 3
Joined 2006-02-19 12:09
20-year member
UID 50617
Status Offline
Originally posted by exzzz at 2009-3-19 01:40:
I haven't installed USB ones, but you can first install manually once, check what characters are in the printer port properties, remember it should be something like USB01: and so on, modify the printer IP in the txt file...


USB001

But it doesn't work.

I tried it

When installing to the end, it appears that the printer cannot be installed

I don't know the reason

It seems that "virtual printer" is also a new port ah,

Why does USB have to be connected to the printer to work?
Floor 7 Posted 2010-06-27 11:37 ·  中国 浙江 温州 电信
初级用户
★★
Credits 170
Posts 83
Joined 2007-11-30 16:44
18-year member
UID 104263
Gender Male
Status Offline
Because the USB ports are generated randomly, and a USB port is created only when a USB printer is connected.

In Windows 2000 and XP systems, USB interface devices are all regarded as plug-and-play devices, so USB printers will not have a specifically pre-designated port in the list.
Only after connecting the printer will it be randomly assigned according to system interrupts, ports and other issues. So if your printer is a USB port, after you plug in the printer and turn on the printer power, the system will automatically search and install.
Floor 8 Posted 2010-07-26 17:46 ·  中国 福建 厦门 电信
新手上路
Credits 1
Posts 1
Joined 2010-07-24 09:46
16-year member
UID 171100
Gender Male
Status Offline
This simply cannot achieve the functions you mentioned. Ports cannot be added, let alone installing a printer.
Forum Jump: