0
set SourceDir=\\my_server\path    

if "%SourceDir:~0,2%"=="\\" (
  set sourceHost= REM not sure what to do. here I need to isolate hostname
  for /f "tokens=2" %%b in ('nslookup %SourceDir%^|find /i "Address"') do set ser_ip=%%b
  REM here I need to replace my_server with %ser_ip%     
)

In this code I'm trying to replace the host-name with IP address in a network path.

  1. in the set sourceHost= REM not sure what to do. here I need to isolate hostname Line I need to isolate the hostname from the path
  2. in the REM here I need to replace my_server with %ser_ip% line need to replace host name with retrieved ip address

if the nslookup ip adress is 10.12.13.14 last result should be \\10.12.13.14\path Please help me for these two lines. Thank you!

2
  • Are you getting the hostname of the pc it's running on, or the hostname of the server? Do you know the name of the server already? Commented Apr 6, 2017 at 14:57
  • its host name of the server. Network path pass to the batch file. So I have the hostname as the first part of the path. \\`my_server`\path Commented Apr 6, 2017 at 23:41

2 Answers 2

1

I'd go with ping instead of nslookup. If there are IPv6 addresses they are more difficult to parse and ping has a -4 option.
The ip is easy to extract as it is enclosed in square brackets.:

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
set "SourceDir=\\my_server\path"
if "%SourceDir:~0,2%"=="\\" (
  for /f "delims=\" %%H in ( "%SourceDir%"
  ) Do for /f "tokens=2delims=[]" %%b in ('ping -n 1 -4 %%H^|find "["'
  ) do Set "SourceDir=!SourceDir:%%H=%%b!"
)
Echo %SourceDir%
Sign up to request clarification or add additional context in comments.

1 Comment

I choose this answer because of the IPV6 thing @LotPings talks about, though aschipfi answer cater for my requirement.
1

What about this (assuming you want to stick to nslookup):

set "SourceDir=\\my_server\path"

if "%SourceDir:~0,2%"=="\\" (
  rem // Extract server name and splif off remaining path:
  for /F "tokens=1* delims=\" %%a in ("%SourceDir%") do set "sourceHost=%%a" & set "sourceShare=%%b"
  for /F "tokens=2" %%b in ('nslookup %SourceDir%^|find /I "Address"') do set "ser_ip=%%b"
  rem // Assemble new path by IP and former remaining path:
  echo "\\%ser_ip%\%sourceShare%"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.