Find and replace a string in multiple files using

2019-06-22 07:26发布

问题:

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

回答1:

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

fart *.txt E:\Test E:\Test\Temp


回答2:

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.