My time server (router) is 192.168.8.1
. To update a Windows box's time:
net time \\192.168.8.1 /set /y
The goal is to implement the command with a variable in a .bat file:
set "ip="
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do if not defined ip set ip=%%b
net time \\%ip% /set /y
Why does the script implement a space between \
and the 192.168.8.1
:
net time \\ 192.168.8.1 /set /y
How to remedy?
If the variable as it is has a space, how is it "clipped" off?
My time server (router) is 192.168.8.1
. To update a Windows box's time:
net time \\192.168.8.1 /set /y
The goal is to implement the command with a variable in a .bat file:
set "ip="
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do if not defined ip set ip=%%b
net time \\%ip% /set /y
Why does the script implement a space between \
and the 192.168.8.1
:
net time \\ 192.168.8.1 /set /y
How to remedy?
If the variable as it is has a space, how is it "clipped" off?
Share Improve this question edited Feb 7 at 6:55 Mofi 49.1k19 gold badges87 silver badges153 bronze badges asked Feb 7 at 3:44 gatorbackgatorback 1,5375 gold badges23 silver badges47 bronze badges 3 |1 Answer
Reset to default 0The space is appearing because the ipconfig
line contains a space after the colon, so it is included in the value set
into the variable.
Simplest solution afaics is
net time \\%ip:* =% /set /y
which should remove all characters in ip
up to, and including, the first space (the character between the *
and =
and replacing that string with nothing (the string abetween the =
and the %
)
for /f
line to:for /F "tokens=2 delims=:" %%G in ('%SystemRoot%\System32\ipconfig.exe ^| %SystemRoot%\System32\find.exe "Default"') do if not defined ip for /F %%H in ("%%G") do set "ip=%%H"
Open a command prompt window, runfor /?
and read the output usage help carefully and completely from top of first to bottom of last page. It looks like you are usingfor /F
without knowing what all the options liketokens=
anddelims=
really mean. There is a space afterDefault Gateway . . . . . . . . . :
. The delimiter of firstfor /F
is just:
. The space after:
is kept! – Mofi Commented Feb 7 at 7:01