Reputation: 1
I would like to ask you, how can I do this:
I have three files in folder, car.txt
, bus.txt
, plane.txt
. File car.txt
contains some text with string for example test="in_car
" and I know, that I find word with prefix "in_
" + word from filename "car
" and if I find it, I want to replace it by word "hello
". And for files bus.txt
and plane.txt
do same thing, find word in_bus
and replace it by hello
etc. And I know that prefix "in_
" is only at this place.
Please, can you help me?
I try something like this:
for /R "../myFolder" %%f in (*.txt) do (
set FILENAME=%%~nf
set PREFIX=in_
set REPLACETEXT=hello
SET string=%%A
set modified=!string:%PREFIX%%FILENAME%=%REPLACETEXT%!
)
but it is not working and I don't know, how can I save it to current file (replace it in file)
Upvotes: 0
Views: 321
Reputation: 1
so I have this function solution:
pushd "../myFolder"
Setlocal enabledelayedexpansion
set "FILENAME=%%~nf"
set "OUTPUT=%%~nxf"
set "PREFIX=in_"
set "REPLACETEXT=hello"
set "TEMP=tmp"
for /R %%f in (*.txt) do (
for /f "delims=" %%A in (%%f) do (
SET "string=%%A"
SET modified=!string:%PREFIX%%FILENAME%=%REPLACETEXT%!
echo !modified! >> %TEMP%
)
del %OUTPUT%
rename %TEMP% %OUTPUT%
)
popd
but there is still one problem, I have not character ! in summary of output file, one line contains < !-- (without space,but I can't write it right here) but in output I have only this <--
Upvotes: 0
Reputation: 56180
you forgot another for
(to read the files) (although you obviously planned it, as you used %%A
):
@echo off
setlocal enabledelayedexpansion
set PREFIX=in_
set REPLACETEXT=hello
for /R "." %%f in (*.txt) do (
set FILENAME=%%~nf
(for /f "delims=" %%A in (%%f) do (
SET string=%%A
set modified=!string:%PREFIX%%%~nf=%REPLACETEXT%!
echo !modified!
REM echo !string:%PREFIX%%%~nf=%REPLACETEXT%!
))>%%~dpnf.new
)
Upvotes: 1