Get Default Printer-Output to temp-Restore Default printer
Moderator: DosItHelp
Get Default Printer-Output to temp-Restore Default printer
I have been spending quite a bit of time researching and building some batch files to reside on a jump drive. I have it down to a single file that ask the user many questions and is far from idiot proof. My purpose is to create a jump drive that allows the user to install a pdf printer if needed, change the default to the pdf printer and when complete restore the original default printer back to default.
I have the following file working great on win xp and win7 but am stumped on getting the original default printer into a temp file and retrieving that data at the end for restoring the original default back to default. The kicker is that the original default printer will not always be the same printer, as some of the pc's will have different printer types as their original default.
Here is the code I have working. There are a lot of baggage and test printers listed here for testing. Most systems affected will only have one or two printers installed locally. The file calls for the install of a pdf printer if needed. I need to keep the choice as initially most pc's will not have the pdf printer and the user will need to install the printer then change it to default. Ideally I would like to find a way to have the pdf printer upon install be made the default printer. Alternatively if the pc already has the pdf printer installed it would be made default when the script is ran and the original restored at the end of the script. Whew.. I am expecting hundreds of people of various technical levels to use this and am trying to make it as idiot proof as possible.
Any help would be appreciated.
@ECHO off
I would like to put the get default printer and write temp file data at the start of the process here
mode con:cols=90 lines=40
:start
ECHO .
ECHO Take note of the Default Printer the customer is using to print with!
ECHO .
ECHO MAKE Sure you change the default printer back and print a test page before you leave!!
ECHO .
ECHO .
:swap
wmic printer get name,default
ECHO The Default Printer is labeled TRUE
ECHO.
ECHO .
ECHO 1. Select Brother As Default Printer
ECHO 2. Select CutePDF Writer AS Default Printer
ECHO 3. Select PDF24 PDF as Default Printer
ECHO 4. To exit Default Printer Swap
ECHO 5. To install a PDF printer
ECHO .
set choice=
set /p choice=Type the number and hit enter to change the Default Printer.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Brother
if '%choice%'=='2' goto CutePDF
if '%choice%'=='3' goto PDF24
if '%choice%'=='4' goto EXIT
if '%choice%'=='5' goto PDFinstall
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:Brother
wmic printer where name='Brother MFC-7860DW Printer' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected Brother MFC-7860DW Printer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
GOTO swap
:CutePDF
wmic printer where name='CutePDF Writer' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected CutePDF Writer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
goto swap
:PDF24
wmic printer where name='PDF24 PDF' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected PDF24 PDF Printer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
GOTO swap
ECHO .
pause
goto end
:PDFinstall
start pdf24-creator.msi AUTOUPDATE=No DESKTOPICONS=No FAXPRINTER=No /QB+
CLS
pause
goto start
:Exit
ECHO .
ECHO Insure the correct printer is the default printer before leaving!
ECHO .
pause
I would like to put the restore original default printer code here, insuring that anytime a user exits the script the original default printer is restored to default.
:end
I have the following file working great on win xp and win7 but am stumped on getting the original default printer into a temp file and retrieving that data at the end for restoring the original default back to default. The kicker is that the original default printer will not always be the same printer, as some of the pc's will have different printer types as their original default.
Here is the code I have working. There are a lot of baggage and test printers listed here for testing. Most systems affected will only have one or two printers installed locally. The file calls for the install of a pdf printer if needed. I need to keep the choice as initially most pc's will not have the pdf printer and the user will need to install the printer then change it to default. Ideally I would like to find a way to have the pdf printer upon install be made the default printer. Alternatively if the pc already has the pdf printer installed it would be made default when the script is ran and the original restored at the end of the script. Whew.. I am expecting hundreds of people of various technical levels to use this and am trying to make it as idiot proof as possible.
Any help would be appreciated.
@ECHO off
I would like to put the get default printer and write temp file data at the start of the process here
mode con:cols=90 lines=40
:start
ECHO .
ECHO Take note of the Default Printer the customer is using to print with!
ECHO .
ECHO MAKE Sure you change the default printer back and print a test page before you leave!!
ECHO .
ECHO .
:swap
wmic printer get name,default
ECHO The Default Printer is labeled TRUE
ECHO.
ECHO .
ECHO 1. Select Brother As Default Printer
ECHO 2. Select CutePDF Writer AS Default Printer
ECHO 3. Select PDF24 PDF as Default Printer
ECHO 4. To exit Default Printer Swap
ECHO 5. To install a PDF printer
ECHO .
set choice=
set /p choice=Type the number and hit enter to change the Default Printer.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Brother
if '%choice%'=='2' goto CutePDF
if '%choice%'=='3' goto PDF24
if '%choice%'=='4' goto EXIT
if '%choice%'=='5' goto PDFinstall
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:Brother
wmic printer where name='Brother MFC-7860DW Printer' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected Brother MFC-7860DW Printer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
GOTO swap
:CutePDF
wmic printer where name='CutePDF Writer' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected CutePDF Writer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
goto swap
:PDF24
wmic printer where name='PDF24 PDF' call setdefaultprinter
CLS
ECHO You have changed the default printer!
ECHO .
ECHO You have Selected PDF24 PDF Printer as your default printer
ECHO .
Echo Make sure you change it back before you leave!
ECHO .
GOTO swap
ECHO .
pause
goto end
:PDFinstall
start pdf24-creator.msi AUTOUPDATE=No DESKTOPICONS=No FAXPRINTER=No /QB+
CLS
pause
goto start
:Exit
ECHO .
ECHO Insure the correct printer is the default printer before leaving!
ECHO .
pause
I would like to put the restore original default printer code here, insuring that anytime a user exits the script the original default printer is restored to default.
:end
Re: Get Default Printer-Output to temp-Restore Default print
It occurs to me that you will have network and local printers and I don't have a solution, but is it possible to use the batch file
to print your files directly to the installed PDF printer from the command line?
to print your files directly to the installed PDF printer from the command line?
Re: Get Default Printer-Output to temp-Restore Default print
Thanks for the reply
I will not have network printers as these are "specially isolated" pc's that only have local printers. Managing print jobs is not a goal of the batch file. Just managing the default printer/installing the pdf printer.
I was playing with the below code that returns the current default printer but I'm still a bit shaky on using the temp file for later use. I have not tested the below code on windows xp but it does return the default printer on win7x64
@ECHO OFF
ECHO.
:: Read default printer from registry and store in temporary file
REGEDIT /E %TEMP%.\DefPRN.dat "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
:: Read default printer from temporary file
FOR /F "tokens=2 delims=,=" %%A IN ('TYPE %TEMP%.\DefPRN.dat ^| FIND "Device"') DO SET DefPRN=%%A
:: Remove temporary file
DEL %TEMP%.\DefPRN.dat
:: Remove quotes from variable and display its value
SET DefPRN=%DefPRN:"=%
SET DefPRN
:: Done
pause
GOTO End
Was thinking I could modify this code to not delete the temp file
and later use
wmic printer where name== %DefPRN:"=% call setdefaultprinter
To call the original default printer... I just guessing here.. this is the part I fall down really hard on..
I will not have network printers as these are "specially isolated" pc's that only have local printers. Managing print jobs is not a goal of the batch file. Just managing the default printer/installing the pdf printer.
I was playing with the below code that returns the current default printer but I'm still a bit shaky on using the temp file for later use. I have not tested the below code on windows xp but it does return the default printer on win7x64
@ECHO OFF
ECHO.
:: Read default printer from registry and store in temporary file
REGEDIT /E %TEMP%.\DefPRN.dat "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
:: Read default printer from temporary file
FOR /F "tokens=2 delims=,=" %%A IN ('TYPE %TEMP%.\DefPRN.dat ^| FIND "Device"') DO SET DefPRN=%%A
:: Remove temporary file
DEL %TEMP%.\DefPRN.dat
:: Remove quotes from variable and display its value
SET DefPRN=%DefPRN:"=%
SET DefPRN
:: Done
pause
GOTO End
Was thinking I could modify this code to not delete the temp file
and later use
wmic printer where name== %DefPRN:"=% call setdefaultprinter
To call the original default printer... I just guessing here.. this is the part I fall down really hard on..
Re: Get Default Printer-Output to temp-Restore Default print
This works on my system to get the default printer. It should work on XP Pro and higher.
Code: Select all
@echo off
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "defprint=%%b"
)
echo "%defprint%"
pause
Re: Get Default Printer-Output to temp-Restore Default print
That's a lot cleaner method than using the registry and it works great to display the default printer on my test pc's.
Do you know how to output that to a temp file or set it as a var for later use? Such as TYPE %TEMP%.\defprint
Would like to call that with something like this
wmic printer where name=%defprint=%%b"call setdefaultprinter
or
wmic printer where name=%TEMP%.\DefPRN.dat" call setdefaultprinter
With follow up to NOT delete the temp file in case of problems. This would have the script always overwriting the old temp file but that's fine as long as they don't accumulate.....
This is all very new to me and any help would be appreciated.
Don't really care what the temp file is called. Would be nice if its put in the same location as the bat file is run from though.
Do you know how to output that to a temp file or set it as a var for later use? Such as TYPE %TEMP%.\defprint
Would like to call that with something like this
wmic printer where name=%defprint=%%b"call setdefaultprinter
or
wmic printer where name=%TEMP%.\DefPRN.dat" call setdefaultprinter
With follow up to NOT delete the temp file in case of problems. This would have the script always overwriting the old temp file but that's fine as long as they don't accumulate.....
This is all very new to me and any help would be appreciated.
Don't really care what the temp file is called. Would be nice if its put in the same location as the bat file is run from though.
Re: Get Default Printer-Output to temp-Restore Default print
Got this code working!
It gets the original default printer and sets it as defprint
displays the default printer as defprint
sets default to PDF24 PDF
set default printer back to defprint
I put the pauses in for testing/verification
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "defprint=%%b"
)
ECHO Your current default printer is "%defprint%"
pause
wmic printer where name='PDF24 PDF' call setdefaultprinter
pause
wmic printer where name='%defprint%' call setdefaultprinter
pause
My last challenge is to get this WMIC script to work
wmic printer where name='PDF24 PDF' call setdefaultprinter
IF !ERRORLEVEL! equ 1 GOTO PDFinstall
I have found some references to get around the weird errorlevel behavior of WMIC
Any help would be appreciated.
It gets the original default printer and sets it as defprint
displays the default printer as defprint
sets default to PDF24 PDF
set default printer back to defprint
I put the pauses in for testing/verification
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "defprint=%%b"
)
ECHO Your current default printer is "%defprint%"
pause
wmic printer where name='PDF24 PDF' call setdefaultprinter
pause
wmic printer where name='%defprint%' call setdefaultprinter
pause
My last challenge is to get this WMIC script to work
wmic printer where name='PDF24 PDF' call setdefaultprinter
IF !ERRORLEVEL! equ 1 GOTO PDFinstall
I have found some references to get around the weird errorlevel behavior of WMIC
Any help would be appreciated.
Re: Get Default Printer-Output to temp-Restore Default print
So this batch file is coming together nice and slow.
I think I am down to my last challenge...
I have successfully used the set "PDFPrinter=%%b" to display with echo the default printer as well as allow me to retrieve the original default printer like so.. wmic printer where name='%defprint%' call setdefaultprinter. This success is outlined in my previous post.
I couldn't get the errorlevel tactic to work from the previous post so I though I would go with an if statement.
Now I just need to get the if statement working.
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
echo The New Default Printer is "%PDFPrinter%" This displays on screen "PDF24 PDF"
cls
ECHO .
IF PDFPrinter==PDF24 PDF ( This statement goes to Syntax2 whether it true or false. I have tried a bunch of alternatives here to no avail. Does anyone see the problem with this if statement? I definitely have tried IF PDFPrinter=="PDF24 PDF" and that doesn't work either.
goto Syntax2
) ELSE (
goto Syntax
)
I think I am down to my last challenge...
I have successfully used the set "PDFPrinter=%%b" to display with echo the default printer as well as allow me to retrieve the original default printer like so.. wmic printer where name='%defprint%' call setdefaultprinter. This success is outlined in my previous post.
I couldn't get the errorlevel tactic to work from the previous post so I though I would go with an if statement.
Now I just need to get the if statement working.
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
echo The New Default Printer is "%PDFPrinter%" This displays on screen "PDF24 PDF"
cls
ECHO .
IF PDFPrinter==PDF24 PDF ( This statement goes to Syntax2 whether it true or false. I have tried a bunch of alternatives here to no avail. Does anyone see the problem with this if statement? I definitely have tried IF PDFPrinter=="PDF24 PDF" and that doesn't work either.
goto Syntax2
) ELSE (
goto Syntax
)
Re: Get Default Printer-Output to temp-Restore Default print
Your variable needs percent symbols and you should enclose both values in quotes.
Re: Get Default Printer-Output to temp-Restore Default print
That did it. Thanks man. After my burnt out brain cools off I'll put the whole script together and post it.
From
IF PDFPrinter==PDF24 PDF
to
IF "%PDFPrinter%"=="PDF24 PDF"
Was wondering if maybe whittling would be a less frustrating hobby...
From
IF PDFPrinter==PDF24 PDF
to
IF "%PDFPrinter%"=="PDF24 PDF"
Was wondering if maybe whittling would be a less frustrating hobby...
Re: Get Default Printer-Output to temp-Restore Default print
Works Great. Nice fairly automated little pdf printer installer/swapper/restorer. Give it a whirl. I intend on running it from a jump drive so I don't have to print a bunch of work documents just to scan them and email them later (with a massive decrease in file sizes to boot).
You will need pdf24-creator.msi from
http://en.pdf24.org/pdf-creator-download.html
Hope this helps someone like the many online forums I have benefitted from over the years. Got snippets for this from all over so if it looks familiar.. thanks :)
Enjoy
@ECHO off
mode con:cols=90 lines=40
ECHO Welcome to PDF Printer Magic
ECHO .
Pause
:start
cls
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "defprint=%%b"
)
IF "%defprint%"=="PDF24 PDF" (
goto choose
)
cls
wmic printer where name='PDF24 PDF' call setdefaultprinter
cls
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
cls
IF "%PDFPrinter%"=="PDF24 PDF" (
goto Syntax2
) ELSE (
goto PDFinstall
)
cls
:Syntax2
ECHO.
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
cls
ECHO PDF Printer Magic found an installed PDF Printer!
ECHO .
ECHO Your current default printer has been changed to "%PDFPrinter%"
ECHO .
ECHO DO NOT close this DOS window.
ECHO .
ECHO Follow ALL prompts to restore original default and exit program when ready
ECHO .
ECHO Make sure to print a paper test page from the machine interface
ECHO after exiting this program VERY IMPORTANT!
ECHO .
ECHO Switch back to the machine Software and print your documents to the PDF Printer NOW
ECHO ALT+TAB works well for this.
ECHO .
ECHO When your finished printing your PDF files to the jump drive.
ECHO Use ALT+Tab to switch back to PDF Printer Magic and...
ECHO .
pause
GOTO exit
:PDFinstall
start pdf24-creator.msi AUTOUPDATE=No DESKTOPICONS=No FAXPRINTER=No /QB+
CLS
ECHO PDF Printer Magic tried to swap the default to the preffered PDF Printer but you dont have it installed!
ECHO .
ECHO PDF Printer Magic is installing preffered PDF Printer....
ECHO .
ECHO When install is complete click ok and then.
pause
wmic printer where name='PDF24 PDF' call setdefaultprinter
goto syntax2
:choose
Echo PDF Printer Magic detected a PDF Printer as your default printer
Echo .
Echo Please select the proper printer for paper printing on this machine
Echo .
Echo Or reselect your PDF Printer to continue sending
ECHO ALL print jobs to the PDF Printer (NOT RECOMMENDED)
Echo .
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 .
cls
echo Default Printer is now "%printer%"
:chooseagain
Echo .
Echo Choose 1 to rerun PDF Printer Magic
Echo .
Echo Choose 2 to exit PDF Printer Magic
Echo .
set choice=
Echo .
set /p choice=What would you like to do?
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Start
if '%choice%'=='2' goto End
:exit
wmic printer where name='%defprint%' call setdefaultprinter
cls
echo .
echo Default Printer is now "%defprint%"
echo .
echo If this is the wrong printer for paper printing just rerun PDF Printer Magic
echo and select the correct printer.
echo TO Exit
pause
:end
You will need pdf24-creator.msi from
http://en.pdf24.org/pdf-creator-download.html
Hope this helps someone like the many online forums I have benefitted from over the years. Got snippets for this from all over so if it looks familiar.. thanks :)
Enjoy
@ECHO off
mode con:cols=90 lines=40
ECHO Welcome to PDF Printer Magic
ECHO .
Pause
:start
cls
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "defprint=%%b"
)
IF "%defprint%"=="PDF24 PDF" (
goto choose
)
cls
wmic printer where name='PDF24 PDF' call setdefaultprinter
cls
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
cls
IF "%PDFPrinter%"=="PDF24 PDF" (
goto Syntax2
) ELSE (
goto PDFinstall
)
cls
:Syntax2
ECHO.
for /f "tokens=2,* delims=," %%a in ('"wmic printer get name, default /format:csv |find "^,TRUE^,""') do (
set "PDFPrinter=%%b"
)
cls
ECHO PDF Printer Magic found an installed PDF Printer!
ECHO .
ECHO Your current default printer has been changed to "%PDFPrinter%"
ECHO .
ECHO DO NOT close this DOS window.
ECHO .
ECHO Follow ALL prompts to restore original default and exit program when ready
ECHO .
ECHO Make sure to print a paper test page from the machine interface
ECHO after exiting this program VERY IMPORTANT!
ECHO .
ECHO Switch back to the machine Software and print your documents to the PDF Printer NOW
ECHO ALT+TAB works well for this.
ECHO .
ECHO When your finished printing your PDF files to the jump drive.
ECHO Use ALT+Tab to switch back to PDF Printer Magic and...
ECHO .
pause
GOTO exit
:PDFinstall
start pdf24-creator.msi AUTOUPDATE=No DESKTOPICONS=No FAXPRINTER=No /QB+
CLS
ECHO PDF Printer Magic tried to swap the default to the preffered PDF Printer but you dont have it installed!
ECHO .
ECHO PDF Printer Magic is installing preffered PDF Printer....
ECHO .
ECHO When install is complete click ok and then.
pause
wmic printer where name='PDF24 PDF' call setdefaultprinter
goto syntax2
:choose
Echo PDF Printer Magic detected a PDF Printer as your default printer
Echo .
Echo Please select the proper printer for paper printing on this machine
Echo .
Echo Or reselect your PDF Printer to continue sending
ECHO ALL print jobs to the PDF Printer (NOT RECOMMENDED)
Echo .
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 .
cls
echo Default Printer is now "%printer%"
:chooseagain
Echo .
Echo Choose 1 to rerun PDF Printer Magic
Echo .
Echo Choose 2 to exit PDF Printer Magic
Echo .
set choice=
Echo .
set /p choice=What would you like to do?
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto Start
if '%choice%'=='2' goto End
:exit
wmic printer where name='%defprint%' call setdefaultprinter
cls
echo .
echo Default Printer is now "%defprint%"
echo .
echo If this is the wrong printer for paper printing just rerun PDF Printer Magic
echo and select the correct printer.
echo TO Exit
pause
:end