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

Re: how can i exclude printers from my batch file

#16 Post by bramsharon » 13 Sep 2012 05:10

no sorry it doesnt work. the list is still the same and when i choose an printer it gives me an error loop to fast to read.

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

Re: how can i exclude printers from my batch file

#17 Post by abc0502 » 13 Sep 2012 05:19

ok, foxidrive was right i didn't understand what he said till i tested it, we should first get the printerlist, modify it, then display what is in that file and then choose the printer, i will post the code in few minutes.
Last edited by abc0502 on 13 Sep 2012 05:32, edited 1 time in total.

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

Re: how can i exclude printers from my batch file

#18 Post by bramsharon » 13 Sep 2012 05:21

you're to kind! really helpful and lots of patience.

i will thank you for that!

i wan't to learn this so badly but it aint easy if its new for you!

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

Re: how can i exclude printers from my batch file

#19 Post by abc0502 » 13 Sep 2012 05:30

try it now , i changed the order of the commands but all command the same

Code: Select all

@echo off

:: generate Vbscript and call it to get the printer list, then delete it
set "vbsscript=printerlist.vbs"
Call :vb
cscript /nologo "%vbsscript%" >printerlist.txt
Del /F /S /Q "%vbsscript%" >nul

:: now we remove unwanted printers and fix the number order
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"
   )
Endlocal

:: delete temp files
Del /F /S /Q "printerlist.tmp" >nul
Del /F /S /Q "printerlist_2.tmp" >nul

:: now display the new printer list after modification, so the user get to choose
type printerlist.txt
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
)

:: set the default printer
for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"

echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"

pause
Exit /B

:vb
> "%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

if you want the old order i can post it to you or pm you with it

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

Re: how can i exclude printers from my batch file

#20 Post by foxidrive » 13 Sep 2012 05:36

Your code will break when you add new printers, or use it on another machine.

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

Re: how can i exclude printers from my batch file

#21 Post by bramsharon » 13 Sep 2012 05:40

dude you are wonderfull! it works! thanks so much man, you guys really know how to help people. kudo's for you!

i will throw this script into my full one and let you know how that turns out!

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

Re: how can i exclude printers from my batch file

#22 Post by abc0502 » 13 Sep 2012 05:43

bramsharon wrote:dude you are wonderfull! it works! thanks so much man, you guys really know how to help people. kudo's for you!

i will throw this script into my full one and let you know how that turns out!

you better consider Foxidrive note
Foxidrive wrote:Your code will break when you add new printers, or use it on another machine.

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

Re: how can i exclude printers from my batch file

#23 Post by bramsharon » 13 Sep 2012 05:47

darn it, i think you guys helped me enough, i am very pleased on how you guys helped me out. i think i just talk with my boss that its best to just keep that list. its not that it gets much more crowed because of that. perhaps he thinks its okay this way.

thanks a lot people! you guys helped me out alot!

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

Re: how can i exclude printers from my batch file

#24 Post by foxidrive » 13 Sep 2012 05:50

It seems that the printers aren't in alphabetic order, and not even in order of installation. The fax driver in mine should be a default on installation and here it is listed last.

1) Snagit 10
2) Samsung ML-1740 Series
3) Microsoft XPS Document Writer
4) Fax


So it is entirely machine dependent.

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

Re: how can i exclude printers from my batch file

#25 Post by abc0502 » 13 Sep 2012 05:51

yes just as you said, but in any way you always choose the printer you want to be the default so there is no need to remove these 3 printers, just get the list and then select the one you want

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

Re: how can i exclude printers from my batch file

#26 Post by bramsharon » 13 Sep 2012 05:53

yes i will leave it that way, it works to but my boss would like it more if those printers were 'invincible' but i already thought that would be an impossible job.

thanks again guys! you were very helpfull and patient!

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

Re: how can i exclude printers from my batch file

#27 Post by abc0502 » 13 Sep 2012 06:04

Here is a fix for the problem, we won't use line number but will use the names of the 3 printers

Code: Select all

@echo off

:: generate Vbscript and call it to get the printer list, then delete it
set "vbsscript=printerlist.vbs"
Call :vb
cscript /nologo "%vbsscript%" >printerlist.txt
Del /F /S /Q "%vbsscript%" >nul

:: now we remove unwanted printers and fix the number order
setlocal enabledelayedexpansion
:: This will delete un-wanted printers
ren "printerlist.txt" "printerlist.tmp"

For /F "tokens=*" %%a in ('find /I /v "OneNote" ^<"printerlist.tmp"') Do echo %%a>>"temp1.txt"
For /F "tokens=*" %%a in ('find /I /v "Fax" ^<"temp1.txt"') Do echo %%a>>"temp2.txt"
For /F "tokens=*" %%a in ('find /I /v "Writer" ^<"temp2.txt"') Do 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"
   )
Endlocal

:: delete temp files
Del /F /S /Q "printerlist.tmp" >nul
Del /F /S /Q "printerlist_2.tmp" >nul
Del /F /S /Q "temp1.txt" >nul
Del /F /S /Q "temp2.txt" >nul

:: now display the new printer list after modification, so the user get to choose
type printerlist.txt
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
)

:: set the default printer
for /f "tokens=1,*" %%a in ('findstr /r "^%in%)" ^< "printerlist.txt"') do set "printer=%%b"

echo.
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "%printer%"
echo Default Printer is now "%printer%"

pause
Exit /B

:vb
> "%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

Instead of this code:

Code: Select all

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" )))
)

we use this

Code: Select all

For /F "tokens=*" %%a in ('find /I /v "OneNote" ^<"printerlist.tmp"') Do echo %%a>>"temp1.txt"
For /F "tokens=*" %%a in ('find /I /v "Fax" ^<"temp1.txt"') Do echo %%a>>"temp2.txt"
For /F "tokens=*" %%a in ('find /I /v "Writer" ^<"temp2.txt"') Do echo %%a>>"printerlist_2.tmp"

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

Re: how can i exclude printers from my batch file

#28 Post by bramsharon » 13 Sep 2012 06:18

sorry to say but it aint fixed, but its fine i'll deal it with my boss it will be fine was just curious if it was possible, i don't want to make a new script for every pc as whe have 400 pc's

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

Re: how can i exclude printers from my batch file

#29 Post by abc0502 » 13 Sep 2012 07:20

are you sure, i tested on Foxidrive printer list, billrich list and your list and it always get the printerlist.txt file with the 3 printers removed and numbers fixed.

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

Re: how can i exclude printers from my batch file

#30 Post by foxidrive » 13 Sep 2012 08:35

It works here.

Post Reply