Getting Printers and Addresses from Registry - String Manipulation in CMD or VBScript
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers | findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)">a.txt
FOR /f "usebackq delims=}" %p IN (`reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers ^| findstr /i "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)"`) DO reg query "%p\DsSpooler" /v portName | findstr "portName">>a.txt
I can get the following output file, a.txt:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\HP Color LaserJet CP5220 Series PCL6 portName REG_MULTI_SZ 18.104.22.168
Now, what I need to do, is look in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\ and pull the HostName or IPAddress keys (just in case some creative tech made the name different from the actual address), but my problem is (I'm embarased to admit) manipulating the output to get JUST a) the name of the printer ("HP Color LaserJet CP5220 Series PCL6") and b) its portName (22.214.171.124) into a usable format so I can plug it back into reg.exe and get the actual address. Any help? I know this would be easier with VBS but its been forever and a day since I used VB; I considered PowerShell but I dont know if its built into the KBE. If you can forgive my archaic methods I would be forever grateful.
REM Skip the DefaultSpoolDirectory and LANGIDOfLastDefaultDevmode lines
FOR /F "SKIP=5 TOKENS=*" %A IN ('REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers') DO (CALL :PRINTERINFO %A)
REG QUERY "%*" | FINDSTR /I "\hp \dell \canon \epson \lexmark \xerox \laserjet \forms \room \office \(color printer) \copier \(copy machine)" > nul
IF "%ERRORLEVEL%" EQU "0" (
FOR /F "SKIP=1 TOKENS=7 DELIMS=\" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)
FOR /F "SKIP=2 TOKENS=3" %A IN ('REG QUERY "%*\DsSpooler" /v portName') DO (ECHO %A>> a.TXT)
To simplify the string manipulation, I'd first use Split with 'portName' as the separator, then use Split again on the first element of the returned array but this time using '\' as the separator. However, it would be a lot easier to use the registry class which I have posted a quadzillion times here on ITNinja and just get the values you want from the registry, using a For Each loop to...errrr...loop through the 'Printers' key. Actually, there's a function in that class that will retrieve the value names and the associated data and put it into an array.