?
Solved

Batch file help

Posted on 2004-04-23
6
Medium Priority
?
2,435 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 2000 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 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