Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#1
Post
by absuk » 23 Dec 2012 07:26
Guys,
I found this script on the web.
Code: Select all
@ECHO OFF
FOR /f "tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"') DO (
SET PA=%%a
IF NOT "%PA%"=="." GOTO Done
)
:Done
ECHO D%PA:-=%|clip
CLS
ECHO The Physical Address has been copied to the clipboard.
ECHO.
PAUSE
EXIT
however, what i want it to do is to copy the Mac address from the a list of adapters. as the machine is a VM, the adapter i want to capture is
Ethernet adapter Ethernet
its the second Mac address in the list. is there a way to ask it to skip the first result, and pick the second one.
thanks all.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 23 Dec 2012 08:15
Changes in blue:
@echo off
FOR /f "skip=1 tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"') DO (
SET PA=%%a
GOTO Done
)
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#3
Post
by absuk » 23 Dec 2012 08:17
Thank you so very mcuh. you have made my day.. happy christmas.

-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 23 Dec 2012 08:20
This is a more reliable option if that "Ethernet adapter Ethernet" is the exact text in the line:
Code: Select all
@echo off
FOR /f "tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"^|FINDSTR /c:"Ethernet adapter Ethernet"') DO SET PA=%%a
ECHO D%PA:-=%|clip
CLS
ECHO The Physical Address has been copied to the clipboard.
ECHO.
PAUSE
EXIT
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 23 Dec 2012 17:01
If you are on Vista, 7 or 8 you can use the GETMAC command.
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#6
Post
by Ed Dyreen » 23 Dec 2012 18:40
Squashman wrote:If you are on Vista, 7 or 8 you can use the GETMAC command.
If you are on XP also, I believe millenium, 98 has it too...
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 23 Dec 2012 18:57
Ed Dyreen wrote:Squashman wrote:If you are on Vista, 7 or 8 you can use the GETMAC command.
If you are on XP also, I believe millenium, 98 has it too...
Neither Win98SE or Win2000 have getmac as default. XP does have it though.
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#8
Post
by absuk » 24 Dec 2012 06:46
Wow. thank you all very much for the replies. thank you Mr Fox, very helpful. thanks again everyone for your help. am just trying to automate a few things on AWS while keeping it simple for my challenging brain.

-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#9
Post
by absuk » 24 Dec 2012 07:12
last question if anyone knows. i am trying to get this VBScript to paste the data from the clip board to replace "textdata" anyone got any pointers
Code: Select all
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "settings.xml"
'Locate the desired node
'Note the use of XPATH instead of looping over all the child nodes
Set nNode = xmlDoc.selectsinglenode ("//dataroot/address/helpers")
'Set the node text with the new value
nNode.text = "textdata"
'Save the xml document with the new settings.
strResult = xmldoc.save("settings.xml")
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#10
Post
by abc0502 » 24 Dec 2012 07:45
you already have the code, then you could past the data from clipboard to a file.
then use a batch code to take this data from that file and use it as an input to replace the word or the sentence with it.
you can have a look at this to replace lines "by Aacini" :
link
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#11
Post
by absuk » 24 Dec 2012 07:53
Hi,
from what understood before, you cant edit xml files using windows .bat code. hence why I thought it would be better to use a VBscript, guess am just looking for the quickest way to set this up.
thanks for the link, but its way to confusing to make any sense. I will keep trying with figuring out how to paste from clipboard in the VBscript.
thanks again

-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#12
Post
by foxidrive » 24 Dec 2012 08:22
This will paste from clipboard to file. If no filename is supplied then it will use 'cliptext.txt' as the file.
Code: Select all
@echo off
if "%~f1"=="" %0 "cliptext.txt"
(
echo Set objHTML = CreateObject("htmlfile"^)
echo ClipboardText = objHTML.ParentWindow.ClipboardData.GetData("text"^)
echo Set objFSO = CreateObject("Scripting.FileSystemObject"^)
echo Set objFile = objFSO.OpenTextFile("%~f1", 2, true^)
echo objFile.WriteLine ClipboardText
echo objFile.Close
) > "%temp%\clip.vbs"
start "" "%temp%\clip.vbs"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#13
Post
by foxidrive » 24 Dec 2012 08:25
Here are three search and replace batch files.
Code: Select all
:: Search and replace
@echo off
if "%~2"=="" (
echo search and replace usage: "%~nx0" "search string" "new string"
echo.
echo. input file is in.txt and output file is out.txt
pause
goto :EOF
)
set "file=in.txt"
set "newfile=out.txt"
set "oldstr=%~1"
set "newstr=%~2"
set "tempfile=%temp%\sartmp.vbs"
echo> "%tempfile%" s = Wscript.StdIn.ReadAll
echo>> "%tempfile%" Wscript.Echo Replace(s,"%oldstr%","%newstr%")
type "%file%" |cscript /nologo "%tempfile%" > "%newfile%"
del "%tempfile%"
Code: Select all
::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs
::Search and replace with Jscript
:: uses regular expressions
Code: Select all
@set @JScript=1/*
@echo off
setlocal
cscript //nologo //E:JScript "%~f0"
goto :eof
*/
// JScript
//
var ForReading= 1
var ForWriting = 2
var fso = new ActiveXObject("Scripting.FileSystemObject");
// paths must have doubled backslashes if used
// var input = fso.OpenTextFile("c:\\temp\\filein.txt", ForReading)
// var output = fso.OpenTextFile("c:\\temp\\fileout.txt", ForWriting, true)
var input = fso.OpenTextFile("filein.txt", ForReading)
var output = fso.OpenTextFile("fileout.txt", ForWriting, true)
var data = input.Readall()
data = data.replace(/text to replace/gi, "new text")
output.Write(data)
input.Close()
output.Close()
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#14
Post
by absuk » 24 Dec 2012 08:41
Hi Foxidrive.
thank you for all your help, think i have enough to go on with. Thanks a million for taken the time to reply.
hope you have a great merry Christmas.
Cheers
A
-
absuk
- Posts: 8
- Joined: 23 Dec 2012 07:22
#15
Post
by absuk » 25 Dec 2012 08:42
Hi Foxidrive, I have changed the way to do things. do you know how i can combine these 2x scripts to capture the MAC address and place it in the XML file.
XML VB Script
Code: Select all
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "settings.xml"
'Locate the desired node
'Note the use of XPATH instead of looping over all the child nodes
Set nNode = xmlDoc.selectsinglenode ("//settings/server/")
For Each objItem in colItems
'Set the node text with the new value
nNode.text = "NEW MAC DATA"
'Save the xml document with the new settings.
strResult = xmldoc.save("settings.xml")
capture MAc address VBscript
Code: Select all
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapter " _
& "Where NetConnectionID = " & _
"'Local Area Connection'")
For Each objItem in colItems
Wscript.Echo "MAC Address: " & objItem.MACAddress
Next
all i want to do is combine the VBscripts to populate the new nNode.text = "NEW MAC DATA"
hope this makes life simpler.