Replace a string at a fixed position in a text file with the output of a command with cmd
I wants to replace the QUICKSERVER
string in line 3 of my text file which is located at c:\BR.txt
with the output from the hostname
command in Windows CMD. hostname
is a Windows command which returns the system name.
C:\>hostname
QUICKSERVER
my text file is:
开发者_C百科 userName=Administrator
password=
CMS=QUICKSERVER:6400
authentication=secEnterprise
Please find the command which I can execute in the Windows command line to replace the hostname with QUICKSERVER
.
The host name can also be found in the %computername%
environment variable, so you don't need the hostname
command.
@echo off
setlocal enabledelayedexpansion
for %%L in (c:\br.txt) do (
set "LINE=%%L"
set "LINE=!LINE:QUICKSERVER=%COMPUTERNAME%!"
echo !LINE!
) > C:\br2.txt
move /y C:\br2.txt c:\br.txt
精彩评论