Link to home
Start Free TrialLog in
Avatar of jskfan
jskfanFlag for Cyprus

asked on

Read from Text file with Batch Script

Read from Text file with Batch Script

I have this Script that adds a printer to a computer named Windows7.
The Script is good when you run it against one computer, but when you have many computers, you will have to change the computer name inside the Script for every computer your run the script against.

I would like to have this script gets the computer name from a separate text file.

Thank you


@echo starting pnpport 
cscript.exe "C:\windows\system32\Printing_Admin_Scripts\en-US\prnport.vbs" -a  -s windows7 -r "printer1" -h hostname -o raw -n 9100
@echo Waiting 5 seconds
timeout 5
@echo starting setting MyPrinter
cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a  -p "Myprinter" -m "Generic / text only" -r "printer1" -s windows7
timeout 5

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Avatar of jskfan

ASKER

Thanks oBdA.

I will test it whenever I get a chance. in the meantime if you can take a look at this question and see if you can help me with it.

Appreciate it.



https://www.experts-exchange.com/questions/29166430/Enable-Print-Directly-to-Printer-using-Windows-Batch.html
Avatar of jskfan

ASKER

I tried the batch file, but it opens the text file ComputerList.txt, it does not execute the code.

@echo off
setlocal
for /f %%a in ('C:\ComputerList.txt') do (
	@echo starting pnpport on %%a
	cscript.exe "C:\windows\system32\Printing_Admin_Scripts\en-US\prnport.vbs" -a  -s %%a -r "printer1" -h hostname -o raw -n 9100
	@echo Waiting 5 seconds
	timeout 5
	@echo starting setting MyPrinter on %%a
	cscript C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a  -p "Myprinter" -m "Generic / text only" -r "printer1" -s %%a
	REM Not really needed, because the script will continue with the next machine
	REM timeout 5
)

Open in new window

Avatar of jskfan

ASKER

I guess I figured it out

I put for /f %%a in (ComputerList.txt) do

instead of for /f %%a in ('C:\ComputerList.txt') do
Avatar of jskfan

ASKER

Excellent!!
Thank you for your Help!
Avatar of oBdA
oBdA

The "type" and the single quotes were there on purpose. It avoids issues with Unicode files, and when the path must be in double quotes.
for /f %%a in ('type "C:\ComputerList.txt"') do (...

Open in new window

Avatar of jskfan

ASKER

so if I have left it as :
for /f %%a in ('type "C:\ComputerList.txt"') do

Open in new window


it will still work ?
You don't have to change it; if it works with your change, it's OK as well.
Using "type" just avoids the issues I described above.
If you want to replace it, don't forget the opening bracket at the end.
for /f %%a in ('type "C:\ComputerList.txt"') do (

Open in new window