2016-02-29 53 views
1

我要完成 - 文本文件的写入当前目录的完整路径与蝙蝠的文本文件

1.read内容

2.save当前目录路径变量

3 .replace在内容的文本串与路径

 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 

回答

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" 
输出