2016-02-29 52 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" 
輸出