我想完成 -
1.read文本文件的内容
2.保存变量的当前目录路径
3.使用路径
替换内容中的文本字符串 set mypath=%cd%
set content=
for /f "delims=:" %%i in (
'type text.txt') do set. content=%content% %%i
echo %content%
set str=%content%
set str=%str:stringtoreplace= mypath %
@echo off
(echo %str%)>text.txt
答案 0 :(得分:1)
您无法在同一个正在阅读的文件中书写!
启用delayed expansion
并尝试如下:
@echo off
setlocal enabledelayedexpansion
set "mypath=%cd%"
set "stringtoreplace=toto"
(for /f "delims=" %%a in ('type test.txt') do (
set "content=%%a"
set "content=!content:%stringtoreplace%=%mypath%!"
echo !content!
))>output.txt
如果您需要具有相同名称的输出
,请在末尾执行rename
del "test.txt"
ren "output.txt" "test.txt"