Adding characters to variable in FOR loop

July 17, 2009 at 12:08:28
Specs: Windows XP, 1.3 Ghz / 1 Gb RAM
We have a batch file that outputs MAC addresses without any colons or dashes. I'm trying to write a small script that will simply add dashes for every third character in the string. I was attempting to do it in a FOR /F loop, but couldn't get the individual characters in %%a separated.

An example of a MAC address we have would be: "0024e8919447"
I would want to make it "00-24-e8-91-94-47"
I have text files with ten to twenty MAC addresses each like this.

I've done this with variables in the past, just never within a FOR /F loop. Is there a simpler way to do this?

Thanks in advance for any help!

See More: Adding characters to variable in FOR loop

Report •

July 17, 2009 at 13:16:36
@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in (MAClist) do (
set s=%%a
echo !s:~0,2!-!s:~2,2!-!s:~4,2!-!s:~6,2!-!s:~8,2!-!s:~10,2!>> newfile

If at first you don't succeed, you're about average.


Report •

July 17, 2009 at 17:17:27
here's a vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strMAC = objArgs(0)
For i=1 To Len(strMAC) Step 2
	s=s & Mid(strMac,i,2)&"-"
WScript.Echo Left(s,Len(s)-1)

save it as mymac.vbs then on command line

C:\test>cscript /nologo test.vbs 0024e8919447

GNU win32 packages | Gawk

Report •

July 20, 2009 at 06:46:59
Mechanix2Go: As usual, your script worked like a charm! Thanks!!!

ghostdog: I'm keeping as much of my stuff in batch for the time being, but I appreciate the VBS as well! I will definitely hang on to that for future use!! Thanks!

Report •

Related Solutions

Ask Question