Batch file to copy and rename file with ipaddress.extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mushroomking
Posts: 1
Joined: 21 Sep 2018 04:41

Batch file to copy and rename file with ipaddress.extension

#1 Post by Mushroomking » 21 Sep 2018 04:46

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 21 Sep 2018 08:05

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#3 Post by Ed Dyreen » 21 Sep 2018 08:54

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.

Post Reply