how can i exclude printers from my batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

how can i exclude printers from my batch file

#1 Post by bramsharon » 12 Sep 2012 03:30

hi there,

with some help here on the forum i have created the following batch file

the written script under has the meaning of creating an temp file with the name of printers, creating an menu in cmd of it an then give the user an chance to choose one as default.

now i want to exclude one of the printers, like the default onenote and fax printers.

is there an possible way?

i heard the IF command but im not familiar with that command

its not that i'm lazy, but i'm building this script for a week now and it needs to be done at the end of the day.
HERE IS THE COMMAND:
======================================================================

"@echo off
set "vbsscript=printerlist.vbs"

> "%vbsscript%" echo strComputer = "."
>> "%vbsscript%" echo Set objWMIService = GetObject("winmgmts:" ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")
>> "%vbsscript%" echo Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
>> "%vbsscript%" echo num=1
>> "%vbsscript%" echo For Each objPrinter in colInstalledPrinters
>> "%vbsscript%" echo Wscript.Echo num ^& ") " ^& objPrinter.Name
>> "%vbsscript%" echo num = num + 1
>> "%vbsscript%" echo Next

cscript /nologo "%vbsscript%" >printerlist.txt

type printerlist.txt

del "%vbsscript%"
echo.
:loop
set "in="
set /p "in=Type the printer number (or just ENTER to quit): "
if not defined IN (
del printerlist.txt
goto :EOF
)
for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"
del printerlist.txt
echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: how can i exclude printers from my batch file

#2 Post by Squashman » 12 Sep 2012 12:52

So you have a temporary file being created called printerlist.txt.
That file is the list of your printers.
Parse that text file to remove the ones you don't want.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: how can i exclude printers from my batch file

#3 Post by abc0502 » 12 Sep 2012 13:49

can you post a sample of that file so we have something to work on it, and is the printer you wan't to exclude will always in
a specific line or it might change? and final question what printers you want to exclude?

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#4 Post by bramsharon » 12 Sep 2012 14:34

the script i post above is the script i use. in the file printerlist.txt there are just the name of the printers. nothing more.

i want the preinstalled printers like Fax and the one note and XPS document printer to be hidden. i prefer to keep them installed for if someone might need it in any other odd way.

i really hope you peeps could help me out as my boss gave me till friday which is 2 days more than i should have!!

i really hope you guys can help me out that would be sweet!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: how can i exclude printers from my batch file

#5 Post by abc0502 » 13 Sep 2012 02:33

what i mean with "a sample of that file" is the printer.txt file

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#6 Post by bramsharon » 13 Sep 2012 03:07

what it does is it creates two files, type and findstr without extention.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#7 Post by bramsharon » 13 Sep 2012 03:12

abc0502 wrote:what i mean with "a sample of that file" is the printer.txt file



this is the exact file
i replaced the original printer names with others for privacy reasons. its from a company:) you never know.

printerlist.txt
1) Verzenden naar OneNote 2010 (needs to be deleted)
2) original printer 1
3) Microsoft XPS Document Writer (needs to be deleted)
4) original printer 2
5) original printer 3
6) Fax (needs to be deleted)
7) original printer 4

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: how can i exclude printers from my batch file

#8 Post by abc0502 » 13 Sep 2012 04:00

is this all printer you want to exclude?
1) Verzenden naar OneNote 2010 (needs to be deleted)
3) Microsoft XPS Document Writer (needs to be deleted)
6) Fax (needs to be deleted)

and if so is it always in that order every time you run your code

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#9 Post by bramsharon » 13 Sep 2012 04:05

do you run windows? then you probably know what i mean.the order is
Fax
Microsoft XPS Document Writer
Verzenden naar OneNote 2010

those are needed to be excluded. the file that contains the printers is a temp file.
im talking about the printerlist.txt. it will be removed at the end of the script

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how can i exclude printers from my batch file

#10 Post by foxidrive » 13 Sep 2012 04:26

If you don't adjust the VBS script then you will have to alter the numbers too, for sequential viewing.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: how can i exclude printers from my batch file

#11 Post by abc0502 » 13 Sep 2012 04:37

you didn't understand me, you want to exclude "onenote, document writer & fax" from the printerlist.txt so when you assign the %printer% variable it will be set to printer1 , is that right?

so i'm asking if "onenote, document writer & fax" will always be in the same order you posted cause we will have to modify the printerlist file before you use it to get the default %printer%

put this code in your batch between

Code: Select all

set "in="
set /p "in=Type the printer number (or just ENTER to quit): "
if not defined IN (
del printerlist.txt
goto :EOF
)


and

Code: Select all

for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"
del printerlist.txt
echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"
pause


here is the code:

Code: Select all

setlocal enabledelayedexpansion

:: This will delete un-wanted printers
ren "printerlist.txt" "printerlist.tmp"
set c=0
for /F "tokens=*" %%A in ('type "printerlist.tmp"') Do (
   set /a c+=1
   IF not !c!==1 (
      IF not !c!==3 (
         IF not !c!==6 (
         echo %%A>>"printerlist_2.tmp" )))
)

:: This will fix printer numbers
set c=0
for /F "tokens=1,* delims=)" %%A in ('type "printerlist_2.tmp"') Do (
   set /a c+=1
   echo !c!^) %%B>>"printerlist.txt"
   )

Del /F /S /Q "printerlist.tmp" >nul
Del /F /S /Q "printerlist_2.tmp" >nul

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: how can i exclude printers from my batch file

#12 Post by foxidrive » 13 Sep 2012 04:52

@abc0502: the printer order will most likely change when a new printer is added.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#13 Post by bramsharon » 13 Sep 2012 04:53

foxidrive wrote:@abc0502: the printer order will most likely change when a new printer is added.


as far as i know the order of those printers wil stay the same except other printers will fill in between them. but the order of the 'excluded' ones should be the same.

bramsharon
Posts: 32
Joined: 07 Sep 2012 03:50

Re: how can i exclude printers from my batch file

#14 Post by bramsharon » 13 Sep 2012 04:54

sorry to keep bothering you, but it doesn't work, is there something to be done? what you understand is right. those printers are in that order and need to be excluded.

its harder than i tought!

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: how can i exclude printers from my batch file

#15 Post by abc0502 » 13 Sep 2012 05:02

@ Foxidrive: the code will first remove the excluded printers, then the second part will fix the first numbers from 2,4,5,7 to 1,2,3,4 and if there is more it will just keep counting and add the numbers so the printerlist.txt numbers will always in order

this is the full code including yours:

Code: Select all

@echo off
set "vbsscript=printerlist.vbs"

> "%vbsscript%" echo strComputer = "."
>> "%vbsscript%" echo Set objWMIService = GetObject("winmgmts:" ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")
>> "%vbsscript%" echo Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
>> "%vbsscript%" echo num=1
>> "%vbsscript%" echo For Each objPrinter in colInstalledPrinters
>> "%vbsscript%" echo Wscript.Echo num ^& ") " ^& objPrinter.Name
>> "%vbsscript%" echo num = num + 1
>> "%vbsscript%" echo Next

cscript /nologo "%vbsscript%" >printerlist.txt

type printerlist.txt

del "%vbsscript%"
echo.
:loop
set "in="
set /p "in=Type the printer number (or just ENTER to quit): "
if not defined IN (
del printerlist.txt
goto :EOF
)

setlocal enabledelayedexpansion
:: This will delete un-wanted printers
ren "printerlist.txt" "printerlist.tmp"
set c=0
for /F "tokens=*" %%A in ('type "printerlist.tmp"') Do (
   set /a c+=1
   IF not !c!==1 (
      IF not !c!==3 (
         IF not !c!==6 (
         echo %%A>>"printerlist_2.tmp" )))
)

:: This will fix printer numbers
set c=0
for /F "tokens=1,* delims=)" %%A in ('type "printerlist_2.tmp"') Do (
   set /a c+=1
   echo !c!^) %%B>>"printerlist.txt"
   )

Del /F /S /Q "printerlist.tmp" >nul
Del /F /S /Q "printerlist_2.tmp" >nul
Endlocal

for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"
del printerlist.txt
echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"
pause

it should work, i forgot to add the endlocal that cod be the reason why it didn't work

Post Reply