0

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)

5
  • Sounds like a job for regular expressions. Commented Oct 18, 2015 at 8:40
  • 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) Commented Oct 18, 2015 at 10:47
  • 2
    Please don't post code snippets in comments; edit your question instead. I did it for occasion. Commented Oct 18, 2015 at 11:33
  • 1
    Why the heck does such a problem need to be solved with a batch ? Use a real programming language for this or at least Windows scripting host which is present on current versions of Windows. Commented Oct 18, 2015 at 11:39
  • 1
    Please show where %%A comes from... Commented Oct 18, 2015 at 11:55

2 Answers 2

1

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
) 
Sign up to request clarification or add additional context in comments.

1 Comment

1. this might fail if there are spaces in the file names, or also special characters like & and ^; 2. what's the REM line for?
0

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 <--

1 Comment

I have it! I set "setLocal" to disable and if for cycle I enable it :) thank you all so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.