Solved

Batch file help

Posted on 2004-04-23
6
2,414 Views
Last Modified: 2007-12-19
I need help creating a batch file.

I will create a file called printerlist.txt that has a list of printers and looks something like this:
\\server\printername1
\\server\printername2
\\server\printername3

I have a command line utility called ADPRINTX (http://www.jsiinc.com/SUBM/tip6000/rh6065.htm) that is used to install remote printers from the command line.

I would like the batch file to dynamically create a list similar to below:
1. printername1
2. printername2
3. printername3

I would like the batch file to prompt the user asking which printer to make the default printer.  Then I would like every printer in the list installed with the appropriate one being set as the default.

Here is the syntax for ADPRINTX
adprintx /c "\\server\printername"         (this adds a printer)
adprintx /cd "\\server\printername"         (this adds a printer and makes it the default)


Thanks!
0
Comment
Question by:lpenrod
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:mrdtn
ID: 10902831
This will do as you ask.

--

mrdtn

--

:init
      @echo off
      setlocal enabledelayedexpansion
      set plistfile=printerlist.txt
      set /a counter=0
      echo. & echo The following printers are specified: & echo.
      for /f "tokens=*" %%r in (%plistfile%) do (
            set /a counter+=1
            set tok=%%r
            set tok=!tok:\= !
            call :getpname !tok!
            echo !counter!. !printername!
      )
      echo.
:prompt
      set /p defnum=Please select the number of the printer to be the default printer:
      set /a defnum=%defnum%
      if %defnum% leq 0 goto :prompt
      if %defnum% gtr %counter% goto :prompt
:main
      set /a counter=0
      for /f "tokens=*" %%r in (%plistfile%) do (
            set /a counter+=1
            if !counter! equ %defnum% (
                  adprintx /cd %%r
                  echo Printer %%r is now the default printer.
            ) else (
                  adprintx /c %%r
            )
      )
      pause
      goto :eof

:getpname
      :loop
            set printername=%1
            shift
            if not %1.==. goto :loop
      goto :eof
0
 
LVL 3

Author Comment

by:lpenrod
ID: 10903361
Thanks, I will check it out!
0
 
LVL 6

Accepted Solution

by:
mrdtn earned 500 total points
ID: 10904781
I realized that space characters in the server path or printer name will not work with the above script, so I have provided a modified script below which properly handles these conditions.

Let me know if there are any questions.

--

mrdtn

--

:init
      @echo off
      setlocal enabledelayedexpansion
      set plistfile=printerlist.txt
      set /a counter=0
      echo. & echo The following printers are specified: & echo.
      for /f "tokens=*" %%r in (%plistfile%) do (
            set /a counter+=1
            call :getpname "%%r"
            echo !counter!. !printername!
      )
      echo.
:prompt
      set /p defnum=Please select the number of the printer to be the default printer:
      set /a defnum=%defnum%
      if %defnum% leq 0 goto :prompt
      if %defnum% gtr %counter% goto :prompt
:main
      set /a counter=0
      for /f "tokens=*" %%r in (%plistfile%) do (
            set /a counter+=1
            if !counter! equ %defnum% (
                  adprintx /cd "%%r"
                  echo Printer "%%r" is now the default printer.
            ) else (
                  adprintx /c "%%r"
            )
      )
      pause
      goto :eof

:getpname
      set tok2=%1
      set tok2=%tok2:"=%
      :loop
            for /f "tokens=1,* delims=\" %%x in ("!tok2!") do (
                  set tok1=%%x
                  set tok2=%%y
            )
            if not "%tok2%"=="" goto :loop
      set printername=%tok1%
      goto :eof
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:mrdtn
ID: 10934566
Make sure you change the line (from my post)

    set plistfile=printerlist.txt

to point to the location of your list file.  Include drive info if necessary such as "c:\files\printerlist.txt" . . .

--

mrdtn
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 10944156
Also, avoid a path or filename containing spaces.  The for loop will choke otherwise.

--

mrdtn
0
 
LVL 3

Author Comment

by:lpenrod
ID: 10986491
You rock, that is exactly what I wanted, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

17 Experts available now in Live!

Get 1:1 Help Now