Link to home
Start Free TrialLog in
Avatar of rgoddard2008
rgoddard2008

asked on

If Statement to check if printer is installed

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
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

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"



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"
Avatar of rgoddard2008
rgoddard2008

ASKER

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
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
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.
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
all the ptinters will be on the same server, but the script will run from a different server
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"
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 ...
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
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"
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"
is there a way to call setupprinter from a txt file ?
What do you want to do, have a list of printers for each subnet?  How many subnets, how many printers?

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.
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 ?

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.. :)
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Ok, no problem!
this works brilliantly!!! ... i think we might have just put me out of a job! :)

thanks alot for your help!!!
Excellent!!!!
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...