Sukhpreet Grewal
Sukhpreet Grewal

Reputation: 63

Write full path of current directory to a text file with bat file

I want to accomplish -

1.read contents of text file

2.save current directory path to a variable

3.replace a text string in contents with the path

     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

Upvotes: 1

Views: 562

Answers (1)

SachaDee
SachaDee

Reputation: 9545

You can't write in the same file you are reading !

Enable the delayed expansion and try like this :

@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

Do a rename at the end if you need the output with the same name

del "test.txt"
ren "output.txt" "test.txt"

Upvotes: 1

Related Questions