5

I have more 3000 files in a folder. I want to find and replace text with another one. How can I do that? I'm a newbie in batch script. I can replace it in 1 file but I don't know how to replace in multiple files.

FOR /F %%L IN (lala.txt) DO (
    SET "line=%%L"
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "x=!line:E:\Test=E:\Test\Temp!"
    echo f | xcopy /E !line! !x! 
    ENDLOCAL
)

How can I edit my code to replace the string in all files? Waiting for your help. Thanks

Vik David
  • 3,154
  • 3
  • 19
  • 26

2 Answers2

9

Install the Find And Replace Text command line utility and then you can simply enter

fart *.txt E:\Test E:\Test\Temp
wimh
  • 14,481
  • 5
  • 42
  • 89
3

You could use a second loop for the files.

for %%f in (*.txt) do (
    FOR /F %%L IN (%%f) DO (
      SET "line=%%L"
      SETLOCAL ENABLEDELAYEDEXPANSION 
      set "x=!line:E:\Test=E:\Test\Temp!" 
      echo f | xcopy /E !line! !x! 
      ENDLOCAL
  )
)

This code shows only how to build the loop for process all text files.
The inner code uses the code of the OP, which will not replace anything, but this wasn't the question.

jeb
  • 70,992
  • 15
  • 159
  • 202
  • i'm curious why you enable delayed expansion inside the loop, isn't more eficient to do it just once outside the loop? – PA. Mar 12 '12 at 10:13
  • and, what is in the echo|xcopy combination that replaces the original file? – PA. Mar 12 '12 at 10:15
  • 1
    I just copy the original block from the OP :-) But the EnableDelayedExpansion should be inside the loop for the [safe toggling technic](http://stackoverflow.com/a/4531090/463115) – jeb Mar 12 '12 at 10:30