Batch file 使用bat文件将当前目录的完整路径写入文本文件
我想完成- 1.读取文本文件的内容 2.将当前目录路径保存到变量 3.将目录中的文本字符串替换为路径Batch file 使用bat文件将当前目录的完整路径写入文本文件,batch-file,cmd,Batch File,Cmd,我想完成- 1.读取文本文件的内容 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:string
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
您不能在正在读取的同一文件中写入强>
启用延迟扩展
,然后像这样尝试:
@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
如果需要使用相同名称的输出,请在末尾执行重命名
del "test.txt"
ren "output.txt" "test.txt"