none
Is there an error with my findstr? RRS feed

  • Question

  • I have a txt file listing the ports that have printers plugged in on my computer and a findstr to look it up and set one or the other as default printers based upon a string of caracters is found or not in the txt file. The command for setting the default printers are working fine outside of the if clause, but, aparently the findstr is unable to find the string. What am I missing here?

    Everytime I run it, it sets the SamsungM printer as the default one, because the ERRORLEVEL is always set to 1 after running the findstr. Prior to it, it was set to 0.

    The code:

    findstr /I "usb001" ports.txt
    if %ERRORLEVEL% == 0 (wmic printer where portname="USB001" call setdefaultprinter) else (wmic printer where "PortName like '%%samsungm'" call setdefaultprinter)

    The txt file:

    PortName              
    USB001                
    \\ses267827\samsungm  
    172.22.41.49          


    • Edited by OrleyLima Monday, March 9, 2020 4:28 PM
    Monday, March 9, 2020 4:22 PM

Answers

  • Problem solved!

    Aparently the findstr has difficulties with certain encodings. The cmd exports txt files with UTF-16LE that can't be read with findstr.

    Worked around it pipeing the wmic command and not saving it to a txt file anymore. The new code looks like this:

    wmic printer get portname | findstr /b /i "usb001"
    if %ERRORLEVEL% == 0 (wmic printer where portname="USB001" call setdefaultprinter) else (wmic printer where "PortName like '%%samsungm'" call setdefaultprinter)

    • Marked as answer by OrleyLima Monday, March 9, 2020 7:33 PM
    Monday, March 9, 2020 7:33 PM