Question

My first post so please be gentle.

I have batch file that outputs ipconfig/all data to a txt file on my desktop.

Is it possible to use the ip address or mac address data contained in the file to reanme the file.

Mark

No correct solution

OTHER TIPS

Yes, it is possible.

First you need to find the address. E.g. with

findstr /C:"  IPv6 Address" foo.txt

Then you need to get the address into a variable:

set Address=
for /f "delims=: tokens=1*" %%a in ('findstr /C:"  IPv6 Address" foo.txt') do (
  if not defined Address=%%b
)

Note the use of %%b there, despite the loop variable being %%a. That's because tokenization creates new variables. Also we're only using the first address because there might be several, but that's only a vague heuristic because it's not really set in stone whether non-NIC devices come before or after them.

Then we need to clean up the address because colons are not valid in file names:

set Address=%Address::=_%

And rename the file

ren foo.txt %Address%.txt

You surely can figure out how to adapt this to IPv4 or MAC addresses, I guess.

This allows you to select the MAC address you need, if you adjust the terms.

At the moment it picks the first Ethernet device it finds in the ipconfig /all output and creates a txt file with the MAC address.

@echo off
for /f "delims=" %%a in ('ipconfig /all ^|findrepl "Ethernet" /e:"Physical Address" /b:"Physical Address" ^|findrepl ".*Physical Address.*: (.*)" "$1" ^|findrepl /o:1:1 ') do (
ipconfig /all >"%%a.txt"
)

The above uses a helper batch file called findrepl.bat - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat in the same folder as the batch file or on the path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top