VBS Wake on LAN from txt file

Hello,

I have got a txt file with all the mac address i would like to send the wake on lan packet to. How would i get a vb script to exec wol.exe mac address with the right formatting?

The txt file has the information in this format.
00:00:00:00:00:00

While the wol.exe needs the mac address without the ":". Example is;
000000000000

Then the script should exec "wol.exe 000000000000" for each mac address. Also, its one mac address per line. I would like the mac address txt file left alone and the removing of the ":" done live, when the script runs. Not editing the mac address txt file at all.
cyberiadminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brendanmeyerCommented:
try
Dim objShell, objFSO, objTextFile
Dim strLine

Const ForReading = 1

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objTextFile = objFSO.OpenTextFile("<filepath/name>", ForReading)
Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.Readline
    objShell.Run "wol.exe " & strLine, 1, true
Loop

Open in new window

rockiroadsCommented:
2nd question from you for today

change this line to point to your file

checkFile = "c:\temp\maclist.txt"


dim fso
	dim checkFile
	dim hfile
	dim sLine
	dim sNewLine
	dim oShell

	checkFile = "c:\temp\maclist.txt"

	Set oShell = CreateObject("WScript.Shell")
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set hFile = fso.OpenTextFile(checkFile, 1)
	Do Until hFile.AtEndOfStream
		sLine = hFile.Readline
		sNewLine = Replace(sLine,":","")
		WScript.echo sNewLine
		oShell.run("wol.exe " & sNewLine)
	Loop
	hFile.Close	
	Set fso = Nothing

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cyberiadminAuthor Commented:
I change the "objShell.Run" to "Wscript.Echo" then i can see what it would be exec. This is what i get.

"wol.exe 00:1A:D0:DA:BC:0C 1 -1"

While the script should only exec this.
"wol.exe 001ad0dabc0c"

How would this be done?

Thanks
Dim objShell, objFSO, objTextFile
Dim strLine

Const ForReading = 1

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objTextFile = objFSO.OpenTextFile("c:\mac.txt", 

ForReading)
Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.Readline
     Wscript.Echo "wol.exe " & strLine, 1, true
Loop

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

rockiroadsCommented:
hmm, should of refreshed, sorry brendan

mistake in my line
oShell.run("wol.exe " & sNewLine)

should be
oShell.run "wol.exe " & sNewLine, 0, -1


brendan beat me to creating the code to read the file so he gets credit for that

Difference in the code now would be the removal of :
rockiroadsCommented:
also forgot to add this line

Set oShell = Nothing

Need to clean up properly!
brendanmeyerCommented:
ok

sorry i forgot about removing the :'s

as for the " 1 -1" at the end of your line that is because the , 1, true is parameters for the Run command
Dim objShell, objFSO, objTextFile
Dim strLine

Const ForReading = 1

Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objTextFile = objFSO.OpenTextFile("c:\mac.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strLine = Replace(objTextFile.Readline, ":" ,"")
    oShell.run "wol.exe " & strLine, 1, true
Loop

Open in new window

cyberiadminAuthor Commented:
rockiroads,

Your code is what i was looking for. It should not be "oShell.run "wol.exe " & sNewLine, 0, -1" but what you had the first time. Thank you, again. Looks like your getting a few points out of my posts today. :)
dim fso
	dim checkFile
	dim hfile
	dim sLine
	dim sNewLine
	dim oShell

	checkFile = "c:\mac.txt"

	Set oShell = CreateObject("WScript.Shell")
	Set fso = CreateObject

("Scripting.FileSystemObject")
	Set hFile = fso.OpenTextFile(checkFile, 1)
	Do Until hFile.AtEndOfStream
		sLine = hFile.Readline
		sNewLine = Replace(sLine,":","")
		oShell.run("wol.exe " & sNewLine)
	Loop
	hFile.Close	
	Set fso = Nothing

Open in new window

cyberiadminAuthor Commented:
Thanks again.
rockiroadsCommented:
I had WScript.echo as a test since I dont/not want to run wol.exe
oShell.run takes 3 args, I confirmed that by looking up but maybe last 2 are optional then.

Use of brackets is when return value expected to be saved. So they are optional

Feel free for more vbscript questions, I''m welcoming your points lol
rockiroadsCommented:
ok, it is optional http://msdn.microsoft.com/en-us/library/d5fk67ky%28VS.85%29.aspx. I use 0 since its then hidden

dont forget to keep

Set oShell = Nothing

in that code
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
Not sure if all that VB above did as you requested or not being mobile.

However this strikes me as a great chance to use regular windows batch since your simply executing windows commands anyway.

Just do as so:

@Echo Off
FOR /F "Tokens=* Delims=:" %%M IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %%M


OR

@Echo Off
FOR /F "Tokens=* Delims=:" %%M IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %%M


OR

@Echo Off
FOR /F "Tokens=* Delims=:" %%M IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %%M


OR


@Echo Off
FOR /F "Tokens=1-6 Delims=:" %%A IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %%A%%B%%C%%D%%E%%F

1st one may leave spaces, so if not usable the second will definitly do it for you.

Of course you past those two lines into a notepad and save as batchwol.bat, then run it by typing it's name in the command prompt

Or can just open a command promp and put this code in directly and forgo any script at all:

FOR /F "Tokens=1-6 Delims=:" %%A IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %%A%%B%%C%%D%%E%%F

1st one may leave spaces, so if not usable the second will definitly do it for you.

Of course you past those two lines into a notepad and save as batchwol.bat, then run it by typing it's name in the command prompt

Or can just open a command promp and put this code in directly and forgo any script at all:

@FOR /F "Tokens=1-6 Delims=:" %A IN ('Type "C:\admin\Maclist.txt') DO "C:\Admin\wol.exe" %A%B%C%D%E%F

PS.
GL HF DS KA ^^
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.