Solved

If Statement to check if printer is installed

Posted on 2008-10-02
23
1,627 Views
Last Modified: 2010-04-21
I am after a IF statement to check if a printer is installed i have got a script working so each floor will install a printer dependent on ur ip range, but this is slow during the log on script so wondered if i can do a if statement to check to see if  a printer is installed and if its installed go to end.

hopefully you get what i am tryin to achieve
0
Comment
Question by:rgoddard2008
  • 12
  • 11
23 Comments
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
If you are talking batch then you could drop a flag file in the users profile perhaps, not fool proof if the user decides to delete mind:

@echo off
rem Rest of login script
mkdir "%userprofile%\printers" >NUl 2>&1
call :setupprinter printer1
call :setupprinter printer2
goto :eof
:setupprinter
 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1" & echo
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"



0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
And without the typo:

@echo off
rem Rest of login script
mkdir "%userprofile%\printers" >NUl 2>&1
call :setupprinter printer1
call :setupprinter printer2
goto :eof
:setupprinter
 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1"
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"
0
 

Author Comment

by:rgoddard2008
Comment Utility
can you explain the script...

currently i use a script that opens each printer queue which installs the prnter, i want the script to check if that printer say .. acc_print1 is installed and if it isnt... installs it
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Ok.  This one does:

1. Creates a directory in the user's profile directrory called "printers" if it doesn't exist, e.g. c:\documents and settings\sknight\printers

2. calls a subroutine for each printer to install and passed it the printer name.  If servers are different for different printers we could do a similar but different way.

3. In the subroutine it first checks if a printer name has been specified and if not finishes the subroutine (goto :eof).  It then checked if there is a file called the same as the printer, i.e. printer1.txt in the users profile.  If there is then it stops the subroutine.  If not it runs the START command to install that printer.

How are you doing it at the moment, similarly or a VB Script etc?

Steve
0
 

Author Comment

by:rgoddard2008
Comment Utility
i am just doing it as a batch file which opens the queue....


so for example this

call :setupprinter printer1
call :setupprinter printer2

the call:setupprinter printer1 = accounts_printer

i think i get that part, its the next part i am having trouble with.

goto :eof
:setupprinter
 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1"
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"

also, printers are on a different server to where the file will be run from.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
The subroutine just makes a file on their profile as a 'flag' to say that the printer has been installed, then installs the printer and 'makes' the flaf file so next time it doesn't do anything.  Give it a try trunning it manually without the echo off at the top for instance and you will see what it does I think.

That is fine as long as they are all on the same server - as at the moment \\server is codes in the setupprinter subroutine.  If they are on more than one server can adjjust.

Steve
0
 

Author Comment

by:rgoddard2008
Comment Utility
all the ptinters will be on the same server, but the script will run from a different server
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Then above should be fine.  Have commented code here for you:

@echo off
rem Rest of existing login script
REM net use commands etc.

REM Make sure the printers folder exists in users' profile
mkdir "%userprofile%\printers" >NUl 2>&1

REM Add a call for each printer to make sure it is mapped.
call :setupprinter printer1
call :setupprinter printer2

REM Goto end of file so it skips the subroutines
goto :eof

:setupprinter
REM subroutine to take printer name, check if file exists in profile
REM and if not then connect to printer and create flag file so that
REM it only tries to install once.

 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1"
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"
0
 

Author Comment

by:rgoddard2008
Comment Utility
hiya,

i got the above working... only thing i have noticed is .. when i call the printer .. i have to enter the print queue in for each printer (share name) ... and i dont think i can run this along side our other batch file of installing a printer by IP ...
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Glad you got it working.  Should be no reason why you can't do it with any other batch file.  if you aren't sure post the batch file (or part of it) here or send it via email if you prefer (click on dragon-it name to get it) and will see for you.

Steve
0
 

Author Comment

by:rgoddard2008
Comment Utility
script im using to check ip address is XXX then installs printers as per floor.. so i wanted the IF statement to for new printers added etc.

@echo off
setlocal
 
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IP Address"') do set IP=%%a
set IP=%IP: =%
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Subnet"') do set Mask=%%a
set Mask=%Mask: =%
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
  set s1=%%a
  set s2=%%b
  set s3=%%c
  set s4=%%d
)
for /f "tokens=1-4 delims=." %%a in ("%Mask%") do (
  set /a s1 "&=" %%a
  set /a s2 "&=" %%b
  set /a s3 "&=" %%c
  set /a s4 "&=" %%d
)
set LocalNet=%s1%.%s2%.%s3%.%s4%
echo Local Subnet: %LocalNet%
if "%LocalNet%"=="192.168.1.0" call "\\server\Printers.bat"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Ok, you could change your if lines to something like this to call a subroutine for each subnet, then have your subroutine for printers

REM Map printers for all subnets and specific ones for subnet  1 and 2
call :allnets
if "%LocalNet%"=="192.168.1.0" call :subnet1
if "%LocalNet%"=="192.168.2.0" call :subnet2
goto :eof

:allnets
 call :setupprinter printer1
goto :eof

:subnet1
 call :setupprinter printer2
 call :setupprinter printer3
goto :eof

:subnet2
 call :setupprinter printer3
 call :setupprinter printer4
goto :eof

and add this to start of it

REM Make sure the printers folder exists in users' profile
mkdir "%userprofile%\printers" >NUL 2>&1

and this to the end of it

REM Goto end of file so it skips the subroutines
goto :eof

:setupprinter
REM subroutine to take printer name, check if file exists in profile
REM and if not then connect to printer and create flag file so that
REM it only tries to install once.

 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1"
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"
0
 

Author Comment

by:rgoddard2008
Comment Utility
is there a way to call setupprinter from a txt file ?
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
What do you want to do, have a list of printers for each subnet?  How many subnets, how many printers?

0
 

Author Comment

by:rgoddard2008
Comment Utility
yes, currently i have 3subnets which is in a batch file to install these printers, i wonder if its possible to look at this batch / txt file to see if a printer is installed.

i am aiming for this, that if im on subnet one.. i get all the printers for subnet one, but say after 6months i add a new printer, i want the computer to check agaisnt a list to say .. hang on i dont have this one, lets install it.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
OK, I'm sure what you have above will do this... can you post your printer mapping batch then if it is different to what have ?
0
 

Author Comment

by:rgoddard2008
Comment Utility

Start \\server\DummyPrint
Start \\server\Finance
Start \\server\IT_Colour

this is all the batch does, it just starts each print queue... not complicated at all.. :)
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
So... why can't you add it all in one as above?  The point of the "setupprinter" routine is that you wanted to be able to track if the printers were installed or not.  That needs to be for each printer or, as you say, if you added an extra printer existing clients woudl not get it.
i.e.

@echo off
setlocal

REM Make sure the printers folder exists in users' profile
mkdir "%userprofile%\printers" >NUL 2>&1

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IP Address"') do set IP=%%a
set IP=%IP: =%
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Subnet"') do set Mask=%%a
set Mask=%Mask: =%
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
  set s1=%%a
  set s2=%%b
  set s3=%%c
  set s4=%%d
)
for /f "tokens=1-4 delims=." %%a in ("%Mask%") do (
  set /a s1 "&=" %%a
  set /a s2 "&=" %%b
  set /a s3 "&=" %%c
  set /a s4 "&=" %%d
)
set LocalNet=%s1%.%s2%.%s3%.%s4%
echo Local Subnet: %LocalNet%

REM Map printers for all subnets and specific ones for subnet  1 to 3
call :allnets
if "%LocalNet%"=="192.168.1.0" call :subnet1
if "%LocalNet%"=="192.168.2.0" call :subnet2
if "%LocalNet%"=="192.168.3.0" call :subnet3
goto :eof

:allnets
 call :setupprinter printer1
goto :eof

:subnet1
 call :setupprinter HP1230
 call :setupprinter Reception
goto :eof

:subnet2
 call :setupprinter Sales
 call :setupprinter Marketing
goto :eof

:subnet3
 call :setupprinter Finance
 call :setupprinter IT_Colour
goto :eof

REM Goto end of file so it skips the subroutines
goto :eof

:setupprinter
REM subroutine to take printer name, check if file exists in profile
REM and if not then connect to printer and create flag file so that
REM it only tries to install once.

 if "%1"=="" goto :eof
 if exist "%userprofile%\printers\%1.txt" goto :eof
 start "" "\\server\%1"
 echo Installed %date% %time% by %username% > "%userprofile%\printers\%1.txt"
0
 

Author Comment

by:rgoddard2008
Comment Utility
i see what you mean now, sorry mind block... i wouldnt need the other batch file  as i would replace it. okay thankyou i will work with the one above and post back my success.

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Ok, no problem!
0
 

Author Comment

by:rgoddard2008
Comment Utility
this works brilliantly!!! ... i think we might have just put me out of a job! :)

thanks alot for your help!!!
0
 

Author Closing Comment

by:rgoddard2008
Comment Utility
Excellent!!!!
0
 

Author Comment

by:rgoddard2008
Comment Utility
it doesnt work on windows 2000... i get a "do you want to install driver" .. i have found switches to surpress /y /v but doesnt work...

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now