Batch file question

Posted on 2008-11-06
Medium Priority
Last Modified: 2012-05-05
I am fairly familiar with using batch files to automate simple tasks, and use them often in conjuction with Scheduled Tasks to automate tasks that otherwise are not able to be scheduled. What I would like to do is find out if it is possible to write a batch file or script to loop through a command or series of commands, and use a variable which is replaced by the name of a computer on my network. So I could create a list of computer names on my network, then run the script and have the script reference the list of computer names, and loop through whatever commands are in the script applying each name to the variable.

For example, if I wanted to copy a particular file from the server to the System32 folder on each client, the script might contain the command: copy  filename \\remotecomputername\c$\windows\system32

How can I write it to loop and replace remotecomputername with a name from my list of computers? I realize for this example I could just use a logon script, but that is not the point. I want to learn how to use a variable list in a script.

Question by:westone
  • 3
  • 2

Expert Comment

ID: 22896819
Interesting...  I just wrote a script to do that yesterday.  Are your computers on a domain?  If so, you can use the script below.  You will have to make some modifications related to OU and the file you want to copy.  Let me know if you have any questions with the script.  Save the text as a .vbs file and run it once you have made the changes.  

'This script copies a file from the local computer to every computer in the specified OU
On Error Resume Next
Const ADS_Scope_Subtree=2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open = "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_Scope_Subtree
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the name and location of the log file here
objLogFile = "C:\copiedFileLog.txt"
Set objFile = objFSO.CreateTextFile(objLogFile)
'Change the OU here
objCommand.CommandText="Select Name from 'LDAP://ou=yourOU,dc=yourDomain,dc=com' where objectcategory='computer'"
Set objRecordSet=objCommand.Execute
Do Until objRecordSet.EOF
	strComputer = objRecordSet.Fields("Name").Value
	'This pings the computer before attempting to change the password.  If the computer is not reachable, it is skipped.
	Set objShell = CreateObject("WScript.Shell")
	strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
	set objExecObject = objShell.Exec(strCommand)
		Do While Not objExecObject.StdOut.AtEndOfStream
			strText = objExecObject.StdOut.ReadAll()
				If Instr(strText, "Reply") > 0 Then
'Change the location where you want to copy the file to here
strPath = "\\" & strComputer & "\C$\Windows\system32\filename.txt"
'This actually copies the file, change c:\filename.txt to the file you want to copy
objFSO.CopyFile "c:\filename.txt", strPath, True
If Err = 0 Then	
'This is the text that is written to the log file when it succeeds
	objFile.WriteLine("Copied Filename.txt to " & strComputer & ".")					
'This is the text that is written to the log file when it fails
	objFile.WriteLine("Unable to copy secedit.sdb to " & strComputer & ".")
End if
'This is written to the log file if a computer is unreachable
					objFile.WriteLine("Unable to ping " & strComputer & ".")
				end if
WScript.echo("The utility completed successfully.  Please check the log file " & objLogFile & " for errors.")

Open in new window


Author Comment

ID: 22897134
'This pings the computer before attempting to change the password.  If the computer is not reachable, it is skipped.

What password does this script change? I am logged in as Administrator on DC, and only targeting computers in this domain. I have access to all the drives without resetting passwords, and prefer not to change any.

Expert Comment

ID: 22897304
I'm sorry about that.  It does NOT change any passwords.   Those comments are incorrect.  
(I copied the ping function from a previous script and it had those comments).  

It just pings the computer name.  If the computer doesn't respond, it will skip to the next section.  It should say, 'This pings the computer before attempting to copy the file.  You can change the comments, but they are safe to ignore.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 22897371
Okay, I have used a vb script to change passwords on a remote computer before, and I didn't think I saw any code that looked like it was doing that. Give me some time and I'll get back to you.
LVL 38

Accepted Solution

Shift-3 earned 2000 total points
ID: 22897560
To do this in a batch script, use the FOR /F command.

for /F %%G in (computerlist.txt) do echo %%G

Open in new window


Author Comment

ID: 22897865
Shift-3's solution answered the specific question I asked. Cybersean's solution is more sophisticated, but with Shift-3's solution I have already been able to copy Scheduled Tasks and run maintenance tasks on all the machines on this network. Plus it has the added advantage of being able to be used on workgroups and LANs with no domain just as easily, and I look after a number of those as well. Since Cybersean's script retrieves the list of computers from AD it is restricted to domain use.
Thanks for the help.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 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