Solved

If Statement to check if printer is installed

Posted on 2008-10-02
23
1,665 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
ID: 22623104
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
ID: 22623112
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
ID: 22623131
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
ID: 22623397
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
ID: 22623469
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
ID: 22623613
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
ID: 22623686
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
ID: 22623719
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
ID: 22648704
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
ID: 22648822
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
ID: 22648897
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 22649096
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
ID: 22649140
is there a way to call setupprinter from a txt file ?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22649160
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
ID: 22649243
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
ID: 22649323
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
ID: 22649354

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
ID: 22649455
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
ID: 22649918
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
ID: 22650019
Ok, no problem!
0
 

Author Comment

by:rgoddard2008
ID: 22650663
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
ID: 31502325
Excellent!!!!
0
 

Author Comment

by:rgoddard2008
ID: 22651300
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now