1 | @echo off
|
---|
2 |
|
---|
3 | rem This script will echo the IPv4 of this windows machine. E.g. 100.200.300.45
|
---|
4 | rem If passed -format-for-tomcat-context, it will echo the same with a pipe
|
---|
5 | rem symbol up front and all the . escaped with a "\". E.g. "|100\.200\.300\.45"
|
---|
6 | rem The 3 spaces in front of the IPv4 match expression ensure we grab the line
|
---|
7 | rem that starts with "IPv4 Address", not the many others containing "IPv4 Address".
|
---|
8 | rem Without the spaces, it will grab the last line mentioning "IPv4" (ethernet).
|
---|
9 |
|
---|
10 | for /f "usebackq tokens=2 delims=:" %%a in (`ipconfig ^| find " IPv4"`) do (set result=%%a)
|
---|
11 |
|
---|
12 | :: To lop off the single extra space in front of IPv4 number, end with:
|
---|
13 | ::do (set result=%a& echo %result:~1%)
|
---|
14 | :: Alternative: for /f "tokens=2 delims=:" %a in ('ipconfig ^| findstr /C:"IPv4 Address"') do echo %a
|
---|
15 | :: still need to remove space up front for that too.
|
---|
16 |
|
---|
17 | :: removes extra space in front
|
---|
18 | set result=%result:~1%
|
---|
19 |
|
---|
20 | :: The above produces the IPv4 number, e.g. 100.200.300.45
|
---|
21 |
|
---|
22 |
|
---|
23 | rem Check if we're requested to format the IPv4 for the solr.xml tomcat context file
|
---|
24 | if [%1]==[] goto done
|
---|
25 | if not "%1"=="-format-for-tomcat-context" goto done
|
---|
26 |
|
---|
27 | :: Still need to replace . with \.
|
---|
28 | :: http://scripts.dragon-it.co.uk/scripts.nsf/docs/batch-search-replace-substitute!OpenDocument&ExpandSection=3&BaseTarget=East&AutoFramed
|
---|
29 | :: Syntax: %string:SEARCH=REPLACE%
|
---|
30 | set result=%result:.=\.%
|
---|
31 |
|
---|
32 | :: http://stackoverflow.com/questions/2541767/what-is-the-proper-way-to-test-if-variable-is-empty-in-a-batch-file-if-not-1
|
---|
33 | :: Make sure it is not the empty string or a string of spaces
|
---|
34 | :: Test by setting result to space
|
---|
35 | ::set result=
|
---|
36 | set result=%result: =%
|
---|
37 |
|
---|
38 | ::if not "%result%" == ""
|
---|
39 | ::set result=^|%result%
|
---|
40 |
|
---|
41 | :: Echo a vertical bar up front, regardless of whether the result is empty
|
---|
42 | echo ^|%result%
|
---|
43 | goto fin
|
---|
44 |
|
---|
45 |
|
---|
46 | :done
|
---|
47 | echo %result%
|
---|
48 |
|
---|
49 | :fin
|
---|