Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从批处理cmd替换文本文件中的字符串_Batch File_Cmd - Fatal编程技术网

Batch file 从批处理cmd替换文本文件中的字符串

Batch file 从批处理cmd替换文本文件中的字符串,batch-file,cmd,Batch File,Cmd,假设我有一个包含以下内容的文本文件: config { host: <host_value>, port: <port_value> } 通过使用Windows命令行 我在谷歌上搜索这个,但仍然没有期望如果你不需要命名它,但也许你可以使用这个脚本: set input=input.txt set output=output.txt if exist "%output%" del /f /q "%output%" set /p search="host_value

假设我有一个包含以下内容的文本文件:

config {
  host: <host_value>,
  port: <port_value>
}
通过使用Windows命令行


我在谷歌上搜索这个,但仍然没有期望

如果你不需要命名它,但也许你可以使用这个脚本:

set input=input.txt
set output=output.txt
if exist "%output%" del /f /q "%output%"
set /p search="host_value"
set /p replace="10.0.0.1"
for /f "tokens=*" %%a in (%input%) do (
  set newline=%%a
  call set newline=%%newline:%search%=%replace%%%
  call echo %%output%% >>%output%
)
这将创建一个新文件,您可以在使用

del /f /q %input%
ren %output% %input%
您可以使用:


这是整个文件还是更多的?如果文件真的那么小,那么用脚本简单地重新创建文件可能是值得的——它肯定会更容易;批量查找和替换非常复杂。用户如何准确输入其值?基于“我需要获取零件”,我假设这是一个“为我编写代码”问题。真的很遗憾,因为只有一种方法可以批量获取用户输入字符串,也只有一种方法可以将字符串输出到文件。对不起,我知道如何从命令行获取用户输入,但我不知道如何将这些输入设置为文本文件中的特定位置,事实上,我的文件包含的文本比我的示例中的文本长。只是出于好奇,你在谷歌上搜索了什么?我刚刚搜索了[batch file]replace string in file],找到了28页的结果。
del /f /q %input%
ren %output% %input%
set /p host=Input for Host: 

set /p port=Input for Port: 

vim -c "%s/<host_value>/%host%/g|%s/<port_value>/%port%/g" config_file.txt