how can i exclude printers from my batch file
Moderator: DosItHelp
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
how can i exclude printers from my batch file
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
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
Re: how can i exclude printers from my batch file
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.
That file is the list of your printers.
Parse that text file to remove the ones you don't want.
Re: how can i exclude printers from my batch file
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?
a specific line or it might change? and final question what printers you want to exclude?
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
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!
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!
Re: how can i exclude printers from my batch file
what i mean with "a sample of that file" is the printer.txt file
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
what it does is it creates two files, type and findstr without extention.
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
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
Re: how can i exclude printers from my batch file
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
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
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
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
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
Re: how can i exclude printers from my batch file
If you don't adjust the VBS script then you will have to alter the numbers too, for sequential viewing.
Re: how can i exclude printers from my batch file
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
and
here is the code:
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
Re: how can i exclude printers from my batch file
@abc0502: the printer order will most likely change when a new printer is added.
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
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.
-
- Posts: 32
- Joined: 07 Sep 2012 03:50
Re: how can i exclude printers from my batch file
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!
its harder than i tought!
Re: how can i exclude printers from my batch file
@ 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:
it should work, i forgot to add the endlocal that cod be the reason why it didn't work
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