Page 1 of 1

Batch file to copy and rename file with ipaddress.extension

Posted: 21 Sep 2018 04:46
by Mushroomking
I'm not too familiar with batch files. Most commonly i use VBA/PHP, but i would like to have this one a go :).

I would like to:

Fetch the user's IP address, Copy a file, and rename that file to ipadres.extension

So the new file name should be the IP.

for example:

ipconfig | find "IPv4" copy /y Expeditool.accdb Expeditool"IPv4".accdb

I tried...but just puts IPv4 in file name...

Could anyone help me out with this one? Thanks!

Re: Batch file to copy and rename file with ipaddress.extension

Posted: 21 Sep 2018 08:05
by aGerman
Try

Code: Select all

for /f "delims=: tokens=2" %%i in ('ipconfig^|findstr "\<IPv4"') do for /f "tokens=*" %%j in ("%%i") do copy "Expeditool.accdb" "Expeditool%%j.accdb"
The IP address is assigned to %%j in this example.

Steffen

Re: Batch file to copy and rename file with ipaddress.extension

Posted: 21 Sep 2018 08:54
by Ed Dyreen
Systems can have multiple ethernet cards and thus multiple IP addresses but usually only a single gateway. If you don't want to run the risk receiving an invalid IP or one that is not assigned a gateway and thus cannot be used to access the internet then you should filter gateway first && then IP.