Computing.Net > Forums > Programming > Adding characters to variable in FOR loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Adding characters to variable in FOR loop

Reply to Message Icon

Name: keridbey
Date: July 17, 2009 at 12:08:28 Pacific
OS: Windows XP
CPU/Ram: 1.3 Ghz / 1 Gb RAM
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 17, 2009 at 13:16:36 Pacific
Reply:

@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.

M2


0

Response Number 2
Name: ghostdog
Date: July 17, 2009 at 17:17:27 Pacific
Reply:

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)&"-"
Next
WScript.Echo Left(s,Len(s)-1)

save it as mymac.vbs then on command line

C:\test>cscript /nologo test.vbs 0024e8919447
00-24-e8-91-94-47

GNU win32 packages | Gawk


0

Response Number 3
Name: keridbey
Date: July 20, 2009 at 06:46:59 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Adding characters to variable in FOR loop

count in for loop in dos www.computing.net/answers/programming/count-in-for-loop-in-dos/16609.html

Variables in For loops, Batch www.computing.net/answers/programming/variables-in-for-loops-batch/17037.html

Can't get file to work in for loop www.computing.net/answers/programming/cant-get-file-to-work-in-for-loop-/17103.html