Solved

Batch file help

Posted on 2004-04-23
6
2,411 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
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 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now