#1 08 Nov 2016 20:53

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Store Multiple results from Findstr

I am trying to create a script that will run a query to see if a specific IP printer exists and show the name of the printer. However when I try to get the output from it, I am unable to get the 2 results to 2 different variables. This is what I have so far:

SETLOCAL ENABLEDELAYEDEXPANSION
SET LIST=192.168.1.101 192.168.1.102
SET LOG="C:\Applications\Printer status.txt"

FOR %%a IN (%LIST%) DO FOR /F "TOKENS=2* DELIMS==" %%b in ('WMIC PRINTER WHERE "PortName LIKE '%%%%a%%'" LIST /FORMAT:LIST^|FINDSTR /B /C:"DeviceID" /C:"PortName" /I') DO (SET OUTPUT=%%b
SET %%c=OUTPUT2
ECHO Found Printer: !OUTPUT! mapped to IP: !OUTPUT2! >> %LOG%)

When I check the log I see this:

Found Printer: Lexmark Universal Driver mapped to IP:  
Found Printer: 192.168.1.101 mapped to IP:  

I'm trying to get it to output like:

Found Printer: Lexmark Universal Driver mapped to IP: 192.168.1.101

I'm not sure how to get the 2 results to separate variables. I'm trying to do this without outputting to a text file. Anyone know how to get both results where I don't have to query the results separately? If I have to I will. Thanks.

Last edited by ImAhNoBoDy (10 Nov 2016 03:33)

Offline

#2 09 Nov 2016 20:15

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Re: Store Multiple results from Findstr

So I was able to figure this part out using this site: http://code.activestate.com/recipes/578 … nto-batch/

SETLOCAL ENABLEDELAYEDEXPANSION
SET LIST=192.168.1.101 192.168.1.102"
SET LOG="C:\Applications\Printer status.txt"

    ::INCREMENTAL VARIABLE
    SET "i=0"
    ::STORE FILENAMES INTO AN ARRAY
    FOR %%a IN (%LIST%) DO FOR /F "TOKENS=2 DELIMS==" %%b in ('WMIC PRINTER WHERE "PortName LIKE '%%%%a%%'" LIST /FORMAT:LIST^|FINDSTR /B /I "DeviceID PortName"') do (SET ARR[!i!]=%%b & SET /A "i+=1")

 ::DISPLAY ALL ARRAY ITEMS
    SET ARR
    IF %ERRORLEVEL% EQU 1 GOTO NOPRINTERS
    echo.===================================
    ::PRINT ARRAY ITEMS (FROM 0 TILL N)
    SET "len=!i!"
    set "i=0"
    :LOOP
    ECHO|SET /P="Found Printer: !arr[%i%]! "
    ECHO|SET /P="Found Printer: !arr[%i%]! " >> %LOG% & set /a "i+=1"
    ECHO with IP address: !arr[%i%]!
    ECHO with IP address: !arr[%i%]! >> %LOG%
    FOR %%c in (!arr[%i%]!) DO (WMIC PRINTER WHERE "PortName LIKE '%%%%c%%'" delete)
    SET /A "i+=1"
    IF %i% NEQ %len% GOTO :LOOP

:NOPRINTERS

ECHO No printers to delete

Now I'm trying to do a list of printer ports using prnports.vbs to delete all specific IP Addresses from the %LIST% variable and including any duplicates. The duplicates look like 192.168.1.101_[x]. Where x is a number. Anyone know a way?

Last edited by ImAhNoBoDy (10 Nov 2016 03:35)

Offline

#3 10 Nov 2016 18:40

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Re: Store Multiple results from Findstr

I ended up using WMI instead of prnport.vbs

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET LIST=192.168.1.101 192.168.1.102
SET LOG="C:\Applications\Printer status.txt"

::::::DELETES PRINTERS::::::
    ::INCREMENTAL VARIABLE
    SET "i=0"
    ::STORE FILENAMES INTO ARRAY
    FOR %%a IN (%LIST%) DO FOR /F "TOKENS=2 DELIMS==" %%b in ('WMIC PRINTER WHERE "PortName LIKE '%%%%a%%'" LIST /FORMAT:LIST^|FINDSTR /B /I "DeviceID PortName"') DO (
      SET arr[!i!]=%%b & SET /A "i+=1"
    )

    ::DISPLAY ALL ARRAY ITEMS
    SET arr
    IF %ERRORLEVEL% EQU 1 GOTO NOPRINTER
    ECHO.===================================
    ::PRINT ARRAY ITEMS (FROM 0 TILL N)
    SET "len=!i!"
    SET "i=0"
    :LOOP1
    ECHO|SET /P="Found Printer: !arr[%i%]! " & set /a "i+=1"
    ECHO with IP address: !arr[%i%]!
    FOR %%c IN (!arr[%i%]!) DO (WMIC PRINTER WHERE "PortName LIKE '%%%%c%%'" delete)
    SET /A "i+=1"
    IF %i% NEQ %len% GOTO LOOP1

:NOPRINTER

ECHO No printers to delete

::::::DELETES PRINTER PORTS::::::

    ::INCREMENTAL VARIABLE
    SET "i=0"
    ::STORE FILENAMES INTO ARRAY
    FOR /F "TOKENS=2 DELIMS==" %%d IN ('WMIC PATH WIN32_TCPIPPRINTERPORT GET /FORMAT:LIST^|FINDSTR /B /I /C:"Name"') DO (
      SET arr[!i!]=%%d & SET /A "i+=1"
    )

    ::DISPLAY ALL ARRAY ITEMS
    SET arr
    IF !ERRORLEVEL! EQU 1 GOTO NOPORTS
    ECHO.===================================
    ::PRINT ARRAY ITEMS (FROM 0 TILL N)
    SET "len=!i!"
    SET "i=0"
    :LOOP2
    FOR %%e IN (%LIST%) DO FOR /F "TOKENS=1 DELIMS=_ " %%f IN ('ECHO !arr[%i%]!') DO (IF %%e EQU %%f (WMIC PATH WIN32_TCPIPPRINTERPORT WHERE "Name LIKE '%%%%e%%'" DELETE
	ECHO !arr[%i%]! Deleted))
    SET /A "i+=1"
    IF %i% NEQ %len% GOTO LOOP2

:NOPORTS

ECHO No ports to delete

NET STOP SPOOLER
TIMEOUT /T 3 /NOBREAK
NET START SPOOLER
:EXIT

Last edited by ImAhNoBoDy (10 Nov 2016 19:13)

Offline

Board footer

Powered by FluxBB