We help IT Professionals succeed at work.

Read from Text file with Batch Script

jskfan
jskfan used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Batch:
@echo off
setlocal
for /f %%a in ('type 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

PowerShell:
Get-Content -Path C:\Temp\Computerlist.txt | ForEach-Object {
	Write-Host "Starting prnport.vbs on $($_) ..."
	& "C:\Windows\system32\cscript.exe" "C:\windows\system32\Printing_Admin_Scripts\en-US\prnport.vbs" -a -s $_ -r "printer1" -h hostname -o raw -n 9100
	Write-Host "    Waiting 5 seconds ..."
	Start-Sleep -Seconds 5
	Write-Host "Startingsetting MyPrinter on $($_) ..."
	& "C:\Windows\system32\cscript.exe" C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -a  -p "Myprinter" -m "Generic / text only" -r "printer1" -s $_
	## Not really needed, because the script will continue with the next machine
#	Start-Sleep -Seconds 5
}

Open in new window

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
Excellent!!
Thank you for your Help!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

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

Open in new window


it will still work ?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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