Solved

Batch file help

Posted on 2004-04-23
6
2,426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

732 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