script read file then runs command

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
cedtech23
Posts: 2
Joined: Fri Mar 06, 2009 4:06 am

script read file then runs command

Post by cedtech23 »

I want to create a script that read a file and for each line in the file it run a command with the line data as the argument

example:
text file contains

192.168.1.1
192.168.1.2
192.168.1.3

then the script with run

wake 192.168.1.1

wake 192.168.1.2

wake 192.168.1.3

I think I have the reading of the file figured out but I am missing how to run the command for each argument .

Thanks

Code: Select all

'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
  Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
  WScript.Echo "Input file " & g_strHostFile & " not found."
  WScript.Quit
End If



User avatar
cedtech23
Posts: 2
Joined: Fri Mar 06, 2009 4:06 am

script read file then runs command

Post by cedtech23 »

I want to create a script that read a file and for each line in the file it run a command with the line data as the argument

example:
text file contains

192.168.1.1
192.168.1.2
192.168.1.3

then the script with run

wake 192.168.1.1

wake 192.168.1.2

wake 192.168.1.3

I think I have the reading of the file figured out but I am missing how to run the command for each argument .

Thanks

Code: Select all

'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
  Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
  WScript.Echo "Input file " & g_strHostFile & " not found."
  WScript.Quit
End If

User avatar
rasimmer
Posts: 182
Joined: Fri Jan 30, 2009 12:37 am

script read file then runs command

Post by rasimmer »

Your close, you just need to loop through the file line by line by utilizing .AtEndOfStream and then read each line by (you'll never guess) using .ReadLine. You also need to create the Shell object so that you can run the wake command. Try the script below and it should get you on the right track....

Code: Select all

	 Const FOR_READING = 1
g_strHostFile = "computers.txt"

'Read computer names for install from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSHShell = WScript.CreateObject("WScript.Shell")
	
If objFSO.FileExists(g_strHostFile) Then
   Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
	
Do Until objTextStream.AtEndOfStream
	
   strComputer = Trim(objTextStream.ReadLine)
	
   strResult = objWSHShell.Run("C:whatever_pathwake.exe " & strComputer
	
   If strResult = 0 Then
	
       WScript.Echo  strComputer & vbtab & "Wake command ran successfully"
	
   Else
	
       WScript.Echo strComputer & "Wake command failed"
	
   End If
	
Loop
Else
  WScript.Echo "Input file " & g_strHostFile & " not found."
  WScript.Quit
End If
rasimmer2009-03-06 13:32:21

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

script read file then runs command

Post by jvierra »

Here is a one liner that does it all. It can be tested from teh command line.

In folder with computers.txt at a command prompt type:
FOR /F %c IN (computers.txt) DO wake %c

This will send the command to each computer in succession.


User avatar
rasimmer
Posts: 182
Joined: Fri Jan 30, 2009 12:37 am

script read file then runs command

Post by rasimmer »

This is the VBScript post, not CMD Shell post...sheesh...some people ;oP

Locked